Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions scenedetect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 18 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading