Skip to content

Commit eb01173

Browse files
authored
Fix VideoFromComponents.save_to crash when writing to BytesIO (Comfy-Org#12683)
* Fix VideoFromComponents.save_to crash when writing to BytesIO When `get_container_format()` or `get_stream_source()` is called on a tensor-based video (VideoFromComponents), it calls `save_to(BytesIO())`. Since BytesIO has no file extension, `av.open` can't infer the output format and throws `ValueError: Could not determine output format`. The sibling class `VideoFromFile` already handles this correctly via `get_open_write_kwargs()`, which detects BytesIO and sets the format explicitly. `VideoFromComponents` just never got the same treatment. This surfaces when any downstream node validates the container format of a tensor-based video, like TopazVideoEnhance or any node that calls `validate_container_format_is_mp4()`. Three-line fix in `comfy_api/latest/_input_impl/video_types.py`. * Add docstring to save_to to satisfy CI coverage check
1 parent ac6513e commit eb01173

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

comfy_api/latest/_input_impl/video_types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,18 @@ def save_to(
401401
codec: VideoCodec = VideoCodec.AUTO,
402402
metadata: Optional[dict] = None,
403403
):
404+
"""Save the video to a file path or BytesIO buffer."""
404405
if format != VideoContainer.AUTO and format != VideoContainer.MP4:
405406
raise ValueError("Only MP4 format is supported for now")
406407
if codec != VideoCodec.AUTO and codec != VideoCodec.H264:
407408
raise ValueError("Only H264 codec is supported for now")
408409
extra_kwargs = {}
409410
if isinstance(format, VideoContainer) and format != VideoContainer.AUTO:
410411
extra_kwargs["format"] = format.value
412+
elif isinstance(path, io.BytesIO):
413+
# BytesIO has no file extension, so av.open can't infer the format.
414+
# Default to mp4 since that's the only supported format anyway.
415+
extra_kwargs["format"] = "mp4"
411416
with av.open(path, mode='w', options={'movflags': 'use_metadata_tags'}, **extra_kwargs) as output:
412417
# Add metadata before writing any streams
413418
if metadata is not None:

0 commit comments

Comments
 (0)