diff --git a/scenedetect/__init__.py b/scenedetect/__init__.py index 91656e41..83064851 100644 --- a/scenedetect/__init__.py +++ b/scenedetect/__init__.py @@ -201,19 +201,19 @@ def detect( ValueError: `start_time` or `end_time` are incorrectly formatted. TypeError: `start_time` or `end_time` are invalid types. """ - video = open_video(video_path, backend=backend) - if start_time is not None: - video.seek(FrameTimecode(start_time, video.frame_rate)) - end_timecode = FrameTimecode(end_time, video.frame_rate) if end_time is not None else None - # To reduce memory consumption when not required, we only add a StatsManager if we - # need to save frame metrics to disk. - scene_manager = SceneManager(StatsManager() if stats_file_path else None) - scene_manager.add_detector(detector) - scene_manager.detect_scenes( - video=video, - show_progress=show_progress, - end_time=end_timecode, - ) + with open_video(video_path, backend=backend) as video: + if start_time is not None: + video.seek(FrameTimecode(start_time, video.frame_rate)) + end_timecode = FrameTimecode(end_time, video.frame_rate) if end_time is not None else None + # To reduce memory consumption when not required, we only add a StatsManager if we + # need to save frame metrics to disk. + scene_manager = SceneManager(StatsManager() if stats_file_path else None) + scene_manager.add_detector(detector) + scene_manager.detect_scenes( + video=video, + show_progress=show_progress, + end_time=end_timecode, + ) if scene_manager.stats_manager is not None and stats_file_path is not None: scene_manager.stats_manager.save_to_csv(csv_file=stats_file_path) return scene_manager.get_scene_list(start_in_scene=start_in_scene) diff --git a/tests/test_api.py b/tests/test_api.py index 5bde6cfd..e1b5d5d4 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -25,6 +25,41 @@ def test_api_detect(test_video_file: str): print(f"Scene {i + 1}: {scene[0].get_timecode()} - {scene[1].get_timecode()}") +def test_api_detect_closes_stream_on_return_and_error(test_video_file: str, tmp_path, monkeypatch): + """Ensure `detect()` closes the opened video stream on normal return and on error.""" + import shutil + from unittest.mock import MagicMock + + import scenedetect + from scenedetect import ContentDetector, detect + + real_open_video = scenedetect.open_video + opened_streams = [] + + def tracking_open_video(*args, **kwargs): + stream = real_open_video(*args, **kwargs) + stream.close = MagicMock(wraps=stream.close) + opened_streams.append(stream) + return stream + + monkeypatch.setattr(scenedetect, "open_video", tracking_open_video) + + normal_copy = tmp_path / "normal.mp4" + shutil.copyfile(test_video_file, normal_copy) + detect(normal_copy, ContentDetector(), end_time=10) + assert len(opened_streams) == 1 + opened_streams[0].close.assert_called_once() + normal_copy.unlink() + + error_copy = tmp_path / "error.mp4" + shutil.copyfile(test_video_file, error_copy) + with pytest.raises(ValueError): + detect(error_copy, ContentDetector(), start_time="invalid_timecode") + assert len(opened_streams) == 2 + opened_streams[1].close.assert_called_once() + error_copy.unlink() + + def test_api_detect_start_end_time(test_video_file: str): """Demonstrate usage of the `detect()` function to process a subset of a video.""" from scenedetect import ContentDetector, detect