forked from Breakthrough/PySceneDetect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
102 lines (85 loc) · 4 KB
/
Copy pathtest_api.py
File metadata and controls
102 lines (85 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
#
# PySceneDetect: Python-Based Video Scene Detector
# ---------------------------------------------------------------
# [ Site: http://www.scenedetect.scenedetect.com/ ]
# [ Docs: http://manual.scenedetect.scenedetect.com/ ]
# [ Github: https://github.com/Breakthrough/PySceneDetect/ ]
#
# Copyright (C) 2014-2022 Brandon Castellano <http://www.bcastell.com>.
# PySceneDetect is licensed under the BSD 3-Clause License; see the
# included LICENSE file, or visit one of the above pages for details.
#
"""PySceneDetect API Tests
Demonstrates high-level usage of the PySceneDetect API. These tests are provided for
examples of common use cases, and only validate semantic correctness.
"""
from typing import List, Tuple
from scenedetect import detect, open_video
from scenedetect import ContentDetector, FrameTimecode, SceneManager, StatsManager
from scenedetect.backends import VideoStreamCv2
STATS_FILE_PATH = 'api_test_statsfile.csv'
def print_scenes(scene_list: List[Tuple[FrameTimecode, FrameTimecode]]):
"""Helper function to print a list of scenes to the terminal."""
print('Scene List:')
for i, scene in enumerate(scene_list):
print(' Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
i + 1,
scene[0].get_timecode(),
scene[0].get_frames(),
scene[1].get_timecode(),
scene[1].get_frames(),
))
def test_api_detect(test_video_file: str):
"""Demonstrate basic usage of the `detect` function to process a complete video."""
scene_list = detect(test_video_file, ContentDetector())
print_scenes(scene_list=scene_list)
def test_api_start_end_time(test_video_file: str):
"""Demonstrate processing a subsection of a video based on a starting/ending time."""
video = open_video(test_video_file)
scene_manager = SceneManager()
scene_manager.add_detector(ContentDetector())
# See test_api_timecode_types below for all supported timecode formats.
start_time = 20 # Start at frame (int) 20
end_time = 15.0 # End at 15 seconds (float)
video.seek(start_time)
# Can also specify `duration` instead of `end_time`.
scene_manager.detect_scenes(video=video, end_time=end_time)
scene_list = scene_manager.get_scene_list()
print_scenes(scene_list=scene_list)
def test_api_stats_manager(test_video_file: str):
"""Demonstrate using a StatsManager to save per-frame statistics to disk."""
video = open_video(test_video_file)
scene_manager = SceneManager(stats_manager=StatsManager())
scene_manager.add_detector(ContentDetector())
scene_manager.detect_scenes(video=video)
scene_list = scene_manager.get_scene_list()
print_scenes(scene_list=scene_list)
# Save per-frame statistics to disk.
scene_manager.stats_manager.save_to_csv(csv_file=STATS_FILE_PATH)
def test_api_video_stream_opencv(test_video_file: str):
"""Demonstrate constructing and using a VideoStream backend directly, instead of
using the `open_video` function. Only VideoStreamCv2 is guaranteed to be available.
Applications that do not require a specific backend library should use `open_video`.
"""
video = VideoStreamCv2(test_video_file)
scene_manager = SceneManager()
scene_manager.add_detector(ContentDetector())
scene_manager.detect_scenes(video=video)
scene_list = scene_manager.get_scene_list()
print_scenes(scene_list=scene_list)
def test_api_timecode_types():
"""Demonstrate all different types of timecodes that can be used."""
base_timecode = FrameTimecode(timecode=0, fps=10.0)
# Frames (int)
timecode = base_timecode + 1
assert timecode.get_frames() == 1
# Seconds (float)
timecode = base_timecode + 1.0
assert timecode.get_frames() == 10
# Timecode (str, 'HH:MM:SS' or 'HH:MM:SSS.nnn')
timecode = base_timecode + '00:00:01.500'
assert timecode.get_frames() == 15
# Seconds (str, 'SSSs' or 'SSSS.SSSs')
timecode = base_timecode + '1.5s'
assert timecode.get_frames() == 15