-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-video-transcoder.py
More file actions
352 lines (270 loc) · 8.97 KB
/
batch-video-transcoder.py
File metadata and controls
352 lines (270 loc) · 8.97 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python3
# Batch Video Transcoder
# Copyright (C) 2025, Maciej Jasiński <maciejj2000@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
__version__ = "1.0.0"
from os import path
import asyncio
import re
import argparse
from glob import glob
DEFAULT_QUALITY_THRESHOLD = 95
DEFAULT_TRANSCODE_WORKERS = 1
DEFAULT_VMAF_WORKERS = 1
DEFAULT_VMAF_THREADS = 14
DEFAULT_CQ_STEP = 2
DEFAULT_CQ_INITIAL = 40
DEFAULT_OUT_DIR = "./out"
DEFAULT_OUT_FILE_EXTENSION = ".mkv"
class CustomAsyncQueue(asyncio.Queue):
def __init__(self, maxsize=0):
super().__init__(maxsize)
self._unfinished_tasks_custom = 0
def put_nowait(self, item) -> None:
super().put_nowait(item)
self._unfinished_tasks_custom += 1
def task_done(self) -> None:
super().task_done()
self._unfinished_tasks_custom -= 1
def get_unfinished_tasks(self) -> int:
return self._unfinished_tasks_custom
def ffmpeg_compile_cmd(
input_path: str | list[str],
output_path: str,
ffmpeg_args: dict,
override_output: bool = False,
progress: bool = False,
nostdin: bool = True,
nostats: bool = True,
) -> list[str]:
cmd = ["ffmpeg"]
if isinstance(input_path, str):
input_path = [input_path]
if override_output:
cmd.append("-y")
if progress:
cmd.extend(["-progress", "-"])
if nostdin:
cmd.append("-nostdin")
if nostats:
cmd.append("-nostats")
for _input in input_path:
cmd.extend(["-i", path.abspath(_input)])
for flag, arg in ffmpeg_args.items():
cmd.extend(["-" + str(flag), str(arg)])
if output_path == "-":
cmd.append(output_path)
else:
cmd.append(path.abspath(output_path))
return cmd
async def ffmpeg_run_async(
ffmpeg_cmd: list[str], verbose: bool = False
) -> tuple[str, str]:
if verbose:
print(*ffmpeg_cmd)
try:
process = await asyncio.create_subprocess_exec(
*ffmpeg_cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
)
stdout, stderr = await process.communicate()
finally:
if process.returncode is None:
process.terminate()
return stdout, stderr
async def transcode_async(
in_path: str, out_path: str, cq: int, verbose: bool = False
) -> None:
transcode_settings = {
"vcodec": "hevc_nvenc",
"preset": "slow",
"rc": "vbr",
"cq": cq,
"ac": 1, # downmix audio channels to mono
"map_metadata": 0, # preserve metadata
}
ffmpeg_cmd = ffmpeg_compile_cmd(
in_path, out_path, transcode_settings, override_output=True
)
await ffmpeg_run_async(ffmpeg_cmd, verbose=verbose)
async def transcode_worker(
transcode_q: CustomAsyncQueue,
vmaf_q: CustomAsyncQueue,
verbose: bool = False,
) -> None:
while True:
in_path, out_path, cq = await transcode_q.get()
print(f"transcode start! {cq} {in_path}")
await transcode_async(in_path, out_path, cq, verbose=verbose)
await vmaf_q.put((out_path, in_path, cq))
transcode_q.task_done()
print(f"transcode done! {cq} {in_path}")
def vmaf_get_score(stdout: bytes) -> float | None:
match = re.search(r"(?<=VMAF score: )\d+\.\d+", stdout.decode())
if match:
return float(match.group())
else:
return None
async def vmaf_async(
distorted_path: str, reference_path: str, threads: int, verbose: bool = False
) -> float | None:
vmaf_settings = {
"filter_complex": f"libvmaf=n_threads={threads}",
"f": "null",
}
ffmpeg_cmd = ffmpeg_compile_cmd(
[distorted_path, reference_path], "-", vmaf_settings
)
stdout, _ = await ffmpeg_run_async(ffmpeg_cmd, verbose=verbose)
vmaf_score = vmaf_get_score(stdout)
return vmaf_score
async def vmaf_worker(
transcode_q: CustomAsyncQueue,
vmaf_q: CustomAsyncQueue,
threshold: float,
threads: int,
verbose: bool = False,
cq_step: int = DEFAULT_CQ_STEP,
) -> None:
while True:
distorted, reference, cq = await vmaf_q.get()
print(f"vmaf start! {cq} {reference}")
vmaf_score = await vmaf_async(distorted, reference, threads, verbose=verbose)
if vmaf_score is not None:
new_cq = cq - cq_step
if vmaf_score < threshold and new_cq >= 0:
await transcode_q.put((reference, distorted, new_cq))
print(f"vmaf done! {cq} {reference} Score: {vmaf_score:.2f}")
else:
await vmaf_q.put((distorted, reference, cq))
print(f"vmaf failed! {cq} {reference} retrying!")
vmaf_q.task_done()
def workers_create(
transcode_q: CustomAsyncQueue,
vmaf_q: CustomAsyncQueue,
n_transcode_workers: int,
n_vmaf_worker: int,
threshold: float,
threads: int,
verbose: bool = False,
) -> list[asyncio.Task]:
worker_tasks = []
for _ in range(n_transcode_workers):
worker_tasks.append(
asyncio.create_task(transcode_worker(transcode_q, vmaf_q, verbose=verbose))
)
for _ in range(n_vmaf_worker):
worker_tasks.append(
asyncio.create_task(
vmaf_worker(transcode_q, vmaf_q, threshold, threads, verbose=verbose)
)
)
return worker_tasks
def deduplicate_files(files: list[str]) -> list[str]:
return list(dict.fromkeys(files))
def queues_populate(
transcode_q: CustomAsyncQueue,
vmaf_q: CustomAsyncQueue,
input_files: list[str],
output_dir: str,
init_cq: int,
out_file_extension: str = DEFAULT_OUT_FILE_EXTENSION,
) -> None:
input_files = deduplicate_files(input_files)
for input_file in input_files:
out_name = path.basename(path.splitext(input_file)[0] + out_file_extension)
out_path = path.normpath(path.join(output_dir, out_name))
transcode_q.put_nowait((input_file, out_path, init_cq))
async def queues_wait_for_done(
transcode_q: CustomAsyncQueue,
vmaf_q: CustomAsyncQueue,
worker_tasks: list[asyncio.Task],
) -> None:
while transcode_q.get_unfinished_tasks() > 0 or vmaf_q.get_unfinished_tasks() > 0:
await transcode_q.join()
await vmaf_q.join()
for task in worker_tasks:
task.cancel()
async def main() -> None:
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("-i", "--input", type=str, nargs="+", help="input file path")
parser.add_argument(
"-o",
"--output",
type=str,
default=DEFAULT_OUT_DIR,
help="output directory path",
)
parser.add_argument(
"-cq",
"--init-cq",
type=int,
default=DEFAULT_CQ_INITIAL,
choices=range(0, 51),
metavar="[0 - 50]",
help="initial constant quality (cq) value to transcode with, "
+ f"decreases by {DEFAULT_CQ_STEP} on every step",
)
parser.add_argument(
"-q",
"--quality-threshold",
type=int,
default=DEFAULT_QUALITY_THRESHOLD,
choices=range(0, 101),
metavar="[0 - 100]",
help="the quality level, below which the file will be transcoded again with higher cq",
)
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument(
"-t",
"--threads",
type=int,
default=DEFAULT_VMAF_THREADS,
help="number of threads to compute on",
)
parser.add_argument(
"--transcode-workers",
type=int,
default=DEFAULT_TRANSCODE_WORKERS,
help="number of parallel workers to transcode on",
)
parser.add_argument(
"--vmaf-workers",
type=int,
default=DEFAULT_VMAF_WORKERS,
help="number of parallel workers to compute vmaf on",
)
args = parser.parse_args()
input_files = []
for input_str in args.input:
input_files.extend(glob(input_str))
transcode_q = CustomAsyncQueue()
vmaf_q = CustomAsyncQueue()
queues_populate(transcode_q, vmaf_q, input_files, args.output, args.init_cq)
worker_tasks = workers_create(
transcode_q,
vmaf_q,
args.transcode_workers,
args.vmaf_workers,
args.quality_threshold,
args.threads,
args.verbose,
)
await queues_wait_for_done(transcode_q, vmaf_q, worker_tasks)
if __name__ == "__main__":
asyncio.run(main())