From 0668240bda4d636b7f294da8a0b41af656653b43 Mon Sep 17 00:00:00 2001 From: tjandra Date: Sat, 3 Jan 2026 21:45:10 +0700 Subject: [PATCH 1/2] Resize frame to avoid "Unequal size tensor" error --- nunif/utils/video.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/nunif/utils/video.py b/nunif/utils/video.py index 95b60103..7ea95731 100644 --- a/nunif/utils/video.py +++ b/nunif/utils/video.py @@ -932,6 +932,7 @@ def process_video(input_path, output_path, container_duration=container_duration, input_path=input_path) pbar = tqdm_fn(desc=desc, total=total, ncols=ncols) + frame_dim = None streams = [s for s in [video_input_stream, audio_input_stream] if s is not None] demuxer = input_container.demux(streams) while True: @@ -944,6 +945,11 @@ def process_video(input_path, output_path, for frame in safe_decode(packet): frame = fps_filter.update(frame) if frame is not None: + if frame_dim is None: + frame_dim = [frame.width, frame.height] + elif frame.width != frame_dim[0] or frame.height != frame_dim[1]: # video has inconsistent size! + print("\n[WARN] Converting", [frame.width, frame.height], "to", frame_dim, "!", file=sys.stderr) + frame = frame.reformat(width=frame_dim[0], height=frame_dim[1]) frame = frame.reformat(**rgb24_options) if rgb24_options else frame for new_frame in get_new_frames(frame_callback(frame)): reformatted_frame = reformatter(new_frame) @@ -979,6 +985,11 @@ def process_video(input_path, output_path, while True: frame = fps_filter.update(None) if frame is not None: + if frame_dim is None: + frame_dim = [frame.width, frame.height] + elif frame.width != frame_dim[0] or frame.height != frame_dim[1]: # video has inconsistent size! + print("\n[WARN] Converting", [frame.width, frame.height], "to", frame_dim, "!", file=sys.stderr) + frame = frame.reformat(width=frame_dim[0], height=frame_dim[1]) frame = frame.reformat(**rgb24_options) if rgb24_options else frame for new_frame in get_new_frames(frame_callback(frame)): new_frame = reformatter(new_frame) From 9dfccc0e8b63563f164e2bd2741f0de902959486 Mon Sep 17 00:00:00 2001 From: tjandra Date: Sun, 4 Jan 2026 04:43:31 +0700 Subject: [PATCH 2/2] change initial frame_dim to codec_context instead of first valid frame size --- nunif/utils/video.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/nunif/utils/video.py b/nunif/utils/video.py index 7ea95731..c0a7ed32 100644 --- a/nunif/utils/video.py +++ b/nunif/utils/video.py @@ -866,6 +866,7 @@ def process_video(input_path, output_path, input_container.seek(start_time * av.time_base, backward=True, any_frame=False) video_input_stream = input_container.streams.video[0] + frame_dim = [video_input_stream.codec_context.width, video_input_stream.codec_context.height] video_input_stream.thread_type = "AUTO" # _print_len(video_input_stream) audio_input_stream = audio_output_stream = None @@ -932,7 +933,6 @@ def process_video(input_path, output_path, container_duration=container_duration, input_path=input_path) pbar = tqdm_fn(desc=desc, total=total, ncols=ncols) - frame_dim = None streams = [s for s in [video_input_stream, audio_input_stream] if s is not None] demuxer = input_container.demux(streams) while True: @@ -945,9 +945,7 @@ def process_video(input_path, output_path, for frame in safe_decode(packet): frame = fps_filter.update(frame) if frame is not None: - if frame_dim is None: - frame_dim = [frame.width, frame.height] - elif frame.width != frame_dim[0] or frame.height != frame_dim[1]: # video has inconsistent size! + if frame.width != frame_dim[0] or frame.height != frame_dim[1]: # video has inconsistent size! print("\n[WARN] Converting", [frame.width, frame.height], "to", frame_dim, "!", file=sys.stderr) frame = frame.reformat(width=frame_dim[0], height=frame_dim[1]) frame = frame.reformat(**rgb24_options) if rgb24_options else frame @@ -985,9 +983,7 @@ def process_video(input_path, output_path, while True: frame = fps_filter.update(None) if frame is not None: - if frame_dim is None: - frame_dim = [frame.width, frame.height] - elif frame.width != frame_dim[0] or frame.height != frame_dim[1]: # video has inconsistent size! + if frame.width != frame_dim[0] or frame.height != frame_dim[1]: # video has inconsistent size! print("\n[WARN] Converting", [frame.width, frame.height], "to", frame_dim, "!", file=sys.stderr) frame = frame.reformat(width=frame_dim[0], height=frame_dim[1]) frame = frame.reformat(**rgb24_options) if rgb24_options else frame