From 90b90e3d502de51ee8fc7f898843e88800031940 Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:18:54 +0000 Subject: [PATCH] [bugfix] Close video stream in detect() via context manager Wrap open_video() in a with statement inside scenedetect.detect() so underlying file handles and decoders are deterministically closed on both normal completion and exceptions (preventing WinError 32 file locks on Windows). --- scenedetect/__init__.py | 26 +++++++++++++------------- tests/test_api.py | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/scenedetect/__init__.py b/scenedetect/__init__.py index c07e60c5..17041d0c 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..6df461e2 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -25,6 +25,24 @@ 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): + """Ensure `detect()` closes the opened video stream on normal return and on error.""" + import shutil + + from scenedetect import ContentDetector, detect + + normal_copy = tmp_path / "normal.mp4" + shutil.copyfile(test_video_file, normal_copy) + detect(normal_copy, ContentDetector(), end_time=10) + 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") + 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