From 22310bef00d79e47ffe6852206b02294063fa8ac Mon Sep 17 00:00:00 2001 From: Z User Date: Sun, 27 Sep 2026 02:18:11 +0000 Subject: [PATCH] fix: compose the project timeline at its native size and restore full resolution before any combine output The compose step previously built the timeline at the manifest resolution while staging segments are written at the (hidden) half-res sampling size, so ffmpeg's overlay pasted the smaller segments into the top-left quarter of a black full-res canvas and the combine's restore gate saw frames already at the manifest size and never fired. - compose_h3_project_video now probes the timeline segments and composes at the size they all share (_uniform_probed_size; manifest dims remain the fallback for mixed/unknown sizes) - restore_project_video_resolution (utils/rtx_vsr.py) fast-probes the composed file, and when it differs from the manifest decodes once, upscales (RTX VSR, Lanczos fallback, exact size) and rebuilds the video so VIDEO, images, audio and fps all carry the editor-selected resolution; any failure keeps the original video - the restore lives in the base combine so MoxieCombine (images slot) and the legacy GetVideoComponents wiring are both covered; the duplicated images gate in MoxieCombine is gone --- CHANGELOG.md | 2 +- moxie_combine.py | 14 +- nodes/project.py | 12 +- scripts/test_rtx_upscale_integration.py | 224 ++++++++++++++++++++++-- utils/h3_project.py | 46 ++++- utils/rtx_vsr.py | 59 +++++++ 6 files changed, 330 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f322b9..212fbdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ ### ⚡ Improvements -- **MultiTrack Project / Moxie Project Video Combine**: Single-pass projects now condition and sample at half the editor-selected resolution internally, and Moxie Project Video Combine restores the exact editor-selected resolution on the combined frames right before its `images` output (RTX Video Super Resolution via the optional `nvidia-vfx` package when available, with an automatic Lanczos fallback otherwise). The editor resolution therefore stays the final output resolution while the sampling chain runs smaller and faster. Dual-pass, SelfLift, and passthrough modes are unchanged. +- **MultiTrack Project / Moxie Project Video Combine**: Single-pass projects now condition and sample at half the editor-selected resolution internally. The project timeline is composed at the segments' own size, and Moxie Project Video Combine restores the exact editor-selected resolution in pixel space before any output leaves the node (RTX Video Super Resolution via the optional `nvidia-vfx` package when available, with an automatic Lanczos fallback otherwise) — so both the `VIDEO` output and the `images`/`audio`/`fps` outputs carry the full editor resolution regardless of template wiring. The editor resolution therefore stays the final output resolution while the sampling chain runs smaller and faster. Dual-pass, SelfLift, and passthrough modes are unchanged. - **Moxie Multimedia Loader**: The `lora_name` combo now defaults to the bundled turbo LoRA (`MM3step.safetensors`) so a fresh clone of the pack works out of the box, with a `none` entry to disable LoRA. Previously the default was a `""` placeholder, which silently skipped LoRA application even when a LoRA was bundled. - **Moxie Preview Override**: The `tiny_vae` combo now lists files from the node pack's own `models/vae_approx` folder first (shipping `MM-preview.safetensors` for MiniMax H3 previews as the default), falling back to ComfyUI's standard `models/vae_approx` directory. The folder is rescanned on every schema refresh, so newly dropped files appear without restarting ComfyUI. - **Moxie Multimedia Loader**: Locked the loader to the MiniMax H3 model set. The `clip_type` dropdown (which previously exposed every CLIP recipe in ComfyUI) was removed; the H3 Qwen3-VL text encoder is now always loaded with the `minimax` CLIP recipe. Any older ComfyUI build without `CLIPType.MINIMAX` raises a clear startup error instead of silently loading the wrong encoder. diff --git a/moxie_combine.py b/moxie_combine.py index 94c6ee0..51b6fb6 100644 --- a/moxie_combine.py +++ b/moxie_combine.py @@ -4,8 +4,6 @@ from comfy_api.latest import io from comfy_execution.graph_utils import ExecutionBlocker from .nodes.project import EasyMultiTrackProjectVideoCombine, TYPE_PROJECT_DATA -from .utils.h3_project import safe_h3_project_name -from .utils.rtx_vsr import upscale_images_to_project_resolution class MoxieCombine(EasyMultiTrackProjectVideoCombine): @@ -50,17 +48,15 @@ class MoxieCombine(EasyMultiTrackProjectVideoCombine): if isinstance(video, ExecutionBlocker): return io.NodeOutput(video, video, video, video, video) + # The base combine has already restored the editor-selected + # resolution (utils/rtx_vsr.py) before the video was returned, so + # every output below - including the images slot consumed by + # `easy saveVideo` - carries the full project resolution. components = video.get_components() - # Hidden half-res sampling contract: restore the editor-selected - # resolution right before the frames leave this node. Frames already - # at the project resolution pass through untouched. - images = upscale_images_to_project_resolution( - components.images, safe_h3_project_name(project_name) - ) return io.NodeOutput( video, prefix, - images, + components.images, components.audio, float(components.frame_rate), ) diff --git a/nodes/project.py b/nodes/project.py index 9307066..2ecbf07 100644 --- a/nodes/project.py +++ b/nodes/project.py @@ -36,6 +36,7 @@ from ..utils.h3_project import ( validate_h3_project_outputs, ) from ..utils.models import detect_turbo_lora_from_prompt, detect_turbo_model +from ..utils.rtx_vsr import restore_project_video_resolution from ..utils.project_memory import ( BOUNDARY_META, SEGMENT_META, @@ -2352,7 +2353,16 @@ class EasyMultiTrackProjectVideoCombine(io.ComfyNode): blocker = ExecutionBlocker(None) return io.NodeOutput(blocker, blocker) composed_path = compose_h3_project_video(safe_name, data) - return io.NodeOutput( + # Hidden half-res sampling contract: the timeline is composed at the + # segments' own (sampling) size, so restore the manifest resolution + # here - once - before any output leaves this node. Videos already at + # the project resolution pass through untouched. + video = restore_project_video_resolution( InputImpl.VideoFromFile(str(composed_path)), + safe_name, + source_path=str(composed_path), + ) + return io.NodeOutput( + video, h3_project_filename_prefix(safe_name), ) diff --git a/scripts/test_rtx_upscale_integration.py b/scripts/test_rtx_upscale_integration.py index d4c64f3..da8b6af 100644 --- a/scripts/test_rtx_upscale_integration.py +++ b/scripts/test_rtx_upscale_integration.py @@ -1,8 +1,8 @@ """Standalone sanity checks for the hidden half-res output restore. Runs without ComfyUI or torch: minimal stubs exercise the gating logic of -utils/rtx_vsr.py and verify the plain-halving gate in nodes/project.py plus -the MoxieCombine wiring. +utils/rtx_vsr.py, the compose-at-native-size logic in utils/h3_project.py, +and the restore wiring in the combine nodes. """ from __future__ import annotations @@ -35,10 +35,10 @@ class FakeTensor: self.dtype = "float32" def __getitem__(self, item): - return self + return FakeTensor(self.shape) def movedim(self, *args, **kwargs): - return self + return FakeTensor(self.shape) torch_stub = types.ModuleType("torch") @@ -61,6 +61,54 @@ comfy_stub.utils = comfy_utils_stub sys.modules["comfy"] = comfy_stub sys.modules["comfy.utils"] = comfy_utils_stub +# comfy_api.latest stub (VideoFromComponents / Types.VideoComponents) +comfy_api_pkg = types.ModuleType("comfy_api") +comfy_api_latest = types.ModuleType("comfy_api.latest") + + +class FakeVideoComponents: + def __init__(self, images=None, audio=None, frame_rate=None): + self.images = images + self.audio = audio + self.frame_rate = frame_rate + + +class _Types: + VideoComponents = FakeVideoComponents + + +built_videos: list = [] + + +class _InputImpl: + @classmethod + def VideoFromComponents(cls, components): + video = ("components-video", components) + built_videos.append(video) + return video + + +comfy_api_latest.InputImpl = _InputImpl +comfy_api_latest.Types = _Types +comfy_api_pkg.latest = comfy_api_latest +sys.modules["comfy_api"] = comfy_api_pkg +sys.modules["comfy_api.latest"] = comfy_api_latest + +# utils.video stub (ffprobe only; merge is never called here) +video_stub = types.ModuleType("utils.video") +PROBED: dict = {} +PROBE_RAISE = {"raise": False} + + +def _fake_ffprobe_info(path): + if PROBE_RAISE["raise"]: + raise RuntimeError("probe failed") + return dict(PROBED.get(path, {"width": 1920, "height": 1080})) + + +video_stub.ffprobe_info = _fake_ffprobe_info +sys.modules["utils.video"] = video_stub + MANIFEST: dict = {"width": 0, "height": 0} h3_stub = types.ModuleType("utils.h3_project") h3_stub.safe_h3_project_name = lambda value: str(value or "default") @@ -81,6 +129,7 @@ sys.modules["utils.h3_project"] = h3_stub rtx_vsr = importlib.import_module("utils.rtx_vsr") +# --- rtx_vsr.upscale_images_to_project_resolution --------------------------- # 1. half-res frames are restored to the exact manifest size (lanczos here, # since nvidia-vfx is absent in the sandbox) MANIFEST.update({"width": 1920, "height": 1080}) @@ -129,7 +178,140 @@ check("vsr same-size identity", rtx_vsr.rtx_vsr_upscale(same, 1920, 1080) is sam zero = FakeTensor((0, 528, 960, 3)) check("vsr empty identity", rtx_vsr.rtx_vsr_upscale(zero, 1920, 1080) is zero) -# 7. nodes/project.py carries the plain-halving gate for single-pass only +# --- rtx_vsr.restore_project_video_resolution ------------------------------- +class FakeVideo: + def __init__(self, components=None, error=None): + self._components = components + self._error = error + self.decode_calls = 0 + + def get_components(self): + self.decode_calls += 1 + if self._error is not None: + raise self._error + return self._components + + +# 7. composed file already at manifest size -> untouched, zero decode +upscale_calls.clear() +full_video = FakeVideo() +result = rtx_vsr.restore_project_video_resolution( + full_video, "proj", source_path="composed.mp4" +) +check( + "full-res composed file untouched without decode", + result is full_video + and full_video.decode_calls == 0 + and upscale_calls == [], +) + +# 8. half-res composed file -> decode once, upscale, rebuild the video +upscale_calls.clear() +built_videos.clear() +half_images = FakeTensor((4, 528, 960, 3)) +half_video = FakeVideo(FakeVideoComponents(images=half_images, audio="a", frame_rate=24)) +PROBED["composed.mp4"] = {"width": 960, "height": 528} +result = rtx_vsr.restore_project_video_resolution( + half_video, "proj", source_path="composed.mp4" +) +check( + "half-res composed video rebuilt at manifest size", + result == built_videos[-1] + and half_video.decode_calls == 1 + and result[1].images is not half_images + and result[1].audio == "a" + and result[1].frame_rate == 24 + and upscale_calls == [(1920, 1080, "lanczos", "disabled")], +) + +# 9. failed decode keeps the original video (rendering never breaks) +upscale_calls.clear() +broken_video = FakeVideo(error=RuntimeError("decode failed")) +PROBED["composed.mp4"] = {"width": 960, "height": 528} +result = rtx_vsr.restore_project_video_resolution( + broken_video, "proj", source_path="composed.mp4" +) +check("failed decode keeps original video", result is broken_video) + +# 10. missing manifest keeps the original video without decoding +MANIFEST.update({"missing": True}) +quiet_video = FakeVideo() +result = rtx_vsr.restore_project_video_resolution( + quiet_video, "proj", source_path="composed.mp4" +) +check( + "missing manifest keeps video without decode", + result is quiet_video and quiet_video.decode_calls == 0, +) +MANIFEST.pop("missing") + +# 11. no source path -> decode path decides via the frame gate +upscale_calls.clear() +built_videos.clear() +probeless_video = FakeVideo(FakeVideoComponents(images=FakeTensor((4, 528, 960, 3)))) +result = rtx_vsr.restore_project_video_resolution(probeless_video, "proj") +check( + "no source path still restores", + result == built_videos[-1] + and upscale_calls == [(1920, 1080, "lanczos", "disabled")], +) +PROBED.clear() + +# --- h3_project._uniform_probed_size (exec-extracted: module imports torch) -- +h3_source = (PACK / "utils" / "h3_project.py").read_text(encoding="utf-8") +h3_tree = ast.parse(h3_source) +probe_fn = next( + node + for node in h3_tree.body + if isinstance(node, ast.FunctionDef) and node.name == "_uniform_probed_size" +) +probe_ns: dict = {} +exec( # noqa: S102 - trusted pack source, isolated namespace + compile(ast.Module(body=[probe_fn], type_ignores=[]), "", "exec"), + probe_ns, +) +uniform_probed_size = probe_ns["_uniform_probed_size"] + +check( + "uniform segment sizes compose at native size", + uniform_probed_size( + ["a.mp4", "b.mp4"], lambda p: {"width": 960, "height": 528} + ) + == (960, 528), +) +check( + "mixed segment sizes fall back", + uniform_probed_size( + ["a.mp4", "b.mp4"], + lambda p: {"width": 1920, "height": 1080} + if p == "a.mp4" + else {"width": 960, "height": 528}, + ) + is None, +) +check( + "odd segment sizes fall back", + uniform_probed_size(["a.mp4"], lambda p: {"width": 961, "height": 528}) is None, +) +check( + "unknowable segment sizes fall back", + uniform_probed_size(["a.mp4"], lambda p: {}) is None, +) +check( + "raising probe falls back", + uniform_probed_size(["a.mp4"], lambda p: (_ for _ in ()).throw(RuntimeError("x"))) + is None, +) +check( + "manifest-sized uniform timeline keeps manifest dims", + uniform_probed_size( + ["a.mp4", "b.mp4"], lambda p: {"width": 1920, "height": 1080} + ) + == (1920, 1080), +) + +# --- node wiring ------------------------------------------------------------- +# 12. nodes/project.py: base combine restores before ANY output leaves the node project_source = (PACK / "nodes" / "project.py").read_text(encoding="utf-8") check( "single-pass half-res gate present", @@ -141,19 +323,30 @@ check( "half_up" not in project_source and "h3_halfres_sampling_dimensions" not in project_source, ) +check( + "base combine restores before outputs", + "restore_project_video_resolution(" in project_source + and "source_path=str(composed_path)" in project_source + and "InputImpl.VideoFromFile(str(composed_path))" in project_source, +) +check( + "compose uses the uniform probed size", + "_uniform_probed_size(" in h3_source + and "compose_size[0] if compose_size else width" in h3_source, +) -# 8. MoxieCombine wires the restore right before the images output +# 13. moxie_combine relies on the base restore (no duplicated gate) combine_source = (PACK / "moxie_combine.py").read_text(encoding="utf-8") check( - "combine restores before images output", - "upscale_images_to_project_resolution" in combine_source, + "moxie_combine has no duplicated upscale gate", + "upscale_images_to_project_resolution" not in combine_source, ) check( - "combine passes the safe project name", - "safe_h3_project_name(project_name)" in combine_source, + "moxie_combine exposes restored components", + "components.images" in combine_source, ) -# 9. heavy imports stay lazy in rtx_vsr (pack import stays light) +# 14. heavy imports stay lazy in rtx_vsr (pack import stays light) tree = ast.parse((PACK / "utils" / "rtx_vsr.py").read_text(encoding="utf-8")) module_level: list[str] = [] for node in tree.body: @@ -168,8 +361,13 @@ check( ), ) -# 10. everything touched compiles -for rel in ("nodes/project.py", "moxie_combine.py", "utils/rtx_vsr.py"): +# 15. everything touched compiles +for rel in ( + "nodes/project.py", + "moxie_combine.py", + "utils/rtx_vsr.py", + "utils/h3_project.py", +): try: py_compile.compile(str(PACK / rel), doraise=True) check(f"py_compile {rel}", True) diff --git a/utils/h3_project.py b/utils/h3_project.py index 556a6c4..7c15609 100644 --- a/utils/h3_project.py +++ b/utils/h3_project.py @@ -1161,9 +1161,39 @@ def _h3_project_data( } +def _uniform_probed_size( + sources: list[str], probe +) -> tuple[int, int] | None: + """The single size shared by every source file, or ``None``. + + Returns ``None`` when sizes are unknown, mixed, non-positive, or not + even (video encoders require an even canvas) - callers then keep their + own fallback size. Only used as a safety refinement: uniform timelines + compose at their native size instead of the manifest canvas. Any probe + failure degrades to ``None`` so composing never fails because of it. + """ + size: tuple[int, int] | None = None + for source in sources: + try: + info = probe(source) + except Exception: + return None + width = info.get("width") if isinstance(info, dict) else None + height = info.get("height") if isinstance(info, dict) else None + if not isinstance(width, int) or not isinstance(height, int): + return None + if width <= 0 or height <= 0 or width % 2 or height % 2: + return None + if size is None: + size = (width, height) + elif size != (width, height): + return None + return size + + def compose_h3_project_video(project_name: Any, project_data: Any = None) -> Path: """Compose the selected H3 clips into a temporary video for downstream nodes.""" - from .video import merge_video_track_with_ffmpeg + from .video import ffprobe_info, merge_video_track_with_ffmpeg output_dir = Path(folder_paths.get_output_directory()).resolve() fresh_data = load_h3_project_data(project_name) @@ -1287,12 +1317,22 @@ def compose_h3_project_video(project_name: Any, project_data: Any = None) -> Pat if width <= 0 or height <= 0: raise ValueError("H3 project width and height must be greater than zero") + # Segment media is authored at the sampling resolution, which hidden + # half-res sampling makes smaller than the manifest resolution. Compose + # on the timeline's own size whenever every segment shares it, so the + # black canvas can never letterbox smaller segments into a corner; the + # combine node then restores the manifest resolution in pixel space. + compose_size = _uniform_probed_size( + [str(segment["source"]) for segment in timeline_segments], + ffprobe_info, + ) + temporary = merge_video_track_with_ffmpeg( timeline_segments, cursor, frame_rate, - width, - height, + compose_size[0] if compose_size else width, + compose_size[1] if compose_size else height, ) if temporary is None: raise RuntimeError("FFmpeg could not compose the H3 project video") diff --git a/utils/rtx_vsr.py b/utils/rtx_vsr.py index a785087..13b83f5 100644 --- a/utils/rtx_vsr.py +++ b/utils/rtx_vsr.py @@ -24,6 +24,7 @@ from typing import Any __all__ = [ "rtx_vsr_upscale", + "restore_project_video_resolution", "upscale_images_to_project_resolution", ] @@ -196,3 +197,61 @@ def upscale_images_to_project_resolution( f"{output_width}x{output_height}" ) return rtx_vsr_upscale(images, output_width, output_height) + + +def restore_project_video_resolution( + video: Any, + project_name: Any, + source_path: str | None = None, +) -> Any: + """Restore the manifest resolution on the composed project video. + + The combine composes the timeline at the segments' own size, which the + hidden half-res sampling contract makes smaller than the manifest. This + helper returns the video object untouched when the composed file already + matches the manifest (full-res renders, dual-pass, SelfLift, passthrough); + otherwise it decodes the frames once, upscales them to the exact manifest + resolution, and rebuilds the video so every combine output (VIDEO, + images, audio, fps) carries the editor-selected size. Any failure keeps + the original video - rendering must never break because of the restore. + """ + try: + target = _project_output_resolution(project_name) + if target is None: + return video + output_width, output_height = target + if source_path: + # Cheap ffprobe first: only pay the frame decode when the composed + # file is genuinely smaller than the project resolution. + from .video import ffprobe_info + + info = ffprobe_info(str(source_path)) + probed_width = info.get("width") if isinstance(info, dict) else None + probed_height = info.get("height") if isinstance(info, dict) else None + if ( + isinstance(probed_width, int) + and isinstance(probed_height, int) + and (probed_width, probed_height) == (output_width, output_height) + ): + return video + components = video.get_components() + images = upscale_images_to_project_resolution( + components.images, project_name + ) + if images is components.images: + return video + from comfy_api.latest import InputImpl, Types + + return InputImpl.VideoFromComponents( + Types.VideoComponents( + images=images, + audio=components.audio, + frame_rate=components.frame_rate, + ) + ) + except Exception as error: + _log( + f"Output restore skipped ({error}); the composed video keeps its " + "original resolution." + ) + return video