Files
DumasNodes/dumas_video_nodes.py
T

445 lines
19 KiB
Python

"""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("<i2").tobytes()
with wave.open(str(path), "wb") as stream:
stream.setnchannels(values.shape[0])
stream.setsampwidth(2)
stream.setframerate(sample_rate)
stream.writeframes(interleaved)
return sample_rate
def _encode_images(images, audio, output_path, fps, start_frame=0, quality=18):
if images is None or len(images) <= start_frame:
raise ValueError("Dumas video output has no frames after the requested overlap.")
first = images[0].detach().cpu().numpy() if hasattr(images[0], "detach") else np.asarray(images[0])
height, width = int(first.shape[0]), int(first.shape[1])
wav_path = None
audio_rate = None
try:
prepared = _audio_arrays(audio)
if prepared is not None:
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as handle:
wav_path = Path(handle.name)
audio_rate = _write_wav(audio, wav_path, int(round(start_frame / fps * prepared[1])))
command = [
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y",
"-f", "rawvideo", "-pix_fmt", "rgb24", "-s", f"{width}x{height}",
"-r", str(float(fps)), "-i", "-",
]
if wav_path is not None:
command += ["-i", str(wav_path)]
crf = max(1, min(51, int(quality)))
command += ["-map", "0:v:0", "-c:v", "libx264", "-preset", "fast", "-crf", str(crf), "-pix_fmt", "yuv420p"]
if wav_path is not None:
command += ["-map", "1:a:0", "-c:a", "aac", "-b:a", "192k", "-shortest"]
command += ["-movflags", "+faststart", str(output_path)]
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
for frame in images[start_frame:]:
array = frame.detach().cpu().numpy() if hasattr(frame, "detach") else np.asarray(frame)
if array.shape[:2] != (height, width) or array.shape[-1] < 3:
raise ValueError("All video frames must have the same RGB dimensions.")
process.stdin.write(np.ascontiguousarray(np.clip(array[..., :3] * 255.0, 0, 255).astype(np.uint8)).tobytes())
process.stdin.close()
except Exception:
process.kill()
process.wait()
raise
error = process.stderr.read().decode("utf-8", "replace")
result = process.wait()
if result != 0:
raise RuntimeError(f"ffmpeg could not encode the video: {error.strip()}")
finally:
if wav_path is not None:
wav_path.unlink(missing_ok=True)
return str(output_path)
def _probe(path):
result = subprocess.run(
["ffprobe", "-v", "error", "-show_streams", "-show_format", "-of", "json", str(path)],
capture_output=True, text=True, check=False,
)
if result.returncode != 0:
raise RuntimeError(f"ffprobe could not inspect {path}: {result.stderr.strip()}")
return json.loads(result.stdout or "{}")
def _join_signature(info):
streams = info.get("streams", [])
video = next((item for item in streams if item.get("codec_type") == "video"), None)
audio = next((item for item in streams if item.get("codec_type") == "audio"), None)
if video is None:
raise ValueError("Every joined clip must contain a video stream.")
return (
video.get("codec_name"), video.get("width"), video.get("height"),
video.get("pix_fmt"), video.get("r_frame_rate"),
(audio or {}).get("codec_name"), (audio or {}).get("sample_rate"),
(audio or {}).get("channels"),
)
def _video_fps(info):
streams = info.get("streams", [])
video = next((item for item in streams if item.get("codec_type") == "video"), None)
if video is None:
raise ValueError("Every joined clip must contain a video stream.")
raw = str(video.get("r_frame_rate") or video.get("avg_frame_rate") or "")
try:
numerator, denominator = raw.split("/", 1)
fps = float(numerator) / float(denominator)
except (ValueError, ZeroDivisionError):
fps = 0.0
if fps <= 0:
raise ValueError("Could not determine a valid video frame rate.")
return fps
def _trim_stream_copy(path, reference_frames, fps, temporary_directory):
"""Trim continuation overlap without decoding or re-encoding media."""
output = Path(temporary_directory) / f"trimmed_{time.time_ns()}.mp4"
start_seconds = float(reference_frames) / float(fps)
command = [
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y",
"-ss", f"{start_seconds:.6f}", "-i", str(path),
"-map", "0", "-c", "copy", "-avoid_negative_ts", "make_zero",
"-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 trim continuation overlap: {result.stderr.strip()}")
return output
def _run_stream_copy(command, error_message):
result = subprocess.run(command, capture_output=True, text=True, check=False)
if result.returncode != 0:
raise RuntimeError(f"{error_message}: {result.stderr.strip()}")
def _stream_copy_trim_to_output(path, start_seconds, output):
_run_stream_copy([
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y",
"-ss", f"{float(start_seconds):.6f}", "-i", str(path),
"-map", "0", "-c", "copy", "-avoid_negative_ts", "make_zero",
"-movflags", "+faststart", str(output),
], "ffmpeg could not create the clean continuation clip")
def _stream_copy_tail(path, seconds, output):
_run_stream_copy([
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y",
"-sseof", f"-{float(seconds):.6f}", "-i", str(path),
"-map", "0", "-c", "copy", "-avoid_negative_ts", "make_zero",
"-movflags", "+faststart", str(output),
], "ffmpeg could not extract the continuation tail")
class DumasStandaloneVideoNode:
"""Save a full generated clip and a trimmed, assembly-ready clip."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"fps": ("FLOAT", {"default": 24.0, "min": 1.0, "max": 240.0, "step": 0.001}),
"overlap_frames": ("INT", {"default": 0, "min": 0, "max": 4096}),
"reference_frames": ("INT", {"default": 0, "min": 0, "max": 4096}),
"filename_prefix": ("STRING", {"default": "dumas/clip"}),
},
"optional": {"audio": ("AUDIO",)},
}
RETURN_TYPES = ("STRING", "STRING", "INT", "FLOAT", "INT")
RETURN_NAMES = ("full_clip", "complete_clip", "complete_frames", "duration", "reference_frames")
FUNCTION = "save"
CATEGORY = "Dumas/Video"
OUTPUT_NODE = True
def save(self, images, fps, overlap_frames, filename_prefix, reference_frames=0, audio=None):
total = len(images)
overlap = int(overlap_frames)
reference = int(reference_frames)
if overlap >= 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)",
}