"""Generic video helpers for sequential ComfyUI projects. The nodes in this module deliberately operate on ordinary IMAGE/AUDIO values and completed video paths. They do not depend on a particular video model or custom sampler. """ import json import os import re import subprocess import tempfile import time import wave from pathlib import Path try: import numpy as np except ImportError: # The ComfyUI runtime provides NumPy; keep import-time tests lightweight. np = None import folder_paths try: from .dumas_image_nodes import _next_available_relative_path except ImportError: # Support the lightweight standalone test import. from dumas_image_nodes import _next_available_relative_path _SAFE_NAME = re.compile(r"[^A-Za-z0-9_.-]+") def _safe_name(value, fallback): name = _SAFE_NAME.sub("_", str(value or "").strip()).strip("._") return name or fallback def _output_path(prefix, suffix="mp4"): root = Path(folder_paths.get_output_directory()) root.mkdir(parents=True, exist_ok=True) stamp = time.strftime("%Y%m%d_%H%M%S") return root / f"{_safe_name(prefix, 'clip')}_{stamp}_{time.time_ns() % 1000000:06d}.{suffix}" def _audio_arrays(audio): if np is None: raise RuntimeError("NumPy is required to encode video frames.") if not audio: return None waveform = audio.get("waveform") if isinstance(audio, dict) else None sample_rate = int(audio.get("sample_rate", 48000)) if isinstance(audio, dict) else 48000 if waveform is None: return None values = waveform.detach().cpu().numpy() if hasattr(waveform, "detach") else np.asarray(waveform) if values.ndim == 3: values = values[0] if values.ndim == 1: values = values[None, :] if values.shape[0] == 1: values = np.repeat(values, 2, axis=0) values = values[:2] return np.clip(values, -1.0, 1.0), sample_rate def _write_wav(audio, path, start_sample=0): prepared = _audio_arrays(audio) if prepared is None: return None values, sample_rate = prepared values = values[:, start_sample:] interleaved = (values.T * 32767.0).astype("= total: raise ValueError(f"Overlap ({overlap}) must be smaller than the clip ({total} frames).") if reference >= total: raise ValueError(f"Reference frames ({reference}) must be smaller than the clip ({total} frames).") full = _output_path(f"{filename_prefix}_full") complete = _output_path(f"{filename_prefix}_complete") _encode_images(images, audio, full, float(fps), 0) _encode_images(images, audio, complete, float(fps), overlap) count = total - overlap return str(full), str(complete), count, count / float(fps), reference class DumasSaveVideoNode: """Save an IMAGE batch as an MP4 in any user-selected folder.""" DESCRIPTION = ( "Save Video DUMAS writes an image batch as an MP4 to any folder on the " "server. Leave the folder empty for ComfyUI's output folder, or type " "an absolute path. Existing files are never overwritten." ) @classmethod def INPUT_TYPES(cls): return { "required": { "images": ("IMAGE",), "fps": ("FLOAT", {"default": 24.0, "min": 1.0, "max": 240.0, "step": 0.001}), "folder": ("STRING", {"default": "", "multiline": False}), "filename": ("STRING", {"default": "dumas_video_%counter%", "multiline": False}), "quality": ("INT", {"default": 18, "min": 1, "max": 51}), "save_on_run": ("BOOLEAN", {"default": True}), }, "optional": {"audio": ("AUDIO",)}, } @classmethod def IS_CHANGED(cls, **_kwargs): return float("nan") RETURN_TYPES = ("STRING", "INT", "FLOAT") RETURN_NAMES = ("video_path", "frames", "duration") FUNCTION = "save" CATEGORY = "Dumas/Video" OUTPUT_NODE = True def save(self, images, fps, folder, filename, quality, save_on_run=True, audio=None): if images is None or len(images) == 0: raise ValueError("Save Video DUMAS received no frames.") fps = float(fps) if fps <= 0: raise ValueError("FPS must be greater than zero.") base = Path(folder).expanduser() if str(folder or "").strip() else Path(folder_paths.get_output_directory()) if not save_on_run: base = Path(folder_paths.get_temp_directory()) base = base.resolve() base.mkdir(parents=True, exist_ok=True) relative = str(filename or "dumas_video_%counter%").replace("\\", "/").strip().lstrip("/") if not relative.lower().endswith(".mp4"): relative += ".mp4" relative = _next_available_relative_path(str(base), relative) target = (base / relative).resolve() if os.path.commonpath((str(base), str(target))) != str(base): raise ValueError("Video filename must stay inside the selected folder.") _encode_images(images, audio, target, fps, 0, quality) frames = len(images) return {"ui": {"text": [str(target)]}, "result": (str(target), frames, frames / fps)} class DumasPrepareContinuationNode: """Prepare a generated continuation for chaining and presentation. The raw clip is never fed forward as a whole. The node extracts only its trailing reference window for the next generation and creates a clean clip with the regenerated lead removed. """ @classmethod def INPUT_TYPES(cls): return { "required": { "raw_clip": ("STRING", {"default": "", "multiline": False}), "reference_frames": ("INT", {"default": 48, "min": 1, "max": 4096}), "fps": ("FLOAT", {"default": 24.0, "min": 1.0, "max": 240.0, "step": 0.001}), "filename_prefix": ("STRING", {"default": "dumas/continuation"}), }, } RETURN_TYPES = ("STRING", "STRING", "INT", "FLOAT") RETURN_NAMES = ("clean_clip", "next_reference", "reference_frames", "clean_duration") FUNCTION = "prepare" CATEGORY = "Dumas/Video" OUTPUT_NODE = True def prepare(self, raw_clip, reference_frames, fps, filename_prefix): path = str(raw_clip or "").strip().strip('"').strip("'") if not path: raise ValueError("Connect a raw generated continuation clip.") if not os.path.isfile(path): raise FileNotFoundError(f"Raw continuation clip not found: {path}") reference = int(reference_frames) source_info = _probe(path) source_fps = _video_fps(source_info) if abs(source_fps - float(fps)) > 0.01: fps = source_fps duration = float((source_info.get("format") or {}).get("duration") or 0.0) reference_seconds = reference / float(fps) if duration <= reference_seconds: raise ValueError("Reference window must be shorter than the raw clip.") clean = _output_path(f"{filename_prefix}_clean") tail = _output_path(f"{filename_prefix}_tail") _stream_copy_trim_to_output(path, reference_seconds, clean) _stream_copy_tail(path, reference_seconds, tail) clean_duration = max(0.0, duration - reference_seconds) return str(clean), str(tail), reference, clean_duration class DumasJoinVideoClipsNode: """Concatenate already-complete clips with FFmpeg stream copy.""" @classmethod def INPUT_TYPES(cls): return { "required": {"clip_1": ("STRING", {"default": "", "multiline": False})}, "optional": { "clip_2": ("STRING", {"default": ""}), "clip_3": ("STRING", {"default": ""}), "clip_4": ("STRING", {"default": ""}), "clip_5": ("STRING", {"default": ""}), "clip_6": ("STRING", {"default": ""}), "clip_1_reference_frames": ("INT", {"default": 0, "min": 0, "max": 4096}), "clip_2_reference_frames": ("INT", {"default": 0, "min": 0, "max": 4096}), "clip_3_reference_frames": ("INT", {"default": 0, "min": 0, "max": 4096}), "clip_4_reference_frames": ("INT", {"default": 0, "min": 0, "max": 4096}), "clip_5_reference_frames": ("INT", {"default": 0, "min": 0, "max": 4096}), "clip_6_reference_frames": ("INT", {"default": 0, "min": 0, "max": 4096}), "filename_prefix": ("STRING", {"default": "dumas/assembled_video"}), }, } RETURN_TYPES = ("STRING", "INT", "FLOAT") RETURN_NAMES = ("joined_clip", "clip_count", "duration") FUNCTION = "join" CATEGORY = "Dumas/Video" OUTPUT_NODE = True def join( self, clip_1, clip_2="", clip_3="", clip_4="", clip_5="", clip_6="", clip_1_reference_frames=0, clip_2_reference_frames=0, clip_3_reference_frames=0, clip_4_reference_frames=0, clip_5_reference_frames=0, clip_6_reference_frames=0, filename_prefix="dumas/assembled_video", ): raw_clips = (clip_1, clip_2, clip_3, clip_4, clip_5, clip_6) raw_references = ( clip_1_reference_frames, clip_2_reference_frames, clip_3_reference_frames, clip_4_reference_frames, clip_5_reference_frames, clip_6_reference_frames, ) entries = [ (str(path).strip().strip('"').strip("'"), max(0, int(reference))) for path, reference in zip(raw_clips, raw_references) if str(path or "").strip() ] if not entries: raise ValueError("Connect at least one completed clip.") clips = [path for path, _ in entries] if any(not os.path.isfile(path) for path in clips): missing = next(path for path in clips if not os.path.isfile(path)) raise FileNotFoundError(f"Completed clip not found: {missing}") probed = [_probe(path) for path in clips] signatures = [_join_signature(info) for info in probed] if any(signature != signatures[0] for signature in signatures[1:]): raise ValueError("Clips do not share compatible codec, dimensions, frame rate, pixel format or audio format.") output = _output_path(filename_prefix) with tempfile.TemporaryDirectory(prefix="dumas_join_") as temporary_directory: prepared = [] for (path, reference), info in zip(entries, probed): if reference: prepared.append(_trim_stream_copy(path, reference, _video_fps(info), temporary_directory)) else: prepared.append(Path(path).resolve()) with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False, encoding="utf-8") as handle: list_path = Path(handle.name) for path in prepared: escaped = str(path).replace("'", "'\\''") handle.write(f"file '{escaped}'\n") try: command = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-f", "concat", "-safe", "0", "-i", str(list_path), "-c", "copy", "-movflags", "+faststart", str(output)] result = subprocess.run(command, capture_output=True, text=True, check=False) if result.returncode != 0: raise RuntimeError(f"ffmpeg could not join the clips without re-encoding: {result.stderr.strip()}") finally: list_path.unlink(missing_ok=True) duration = float((_probe(output).get("format") or {}).get("duration") or 0.0) return str(output), len(clips), duration NODE_CLASS_MAPPINGS = { "DumasStandaloneVideo": DumasStandaloneVideoNode, "DumasSaveVideo": DumasSaveVideoNode, "DumasPrepareContinuation": DumasPrepareContinuationNode, "DumasJoinVideoClips": DumasJoinVideoClipsNode, } NODE_DISPLAY_NAME_MAPPINGS = { "DumasStandaloneVideo": "Dumas Standalone Video", "DumasSaveVideo": "DUMAS Pixaroma ยท Save Video", "DumasPrepareContinuation": "Dumas Prepare Continuation", "DumasJoinVideoClips": "Dumas Join Video Clips (No Re-encode)", }