Comfy-Org/ComfyUI · error · ValueError

Video resolution changes mid-stream ({source_size[0]}x{sourc

Error message

Video resolution changes mid-stream ({source_size[0]}x{source_size[1]} -> {frame.width}x{frame.height}); cannot transcode

What it means

The transcode writer configures the output stream once from the first frame (source_size) and then asserts every subsequent frame matches. Streams whose resolution changes mid-way (some webm/avi recordings, concatenated files, broken encodes) cannot be re-muxed into a single fixed-geometry H.264 stream, and silent rescaling would corrupt output, so it raises.

Source

Thrown at comfy_api/latest/_input_impl/video_types.py:678

                            output = av.open(path, **open_kwargs)
                            # Add metadata before writing any streams
                            write_output_metadata(container, output, metadata)
                            out_video = output.add_stream("h264", rate=rate)
                            # no B-frames: reordering makes mp4 sample durations follow decode order,
                            # so irregular-VFR spans and trim windows land wrong
                            out_video.codec_context.max_b_frames = 0
                            out_video.width = out_width
                            out_video.height = out_height
                            out_video.pix_fmt = pix_fmt
                            if crf is not None:
                                out_video.options = {"crf": str(crf)}
                            # source pts pass through (rebased to 0), so variable frame rate survives
                            out_video.codec_context.time_base = video_stream.time_base
                            if audio_stream is not None:
                                out_audio = output.add_stream("aac", rate=sample_rate, layout=layout)
                        if (frame.width, frame.height) != source_size:
                            # encoding would silently rescale the new geometry into the old one
                            raise ValueError(
                                f"Video resolution changes mid-stream "
                                f"({source_size[0]}x{source_size[1]} -> {frame.width}x{frame.height}); cannot transcode"
                            )
                        if rotation_k:
                            if rotation_filter is None:
                                g = av.filter.Graph()
                                g_src = g.add_buffer(width=frame.width, height=frame.height,
                                                     format=frame.format.name, time_base=video_stream.time_base)
                                tail = g_src
                                for filter_name, filter_args in {1: [("transpose", "cclock")],
                                                                 2: [("hflip", None), ("vflip", None)],
                                                                 3: [("transpose", "clock")]}[rotation_k]:
                                    step = g.add(filter_name, filter_args)
                                    tail.link_to(step)
                                    tail = step
                                g_sink = g.add("buffersink")
                                tail.link_to(g_sink)
                                g.configure()

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Split the file at the resolution change and transcode each segment separately
  2. Re-encode with scale to a fixed size first: ffmpeg -i in.mp4 -vf scale=1280:720 fixed.mp4, then use that as input
  3. Drop the offending segment if it is a corrupt trailing frame (trim before the change point)

Example fix

// before
video_input.save_transcoded('out.mp4')  # resolution changes at frame 500

# after
# normalize: ffmpeg -i in.mp4 -vf scale=1280:720:force_original_aspect_ratio=1,pad=1280:720 fixed.mp4
VideoFromFile('fixed.mp4').save_transcoded('out.mp4')
Defensive patterns

Strategy: validation

Validate before calling

# pre-check with ffprobe that frame sizes are constant
# ffprobe -select_streams v -show_entries frame=width,height -of csv in.mp4 | sort -u

Try / catch

try:
    video_input.save_transcoded(out)
except ValueError as e:
    if 'resolution changes mid-stream' in str(e):
        # split at change point or scale-normalize then retry
        raise

Prevention

When it happens

Trigger: Calling the transcode path on a container whose frames change resolution after the first frame — e.g. concatenated videos of different sizes, or a variable-resolution screen capture.

Common situations: Users concatenate clips with ffmpeg without normalizing size; screen recorders that resize mid-recording; corrupted files with a stray frame of different geometry.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/132d340b8f7f2c5e. Report an issue: GitHub.