From a77158e92f7070af662beb49e501f6efeebeed63 Mon Sep 17 00:00:00 2001 From: ahfoysal Date: Mon, 6 Jul 2026 12:58:22 +0600 Subject: [PATCH] fix: keep video movflags scoped to container --- comfy_api/latest/_input_impl/video_types.py | 12 +++------ tests-unit/comfy_api_test/video_types_test.py | 27 +++++++++++++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/comfy_api/latest/_input_impl/video_types.py b/comfy_api/latest/_input_impl/video_types.py index 6c69256ab..faac63346 100644 --- a/comfy_api/latest/_input_impl/video_types.py +++ b/comfy_api/latest/_input_impl/video_types.py @@ -37,7 +37,7 @@ def get_open_write_kwargs( open_kwargs = { "mode": "w", # If isobmff, preserve custom metadata tags (workflow, prompt, extra_pnginfo) - "options": {"movflags": "use_metadata_tags"}, + "container_options": {"movflags": "use_metadata_tags"}, } is_write_to_buffer = isinstance(dest, io.BytesIO) @@ -525,14 +525,8 @@ class VideoFromComponents(VideoInput): if bit_depth is None: bit_depth = self.__bit_depth is_10bit = bit_depth >= 10 - extra_kwargs = {} - if isinstance(format, VideoContainer) and format != VideoContainer.AUTO: - extra_kwargs["format"] = format.value - elif isinstance(path, io.BytesIO): - # BytesIO has no file extension, so av.open can't infer the format. - # Default to mp4 since that's the only supported format anyway. - extra_kwargs["format"] = "mp4" - with av.open(path, mode='w', options={'movflags': 'use_metadata_tags'}, **extra_kwargs) as output: + open_kwargs = get_open_write_kwargs(path, "mp4", format) + with av.open(path, **open_kwargs) as output: # Add metadata before writing any streams if metadata is not None: for key, value in metadata.items(): diff --git a/tests-unit/comfy_api_test/video_types_test.py b/tests-unit/comfy_api_test/video_types_test.py index b25fcb1ca..48d128f92 100644 --- a/tests-unit/comfy_api_test/video_types_test.py +++ b/tests-unit/comfy_api_test/video_types_test.py @@ -5,8 +5,8 @@ import os import av import io from fractions import Fraction -from comfy_api.input_impl.video_types import VideoFromFile, VideoFromComponents -from comfy_api.util.video_types import VideoComponents +from comfy_api.input_impl.video_types import VideoFromFile, VideoFromComponents, get_open_write_kwargs +from comfy_api.util.video_types import VideoComponents, VideoContainer from comfy_api.input.basic_types import AudioInput from av.error import InvalidDataError @@ -237,3 +237,26 @@ def test_duration_consistency(video_components): manual_duration = float(components.images.shape[0] / components.frame_rate) assert duration == pytest.approx(manual_duration) + + +def test_open_write_kwargs_keep_movflags_container_scoped(): + """movflags must not leak into stream codec options such as AAC.""" + kwargs = get_open_write_kwargs("out.mp4", "mp4", VideoContainer.AUTO) + + assert kwargs["container_options"] == {"movflags": "use_metadata_tags"} + assert "options" not in kwargs + + +def test_open_write_kwargs_sets_buffer_format(): + """BytesIO outputs still get an explicit container format.""" + kwargs = get_open_write_kwargs(io.BytesIO(), "mov,mp4,m4a,3gp,3g2,mj2", VideoContainer.AUTO) + + assert kwargs["format"] == "mov" + assert kwargs["container_options"] == {"movflags": "use_metadata_tags"} + assert "options" not in kwargs + + +def test_open_write_kwargs_honors_explicit_container_format(): + kwargs = get_open_write_kwargs(io.BytesIO(), "mov,mp4,m4a,3gp,3g2,mj2", VideoContainer.MP4) + + assert kwargs["format"] == "mp4"