From d070044af0e905d98c8189d30c26c6852083431c 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 1/2] [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 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..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 From 46619b1bf197235b1de2eeb9ce0df3b4d72abf1a Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Fri, 27 Nov 2026 21:44:57 +0000 Subject: [PATCH 2/2] [tests] Assert detect() closes video stream on return and error --- tests/test_api.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index 6df461e2..e1b5d5d4 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -25,21 +25,38 @@ 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): +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()