Skip to content

Commit e55adcf

Browse files
committed
feat(encoder): Validate and resize frames to match initialized stream dimensions
warn if tensor is non contiguous
1 parent e6f4ccf commit e55adcf

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pytrickle/encoder.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ def custom_io_open(url: str, flags: int, options: dict):
221221
if not output_video_stream:
222222
# received video but no video output configured, so drop
223223
continue
224+
225+
# Store the initialized stream dimensions for validation
226+
stream_width = output_video_stream.codec_context.width
227+
stream_height = output_video_stream.codec_context.height
228+
224229
avframe.log_timestamps["frame_end"] = time.time()
225230
_log_frame_timestamps("Video", avframe.frame)
226231

@@ -242,6 +247,11 @@ def custom_io_open(url: str, flags: int, options: dict):
242247
# Explicitly specify RGB mode - decoder produces RGB, encoder expects RGB
243248
image = Image.fromarray(image_np, mode='RGB')
244249

250+
# Resize if frame dimensions don't match initialized stream
251+
if image.width != stream_width or image.height != stream_height:
252+
logger.debug(f"Resizing frame from {image.width}x{image.height} to {stream_width}x{stream_height}")
253+
image = image.resize((stream_width, stream_height), Image.LANCZOS)
254+
245255
frame = av.video.frame.VideoFrame.from_image(image)
246256
except Exception as e:
247257
logger.error(f"Error converting tensor to video frame: {e}, tensor shape: {tensor.shape}, dtype: {tensor.dtype}")

0 commit comments

Comments
 (0)