@@ -111,6 +111,9 @@ def on_new_scene(frame_img: numpy.ndarray, frame_num: int):
111
111
MAX_FRAME_QUEUE_LENGTH : int = 4
112
112
"""Maximum number of decoded frames which can be buffered while waiting to be processed."""
113
113
114
+ MAX_FRAME_SIZE_ERRORS : int = 16
115
+ """Maximum number of frame size error messages that can be logged."""
116
+
114
117
PROGRESS_BAR_DESCRIPTION = ' Detected: %d | Progress'
115
118
"""Template to use for progress bar."""
116
119
@@ -578,6 +581,9 @@ def __init__(
578
581
self ._start_pos : FrameTimecode = None
579
582
# Position of video on the last frame processed by detect_scenes.
580
583
self ._last_pos : FrameTimecode = None
584
+ # Size of the decoded frames.
585
+ self ._frame_size : Tuple [int , int ] = None
586
+ self ._frame_size_errors : int = 0
581
587
self ._base_timecode : Optional [FrameTimecode ] = None
582
588
self ._downscale : int = 1
583
589
self ._auto_downscale : bool = True
@@ -677,6 +683,7 @@ def clear(self) -> None:
677
683
self ._event_list .clear ()
678
684
self ._last_pos = None
679
685
self ._start_pos = None
686
+ self ._frame_size = None
680
687
self .clear_detectors ()
681
688
682
689
def clear_detectors (self ) -> None :
@@ -923,6 +930,28 @@ def _decode_thread(
923
930
frame_im = video .read ()
924
931
if frame_im is False :
925
932
break
933
+ # Verify the decoded frame size against the video container's reported
934
+ # resolution, and also verify that consecutive frames have the correct size.
935
+ decoded_size = (frame_im .shape [1 ], frame_im .shape [0 ])
936
+ if self ._frame_size is None :
937
+ self ._frame_size = decoded_size
938
+ if video .frame_size != decoded_size :
939
+ logger .warn (
940
+ f"WARNING: Decoded frame size ({ decoded_size } ) does not match "
941
+ f" video resolution { video .frame_size } , possible corrupt input." )
942
+ elif self ._frame_size != decoded_size :
943
+ self ._frame_size_errors += 1
944
+ if self ._frame_size_errors <= MAX_FRAME_SIZE_ERRORS :
945
+ logger .error (
946
+ f"ERROR: Frame at { str (video .position )} has incorrect size and "
947
+ f"cannot be processed: decoded size = { decoded_size } , "
948
+ f"expected = { self ._frame_size } . Video may be corrupt." )
949
+ if self ._frame_size_errors == MAX_FRAME_SIZE_ERRORS :
950
+ logger .warn (
951
+ f"WARNING: Too many errors emitted, skipping future messages." )
952
+ # Skip processing frames that have an incorrect size.
953
+ continue
954
+
926
955
if downscale_factor > 1 :
927
956
frame_im = cv2 .resize (
928
957
frame_im , (round (frame_im .shape [1 ] / downscale_factor ),
0 commit comments