From 7cf960ce4145fbebfe44cf24ef3ff7806d70a0b4 Mon Sep 17 00:00:00 2001 From: goranai Date: Wed, 25 Nov 2026 11:11:17 +0800 Subject: [PATCH 1/2] [bugfix] Check crop bounds as inclusive in detect_scenes (#623) --- scenedetect/scene_manager.py | 7 ++++--- tests/test_scene_manager.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/scenedetect/scene_manager.py b/scenedetect/scene_manager.py index aaec6a2d..5ddbcd93 100644 --- a/scenedetect/scene_manager.py +++ b/scenedetect/scene_manager.py @@ -508,7 +508,8 @@ def detect_scenes( logger.debug( f"Crop set: top left = {self._crop[0:2]}, bottom right = {self._crop[2:4]}" ) - x0, y0, x1, y1 = self._crop + # Use the public (inclusive) coordinates, `_crop` is stored one-past-the-end. + x0, y0, x1, y1 = self.crop min_x, min_y = (min(x0, x1), min(y0, y1)) max_x, max_y = (max(x0, x1), max(y0, y1)) frame_width, frame_height = video.frame_size @@ -517,8 +518,8 @@ def detect_scenes( if max_x >= frame_width or max_y >= frame_height: logger.warning("Warning: crop ends outside of video boundary.") effective_frame_size = ( - 1 + min(max_x, frame_width) - min_x, - 1 + min(max_y, frame_height) - min_y, + 1 + min(max_x, frame_width - 1) - min_x, + 1 + min(max_y, frame_height - 1) - min_y, ) # Calculate downscale factor and log effective resolution. if self.auto_downscale: diff --git a/tests/test_scene_manager.py b/tests/test_scene_manager.py index b5388e7a..4f01bd4b 100644 --- a/tests/test_scene_manager.py +++ b/tests/test_scene_manager.py @@ -261,3 +261,38 @@ def test_expand_scenes_to_bounds_does_not_mutate_input(): expand_scenes_to_bounds(scenes, start=t0, end=t300) assert scenes == original + + +def _write_blank_video(path: str, width: int, height: int, num_frames: int = 3) -> None: + import cv2 + import numpy as np + + writer = cv2.VideoWriter(path, cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (width, height)) + for _ in range(num_frames): + writer.write(np.zeros((height, width, 3), dtype=np.uint8)) + writer.release() + + +@pytest.mark.parametrize( + "crop,warns", + [ + # Bounds are inclusive, so these all lie inside of a 64x48 frame. + ((0, 0, 63, 47), False), + ((0, 24, 63, 47), False), + ((10, 10, 20, 20), False), + # One pixel past the right or bottom edge. + ((0, 0, 64, 47), True), + ((0, 0, 63, 48), True), + ], +) +def test_crop_boundary_warning(tmp_path, caplog, crop, warns): + """The warning about a crop ending outside of the video must use the inclusive bounds.""" + path = str(tmp_path / "blank.avi") + _write_blank_video(path, 64, 48) + video = VideoStreamCv2(path) + sm = SceneManager() + sm.crop = crop + sm.add_detector(ContentDetector()) + with caplog.at_level("WARNING", logger="pyscenedetect"): + sm.detect_scenes(video=video) + assert any("crop ends outside" in r.getMessage() for r in caplog.records) == warns From 87a450cf4759afc2bda0753270dd9955a22cfa6b Mon Sep 17 00:00:00 2001 From: goranai Date: Fri, 27 Nov 2026 21:33:41 -0500 Subject: [PATCH 2/2] [bugfix] Fix pyright errors in the crop bounds check and test (#623) --- scenedetect/scene_manager.py | 5 +++-- tests/test_scene_manager.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scenedetect/scene_manager.py b/scenedetect/scene_manager.py index 5ddbcd93..b8bf43a2 100644 --- a/scenedetect/scene_manager.py +++ b/scenedetect/scene_manager.py @@ -508,8 +508,9 @@ def detect_scenes( logger.debug( f"Crop set: top left = {self._crop[0:2]}, bottom right = {self._crop[2:4]}" ) - # Use the public (inclusive) coordinates, `_crop` is stored one-past-the-end. - x0, y0, x1, y1 = self.crop + # `_crop` is stored one-past-the-end, so step back to the inclusive coordinates. + x0, y0, x1, y1 = self._crop + x1, y1 = x1 - 1, y1 - 1 min_x, min_y = (min(x0, x1), min(y0, y1)) max_x, max_y = (max(x0, x1), max(y0, y1)) frame_width, frame_height = video.frame_size diff --git a/tests/test_scene_manager.py b/tests/test_scene_manager.py index 4f01bd4b..ef390046 100644 --- a/tests/test_scene_manager.py +++ b/tests/test_scene_manager.py @@ -267,7 +267,7 @@ def _write_blank_video(path: str, width: int, height: int, num_frames: int = 3) import cv2 import numpy as np - writer = cv2.VideoWriter(path, cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (width, height)) + writer = cv2.VideoWriter(path, cv2.VideoWriter.fourcc(*"MJPG"), 10.0, (width, height)) for _ in range(num_frames): writer.write(np.zeros((height, width, 3), dtype=np.uint8)) writer.release()