-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenedetect_util.py
executable file
·129 lines (110 loc) · 3.85 KB
/
scenedetect_util.py
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
import logging
import os
from time import time
from dane.provenance import Provenance, obtain_software_versions
from models import OutputType, ScenedetectOutput, MediaFile
from scenedetect import ( # type: ignore
SceneManager,
open_video,
ContentDetector,
scene_manager,
)
logger = logging.getLogger(__name__)
class ScenedetectFailureException(Exception):
pass
def _get_keyframe_dir(output_dir: str) -> str:
return os.path.join(output_dir, OutputType.KEYFRAMES.value)
def _get_metadata_path(output_dir: str, kind: str) -> str:
if kind == "shot_boundaries":
return os.path.join(
output_dir,
OutputType.METADATA.value,
ScenedetectOutput.SHOT_BOUNDARIES.value,
)
if kind == "keyframes":
return os.path.join(
output_dir,
OutputType.METADATA.value,
ScenedetectOutput.KEYFRAME_TIMESTAMPS.value,
)
else:
raise Exception()
def run(
media_file: MediaFile,
output_dir: str,
extract_keyframes=False,
) -> Provenance:
logger.info(f"Running scenedetect on {media_file}")
start_time = time()
keyframe_dir = _get_keyframe_dir(output_dir)
try:
video = open_video(media_file.file_path)
except Exception:
logger.error(
f"Failed to run scenedetect on {media_file.file_path}: "
"Could not open video."
)
raise ScenedetectFailureException()
video_scene_manager = SceneManager()
video_scene_manager.add_detector(ContentDetector())
# Detect all scenes in video from current position to end.
video_scene_manager.detect_scenes(video)
# `get_scene_list` returns a list of start/end timecode pairs
# for each scene that was found.
scene_list = video_scene_manager.get_scene_list()
shot_boundaries_path = _get_metadata_path(
output_dir=output_dir, kind="shot_boundaries"
)
with open(shot_boundaries_path, "w") as f:
f.write(str(get_shot_boundaries(scene_list=scene_list)))
output_data = {"shot_boundaries": shot_boundaries_path}
if extract_keyframes:
logger.info("Also telling scenedetect to extract keyframes")
keyframe_dir = _get_keyframe_dir(output_dir)
image_paths = scene_manager.save_images(
scene_list=scene_list,
video=video,
num_images=1,
image_extension="jpg",
encoder_param=100,
output_dir=keyframe_dir,
image_name_template="$TIMESTAMP_MS",
)
output_data["keyframe_dir"] = keyframe_dir
keyframes_path = _get_metadata_path(output_dir=output_dir, kind="keyframes")
with open(keyframes_path, "w") as f:
f.write(str(get_keyframes_timestamps(image_paths)))
output_data["keyframe_timestamps"] = keyframes_path
return Provenance(
activity_name="Python Scenedetect",
activity_description="Shot detection & keyframe extraction",
start_time_unix=start_time,
processing_time_ms=(time() - start_time) * 1000,
software_version=obtain_software_versions(["scenedetect"]),
input_data={"input_file": media_file.file_path},
output_data=output_data,
)
def get_shot_boundaries(scene_list):
return [
tuple(int(scene[i].get_seconds() * 1000) for i in (0, 1))
for scene in scene_list
]
def get_keyframes_timestamps(image_paths):
return [
int(os.path.basename(filename).split(".")[0])
for v in image_paths.values()
for filename in v
]
if __name__ == "__main__":
media_file = MediaFile(
file_path=(
"data/input-files/1411058.1366653.WEEKNUMMER404"
"-HRE000042FF_924200_1089200.mp4"
),
source_id="source_id",
)
provenance = run(
media_file=media_file,
output_dir="tmp",
extract_keyframes=True,
)