-
Notifications
You must be signed in to change notification settings - Fork 19
/
overview.py
153 lines (121 loc) · 4.33 KB
/
overview.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import math
import os
from pathlib import Path
import shutil
import subprocess
import time
from utils import VideoInfoProvider, line, exit_program, Logger
log = Logger("overview")
class ClipError(Exception):
pass
class ConcatenateError(Exception):
pass
def clip_number_to_movie_timestamp(clip_number_seconds):
time_from_clip_number = time.gmtime(clip_number_seconds)
timestamp = time.strftime("%H:%M:%S", time_from_clip_number)
return timestamp
def create_clips(input_video, output_folder, interval_seconds, clip_length):
# The output folder for the clips.
output_folder = os.path.join(output_folder, "clips")
os.makedirs(output_folder, exist_ok=True)
provider = VideoInfoProvider(input_video)
duration = int(float(provider.get_duration()))
if interval_seconds > duration:
raise ClipError(
f"The interval ({interval_seconds}s) may not be longer than the video ({duration}s)."
)
num_clips = math.trunc(duration / interval_seconds)
txt_file_path = os.path.join(output_folder, "clips.txt")
# Create the file.
open(txt_file_path, "w").close()
log.info("Overview mode activated.")
log.info(
f"Creating a {clip_length} second clip every {interval_seconds} seconds from {input_video}..."
)
line()
try:
for clip_number in range(1, num_clips):
clip_name = f"clip{clip_number}.mkv"
with open(txt_file_path, "a") as f:
f.write(f"file '{clip_name}'\n")
clip_output_path = os.path.join(output_folder, clip_name)
clip_offset = clip_number_to_movie_timestamp(clip_number * interval_seconds)
log.info(
f"Creating clip {clip_number} which starts at {clip_offset} and ends {clip_length} seconds later ..."
)
subprocess_cut_args = [
"ffmpeg",
"-loglevel",
"warning",
"-stats",
"-y",
"-ss",
clip_offset,
"-i",
input_video,
"-map",
"0:V",
"-t",
clip_length,
"-c:v",
"libx264",
"-crf",
"0",
"-preset",
"ultrafast",
clip_output_path,
]
subprocess.run(subprocess_cut_args)
except Exception as error:
log.info("An error occurred while trying to create the clips.")
exit_program(error)
else:
return txt_file_path
def concatenate_clips(txt_file_path, output_folder, extension):
if not os.path.exists(txt_file_path):
raise ConcatenateError(f"{txt_file_path} does not exist.")
overview_filename = f"Overview_Video{extension}"
concatenated_filepath = os.path.join(output_folder, overview_filename)
subprocess_concatenate_args = [
"ffmpeg",
"-loglevel",
"warning",
"-stats",
"-y",
"-f",
"concat",
"-safe",
"0",
"-i",
txt_file_path,
"-c",
"copy",
concatenated_filepath,
]
line()
log.info("Concatenating the clips to create the overview video...")
result = subprocess.run(subprocess_concatenate_args)
log.info("Done!")
shutil.rmtree(os.path.join(output_folder, "clips"))
log.info("The clips have been deleted as they are no longer needed.")
if result.returncode == 0:
return concatenated_filepath
def create_overview_video(input_video, output_folder, interval_seconds, clip_length):
os.makedirs(output_folder, exist_ok=True)
extension = Path(input_video).suffix
try:
txt_file_path = create_clips(
input_video, output_folder, interval_seconds, clip_length
)
output_file = concatenate_clips(txt_file_path, output_folder, extension)
result = True
except ClipError as err:
result = False
exit_program(err.args[0])
except ConcatenateError as err:
result = False
exit_program(err.args[0])
if result:
log.info(f"Overview Video: {output_file}")
line()
return result, output_file