Add reference-aware no-reencode video joining

This commit is contained in:
2026-10-12 09:05:22 +00:00
parent a4063ef60a
commit 69d93e685a
3 changed files with 119 additions and 34 deletions
+86 -20
View File
@@ -144,6 +144,38 @@ def _join_signature(info):
)
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
class DumasStandaloneVideoNode:
"""Save a full generated clip and a trimmed, assembly-ready clip."""
@@ -154,28 +186,32 @@ class DumasStandaloneVideoNode:
"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")
RETURN_NAMES = ("full_clip", "complete_clip", "complete_frames", "duration")
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, audio=None):
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)
return str(full), str(complete), count, count / float(fps), reference
class DumasJoinVideoClipsNode:
@@ -191,6 +227,12 @@ class DumasJoinVideoClipsNode:
"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"}),
},
}
@@ -201,30 +243,54 @@ class DumasJoinVideoClipsNode:
CATEGORY = "Dumas/Video"
OUTPUT_NODE = True
def join(self, clip_1, clip_2="", clip_3="", clip_4="", clip_5="", clip_6="", filename_prefix="dumas/assembled_video"):
clips = [str(path).strip().strip('"').strip("'") for path in (clip_1, clip_2, clip_3, clip_4, clip_5, clip_6) if str(path or "").strip()]
if not clips:
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}")
signatures = [_join_signature(_probe(path)) for path in clips]
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.NamedTemporaryFile("w", suffix=".txt", delete=False, encoding="utf-8") as handle:
list_path = Path(handle.name)
for path in clips:
escaped = str(Path(path).resolve()).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)
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