Add reference-aware no-reencode video joining
This commit is contained in:
@@ -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
|
image, JSON and video utilities and intentionally has no model-specific video
|
||||||
sampler dependency.
|
sampler dependency.
|
||||||
|
|
||||||
## Video nodes
|
## Sequential video assembly
|
||||||
|
|
||||||
### Dumas Standalone Video
|
### Dumas Standalone Video
|
||||||
|
|
||||||
Encodes an IMAGE batch and optional AUDIO into two files:
|
`DumasStandaloneVideo` saves both the complete generated clip and a full copy.
|
||||||
|
For a continuation clip, set `reference_frames` to the number of frames taken
|
||||||
- `full_clip`: every generated frame, including any continuation overlap
|
from the previous clip for continuity. The value is returned as the final
|
||||||
- `complete_clip`: the same clip with `overlap_frames` removed before encoding
|
`reference_frames` output so it can be wired directly into
|
||||||
|
`DumasJoinVideoClips`.
|
||||||
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.
|
|
||||||
|
|
||||||
### Dumas Join Video Clips (No Re-encode)
|
### Dumas Join Video Clips (No Re-encode)
|
||||||
|
|
||||||
Accepts up to six completed clip paths and concatenates them with FFmpeg stream
|
`DumasJoinVideoClips` accepts one reference-frame input for each clip. It trims
|
||||||
copy (`-c copy`). It validates codec, dimensions, frame rate, pixel format and
|
that many frames from the beginning of any clip whose value is greater than
|
||||||
audio compatibility before joining. The input clips must already have their
|
zero, then concatenates the resulting files with FFmpeg stream copy. This
|
||||||
continuation overlap removed.
|
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
|
## Other nodes
|
||||||
|
|
||||||
|
|||||||
+86
-20
@@ -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:
|
class DumasStandaloneVideoNode:
|
||||||
"""Save a full generated clip and a trimmed, assembly-ready clip."""
|
"""Save a full generated clip and a trimmed, assembly-ready clip."""
|
||||||
|
|
||||||
@@ -154,28 +186,32 @@ class DumasStandaloneVideoNode:
|
|||||||
"images": ("IMAGE",),
|
"images": ("IMAGE",),
|
||||||
"fps": ("FLOAT", {"default": 24.0, "min": 1.0, "max": 240.0, "step": 0.001}),
|
"fps": ("FLOAT", {"default": 24.0, "min": 1.0, "max": 240.0, "step": 0.001}),
|
||||||
"overlap_frames": ("INT", {"default": 0, "min": 0, "max": 4096}),
|
"overlap_frames": ("INT", {"default": 0, "min": 0, "max": 4096}),
|
||||||
|
"reference_frames": ("INT", {"default": 0, "min": 0, "max": 4096}),
|
||||||
"filename_prefix": ("STRING", {"default": "dumas/clip"}),
|
"filename_prefix": ("STRING", {"default": "dumas/clip"}),
|
||||||
},
|
},
|
||||||
"optional": {"audio": ("AUDIO",)},
|
"optional": {"audio": ("AUDIO",)},
|
||||||
}
|
}
|
||||||
|
|
||||||
RETURN_TYPES = ("STRING", "STRING", "INT", "FLOAT")
|
RETURN_TYPES = ("STRING", "STRING", "INT", "FLOAT", "INT")
|
||||||
RETURN_NAMES = ("full_clip", "complete_clip", "complete_frames", "duration")
|
RETURN_NAMES = ("full_clip", "complete_clip", "complete_frames", "duration", "reference_frames")
|
||||||
FUNCTION = "save"
|
FUNCTION = "save"
|
||||||
CATEGORY = "Dumas/Video"
|
CATEGORY = "Dumas/Video"
|
||||||
OUTPUT_NODE = True
|
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)
|
total = len(images)
|
||||||
overlap = int(overlap_frames)
|
overlap = int(overlap_frames)
|
||||||
|
reference = int(reference_frames)
|
||||||
if overlap >= total:
|
if overlap >= total:
|
||||||
raise ValueError(f"Overlap ({overlap}) must be smaller than the clip ({total} frames).")
|
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")
|
full = _output_path(f"{filename_prefix}_full")
|
||||||
complete = _output_path(f"{filename_prefix}_complete")
|
complete = _output_path(f"{filename_prefix}_complete")
|
||||||
_encode_images(images, audio, full, float(fps), 0)
|
_encode_images(images, audio, full, float(fps), 0)
|
||||||
_encode_images(images, audio, complete, float(fps), overlap)
|
_encode_images(images, audio, complete, float(fps), overlap)
|
||||||
count = total - 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:
|
class DumasJoinVideoClipsNode:
|
||||||
@@ -191,6 +227,12 @@ class DumasJoinVideoClipsNode:
|
|||||||
"clip_4": ("STRING", {"default": ""}),
|
"clip_4": ("STRING", {"default": ""}),
|
||||||
"clip_5": ("STRING", {"default": ""}),
|
"clip_5": ("STRING", {"default": ""}),
|
||||||
"clip_6": ("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"}),
|
"filename_prefix": ("STRING", {"default": "dumas/assembled_video"}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -201,30 +243,54 @@ class DumasJoinVideoClipsNode:
|
|||||||
CATEGORY = "Dumas/Video"
|
CATEGORY = "Dumas/Video"
|
||||||
OUTPUT_NODE = True
|
OUTPUT_NODE = True
|
||||||
|
|
||||||
def join(self, clip_1, clip_2="", clip_3="", clip_4="", clip_5="", clip_6="", filename_prefix="dumas/assembled_video"):
|
def join(
|
||||||
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()]
|
self, clip_1, clip_2="", clip_3="", clip_4="", clip_5="", clip_6="",
|
||||||
if not clips:
|
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.")
|
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):
|
if any(not os.path.isfile(path) for path in clips):
|
||||||
missing = next(path for path in clips if not os.path.isfile(path))
|
missing = next(path for path in clips if not os.path.isfile(path))
|
||||||
raise FileNotFoundError(f"Completed clip not found: {missing}")
|
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:]):
|
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.")
|
raise ValueError("Clips do not share compatible codec, dimensions, frame rate, pixel format or audio format.")
|
||||||
|
|
||||||
output = _output_path(filename_prefix)
|
output = _output_path(filename_prefix)
|
||||||
with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False, encoding="utf-8") as handle:
|
with tempfile.TemporaryDirectory(prefix="dumas_join_") as temporary_directory:
|
||||||
list_path = Path(handle.name)
|
prepared = []
|
||||||
for path in clips:
|
for (path, reference), info in zip(entries, probed):
|
||||||
escaped = str(Path(path).resolve()).replace("'", "'\\''")
|
if reference:
|
||||||
handle.write(f"file '{escaped}'\n")
|
prepared.append(_trim_stream_copy(path, reference, _video_fps(info), temporary_directory))
|
||||||
try:
|
else:
|
||||||
command = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-f", "concat", "-safe", "0", "-i", str(list_path), "-c", "copy", "-movflags", "+faststart", str(output)]
|
prepared.append(Path(path).resolve())
|
||||||
result = subprocess.run(command, capture_output=True, text=True, check=False)
|
with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False, encoding="utf-8") as handle:
|
||||||
if result.returncode != 0:
|
list_path = Path(handle.name)
|
||||||
raise RuntimeError(f"ffmpeg could not join the clips without re-encoding: {result.stderr.strip()}")
|
for path in prepared:
|
||||||
finally:
|
escaped = str(path).replace("'", "'\\''")
|
||||||
list_path.unlink(missing_ok=True)
|
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)
|
duration = float((_probe(output).get("format") or {}).get("duration") or 0.0)
|
||||||
return str(output), len(clips), duration
|
return str(output), len(clips), duration
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,23 @@ class DumasVideoNodeTests(unittest.TestCase):
|
|||||||
|
|
||||||
def test_standalone_node_exposes_full_and_complete_outputs(self):
|
def test_standalone_node_exposes_full_and_complete_outputs(self):
|
||||||
node = self.module.DumasStandaloneVideoNode
|
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("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):
|
def test_join_signature_contains_stream_compatibility_fields(self):
|
||||||
signature = self.module._join_signature(
|
signature = self.module._join_signature(
|
||||||
|
|||||||
Reference in New Issue
Block a user