diff --git a/scenedetect/scene_manager.py b/scenedetect/scene_manager.py index aaec6a2d..b8bf43a2 100644 --- a/scenedetect/scene_manager.py +++ b/scenedetect/scene_manager.py @@ -508,7 +508,9 @@ def detect_scenes( logger.debug( f"Crop set: top left = {self._crop[0:2]}, bottom right = {self._crop[2:4]}" ) + # `_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 @@ -517,8 +519,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..ef390046 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