-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
encoding.py
87 lines (78 loc) · 2.36 KB
/
encoding.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
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu
from __future__ import annotations
import pathlib
import shutil
import subprocess
import tempfile
from typing import Optional
from zimscraperlib import logger
from zimscraperlib.logging import nicer_args_join
def _build_ffmpeg_args(
src_path: pathlib.Path,
tmp_path: pathlib.Path,
ffmpeg_args: list[str],
threads: Optional[int],
) -> list[str]:
if threads:
if "-threads" in ffmpeg_args:
raise AttributeError("Cannot set the number of threads, already set")
else:
ffmpeg_args += ["-threads", str(threads)]
args = [
"/usr/bin/env",
"ffmpeg",
"-y",
"-i",
f"file:{src_path}",
*ffmpeg_args,
f"file:{tmp_path}",
]
return args
def reencode(
src_path,
dst_path,
ffmpeg_args,
delete_src=False, # noqa: FBT002
with_process=False, # noqa: FBT002
failsafe=True, # noqa: FBT002
threads: Optional[int] = 1,
):
"""Runs ffmpeg with given ffmpeg_args
Arguments -
src_path - Path to source file
dst_path - Path to destination file
ffmpeg_args - A list of ffmpeg arguments
threads - Number of encoding threads used by ffmpeg
delete_src - Delete source file after convertion
with_process - Optionally return the output from ffmpeg (stderr and stdout)
failsafe - Run in failsafe mode
"""
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = pathlib.Path(tmp_dir).joinpath(f"video.tmp{dst_path.suffix}")
args = _build_ffmpeg_args(
src_path=src_path,
tmp_path=tmp_path,
ffmpeg_args=ffmpeg_args,
threads=threads,
)
logger.debug(
f"Encode {src_path} -> {dst_path} video format = {dst_path.suffix}"
)
logger.debug(nicer_args_join(args))
ffmpeg = subprocess.run(
args,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
text=True,
check=False,
)
if not failsafe:
ffmpeg.check_returncode()
if ffmpeg.returncode == 0:
if delete_src:
src_path.unlink()
shutil.copy(tmp_path, dst_path)
if with_process:
return ffmpeg.returncode == 0, ffmpeg
return ffmpeg.returncode == 0