Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
PySceneDetect/tests/test_api.py at main · Breakthrough/PySceneDetect · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Breakthrough
PySceneDetect
Repository navigation
Code
Issues
58
(58)
Pull requests
5
(5)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
PySceneDetect
/
tests
/
test_api.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
187 lines (145 loc) · 8.02 KB
main
Breadcrumbs
PySceneDetect
/
tests
/
test_api.py
Copy path
Top
File metadata and controls
Code
Blame
187 lines (145 loc) · 8.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#
# PySceneDetect: Python-Based Video Scene Detector
# -------------------------------------------------------------------
# [ Site: https://scenedetect.com ]
# [ Docs: https://scenedetect.com/docs/ ]
# [ Github: https://github.com/Breakthrough/PySceneDetect/ ]
#
# Copyright (C) 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
These tests demonstrate common workflow patterns used when integrating the PySceneDetect API."""
import
pytest
def
test_api_detect
(
test_video_file
:
str
):
"""Demonstrate usage of the `detect()` function to process a complete video."""
from
scenedetect
import
ContentDetector
,
detect
scene_list
=
detect
(
test_video_file
,
ContentDetector
())
for
i
,
scene
in
enumerate
(
scene_list
):
print
(
f"Scene
{
i
+
1
}
:
{
scene
[
0
].
get_timecode
()
}
-
{
scene
[
1
].
get_timecode
()
}
"
)
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
# Times can be seconds (float), frames (int), or timecode 'HH:MM:SSS.nnn' (str).
# See test_api_timecode_types() for examples of each format.
scene_list
=
detect
(
test_video_file
,
ContentDetector
(),
start_time
=
10.5
,
end_time
=
15.9
)
for
i
,
scene
in
enumerate
(
scene_list
):
print
(
f"Scene
{
i
+
1
}
:
{
scene
[
0
].
get_timecode
()
}
-
{
scene
[
1
].
get_timecode
()
}
"
)
def
test_api_detect_stats
(
test_video_file
:
str
):
"""Demonstrate usage of the `detect()` function to generate a statsfile."""
from
scenedetect
import
ContentDetector
,
detect
detect
(
test_video_file
,
ContentDetector
(),
stats_file_path
=
"frame_metrics.csv"
)
def
test_api_scene_manager
(
test_video_file
:
str
):
"""Demonstrate how to use a SceneManager to implement a function similar to `detect()`."""
from
scenedetect
import
ContentDetector
,
SceneManager
,
open_video
video
=
open_video
(
test_video_file
)
scene_manager
=
SceneManager
()
scene_manager
.
add_detector
(
ContentDetector
())
scene_manager
.
detect_scenes
(
video
=
video
)
scene_list
=
scene_manager
.
get_scene_list
()
for
i
,
scene
in
enumerate
(
scene_list
):
print
(
f"Scene
{
i
+
1
}
:
{
scene
[
0
].
get_timecode
()
}
-
{
scene
[
1
].
get_timecode
()
}
"
)
def
test_api_scene_manager_start_end_time
(
test_video_file
:
str
):
"""Demonstrate how to use a SceneManager to process a subset of the input video."""
from
scenedetect
import
ContentDetector
,
SceneManager
,
open_video
video
=
open_video
(
test_video_file
)
scene_manager
=
SceneManager
()
scene_manager
.
add_detector
(
ContentDetector
())
# Times can be seconds (float), frames (int), or timecode 'HH:MM:SSS.nnn' (str).
# See test_api_timecode_types() for examples of each format.
start_time
=
200
# Start at frame (int) 200
end_time
=
15.0
# End at 15 seconds (float)
video
.
seek
(
start_time
)
scene_manager
.
detect_scenes
(
video
=
video
,
end_time
=
end_time
)
scene_list
=
scene_manager
.
get_scene_list
()
for
i
,
scene
in
enumerate
(
scene_list
):
print
(
f"Scene
{
i
+
1
}
:
{
scene
[
0
].
get_timecode
()
}
-
{
scene
[
1
].
get_timecode
()
}
"
)
def
test_api_open_video_framerate_legacy_alias
(
test_video_file
:
str
):
"""`open_video(framerate=...)` is the deprecated alias for `frame_rate=` (issue #548).
Both forms must produce equivalent streams; when both are provided, `frame_rate` wins."""
from
scenedetect
import
open_video
with
pytest
.
warns
(
DeprecationWarning
,
match
=
"frame_rate"
):
legacy
=
open_video
(
test_video_file
,
framerate
=
30.0
)
canonical
=
open_video
(
test_video_file
,
frame_rate
=
30.0
)
assert
legacy
.
frame_rate
==
canonical
.
frame_rate
# `frame_rate` takes precedence over `framerate` when both are provided.
with
pytest
.
warns
(
DeprecationWarning
,
match
=
"frame_rate"
):
both
=
open_video
(
test_video_file
,
frame_rate
=
30.0
,
framerate
=
24.0
)
assert
both
.
frame_rate
==
canonical
.
frame_rate
def
test_api_timecode_types
():
"""Demonstrate all different types of timecodes that can be used."""
from
scenedetect
import
FrameTimecode
base_timecode
=
FrameTimecode
(
timecode
=
0
,
fps
=
10.0
)
# Frames (int)
timecode
=
base_timecode
+
1
assert
timecode
.
frame_num
==
1
# Seconds (float)
timecode
=
base_timecode
+
1.0
assert
timecode
.
frame_num
==
10
# Timecode (str, 'HH:MM:SS' or 'HH:MM:SSS.nnn')
timecode
=
base_timecode
+
"00:00:01.500"
assert
timecode
.
frame_num
==
15
# Seconds (str, 'SSSs' or 'SSSS.SSSs')
timecode
=
base_timecode
+
"1.5s"
assert
timecode
.
frame_num
==
15
def
test_api_stats_manager
(
test_video_file
:
str
):
"""Demonstrate using a StatsManager to save per-frame statistics to disk."""
from
scenedetect
import
ContentDetector
,
SceneManager
,
StatsManager
,
open_video
video
=
open_video
(
test_video_file
)
scene_manager
=
SceneManager
(
stats_manager
=
StatsManager
())
scene_manager
.
add_detector
(
ContentDetector
())
scene_manager
.
detect_scenes
(
video
=
video
)
# Save per-frame statistics to disk.
filename
=
f"
{
test_video_file
}
.stats.csv"
assert
scene_manager
.
stats_manager
is
not
None
scene_manager
.
stats_manager
.
save_to_csv
(
csv_file
=
filename
)
def
test_api_scene_manager_callback
(
test_video_file
:
str
):
"""Demonstrate how to use a callback with the SceneManager detect_scenes method."""
import
numpy
from
scenedetect
import
ContentDetector
,
FrameTimecode
,
SceneManager
,
open_video
# Callback to invoke on the first frame of every new scene detection.
def
on_new_scene
(
frame_img
:
numpy
.
ndarray
,
position
:
FrameTimecode
):
print
(
f"New scene found at frame
{
position
.
frame_num
}
."
)
video
=
open_video
(
test_video_file
)
scene_manager
=
SceneManager
()
scene_manager
.
add_detector
(
ContentDetector
())
scene_manager
.
detect_scenes
(
video
=
video
,
callback
=
on_new_scene
)
def
test_api_device_callback
(
test_video_file
:
str
):
"""Demonstrate how to use a webcam/device/pipe and a callback function.
Instead of calling `open_video()`, an existing `cv2.VideoCapture` can be used by
wrapping it with a `VideoCaptureAdapter.`"""
import
cv2
import
numpy
from
scenedetect
import
ContentDetector
,
FrameTimecode
,
SceneManager
,
VideoCaptureAdapter
# Callback to invoke on the first frame of every new scene detection.
def
on_new_scene
(
frame_img
:
numpy
.
ndarray
,
position
:
FrameTimecode
):
print
(
f"New scene found at frame
{
position
.
frame_num
}
."
)
# We open a file just for test purposes, but we can also use a device or pipe here.
cap
=
cv2
.
VideoCapture
(
test_video_file
)
video
=
VideoCaptureAdapter
(
cap
)
# Now `video` can be used as normal with a `SceneManager`. If the input is non-terminating,
# either set `end_time/duration` when calling `detect_scenes`, or call `scene_manager.stop()`.
total_frames
=
1000
scene_manager
=
SceneManager
()
scene_manager
.
add_detector
(
ContentDetector
())
scene_manager
.
detect_scenes
(
video
=
video
,
duration
=
total_frames
,
callback
=
on_new_scene
)
# TODO(v0.8): Remove this test when these deprecated modules are removed from the codebase.
def
test_deprecated_modules_emits_warning_on_import
():
import
importlib
import
pytest
SCENE_DETECTOR_WARNING
=
(
"The `scene_detector` submodule is deprecated, import from the base package instead."
)
with
pytest
.
warns
(
DeprecationWarning
,
match
=
SCENE_DETECTOR_WARNING
):
importlib
.
import_module
(
"scenedetect.scene_detector"
)
FRAME_TIMECODE_WARNING
=
(
"The `frame_timecode` submodule is deprecated, import from the base package instead."
)
with
pytest
.
warns
(
DeprecationWarning
,
match
=
FRAME_TIMECODE_WARNING
):
importlib
.
import_module
(
"scenedetect.frame_timecode"
)
VIDEO_SPLITTER_WARNING
=
(
"The `video_splitter` submodule is deprecated, import from the base package instead."
)
with
pytest
.
warns
(
DeprecationWarning
,
match
=
VIDEO_SPLITTER_WARNING
):
importlib
.
import_module
(
"scenedetect.video_splitter"
)
You can’t perform that action at this time.