diff --git a/README.md b/README.md index 9cabaab..8976ed1 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,29 @@ Generic ComfyUI utility nodes under the Dumas brand. The repository contains image, JSON and video utilities and intentionally has no model-specific video sampler dependency. -## Video nodes +## Sequential video assembly ### Dumas Standalone Video -Encodes an IMAGE batch and optional AUDIO into two files: - -- `full_clip`: every generated frame, including any continuation overlap -- `complete_clip`: the same clip with `overlap_frames` removed before encoding - -The full clip is the source for the next sequential generation. The complete -clip is the assembly-ready segment. Removing the overlap before encoding means -the final join does not need to re-encode an already assembled film. +`DumasStandaloneVideo` saves both the complete generated clip and a full copy. +For a continuation clip, set `reference_frames` to the number of frames taken +from the previous clip for continuity. The value is returned as the final +`reference_frames` output so it can be wired directly into +`DumasJoinVideoClips`. ### Dumas Join Video Clips (No Re-encode) -Accepts up to six completed clip paths and concatenates them with FFmpeg stream -copy (`-c copy`). It validates codec, dimensions, frame rate, pixel format and -audio compatibility before joining. The input clips must already have their -continuation overlap removed. +`DumasJoinVideoClips` accepts one reference-frame input for each clip. It trims +that many frames from the beginning of any clip whose value is greater than +zero, then concatenates the resulting files with FFmpeg stream copy. This +avoids a full re-encode of the assembled video. Clip 1 normally remains at +zero; continuation clips receive the reference-frame output from their +standalone nodes. + +Stream-copy trimming is codec/keyframe-aware: the requested boundary is +converted using the clip's frame rate, but a codec may choose the nearest safe +packet boundary. For exact frame cuts, use the existing `complete_clip` output +or accept a re-encoded workflow. ## Other nodes diff --git a/dumas_video_nodes.py b/dumas_video_nodes.py index 6cd2162..9f2cbf3 100644 --- a/dumas_video_nodes.py +++ b/dumas_video_nodes.py @@ -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 diff --git a/tests/test_dumas_video_nodes.py b/tests/test_dumas_video_nodes.py index e7c84dc..dbe3dbc 100644 --- a/tests/test_dumas_video_nodes.py +++ b/tests/test_dumas_video_nodes.py @@ -22,8 +22,23 @@ class DumasVideoNodeTests(unittest.TestCase): def test_standalone_node_exposes_full_and_complete_outputs(self): node = self.module.DumasStandaloneVideoNode - self.assertEqual(node.RETURN_NAMES, ("full_clip", "complete_clip", "complete_frames", "duration")) + self.assertEqual( + node.RETURN_NAMES, + ("full_clip", "complete_clip", "complete_frames", "duration", "reference_frames"), + ) self.assertIn("overlap_frames", node.INPUT_TYPES()["required"]) + self.assertIn("reference_frames", node.INPUT_TYPES()["required"]) + + def test_join_exposes_reference_frame_inputs(self): + optional = self.module.DumasJoinVideoClipsNode.INPUT_TYPES()["optional"] + self.assertEqual(optional["clip_2_reference_frames"][1]["default"], 0) + self.assertEqual(optional["clip_6_reference_frames"][1]["max"], 4096) + + def test_video_fps_parses_frame_rate(self): + self.assertEqual( + self.module._video_fps({"streams": [{"codec_type": "video", "r_frame_rate": "24/1"}]}), + 24.0, + ) def test_join_signature_contains_stream_compatibility_fields(self): signature = self.module._join_signature(