-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg-apple-prores-converter.py
180 lines (160 loc) · 4.87 KB
/
ffmpeg-apple-prores-converter.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
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
import argparse
import logging
import os
import platform
import subprocess
import sys
import time
from pathlib import Path
from typing import Optional
from lxmimgproc.ffmpeg import FFmpegProResDataRate
FILENAME = Path(__file__).stem
LOGGER = logging.getLogger(FILENAME)
def convert_to_prores(
ffmpeg_path: Path,
input_path: Path,
output_path: Path,
prores_data_rate: FFmpegProResDataRate,
prores_quality: int,
*args,
):
# recommendations from https://academysoftwarefoundation.github.io/EncodingGuidelines/EncodeProres.html
encoder = "prores_videotoolbox"
if platform.system() != "Darwin":
encoder = "prores_ks"
LOGGER.warning("prores output bitdepth limited to 10 bits on this platform !")
command = [
str(ffmpeg_path),
"-i",
str(input_path),
"-c:v",
encoder,
"-profile:v",
str(prores_data_rate.value),
"-vendor",
"apl0",
"-qscale:v",
f"{prores_quality}",
]
command += args
command += [
str(output_path),
]
LOGGER.debug(f"subprocess.run({command})")
subprocess.run(command, check=True)
def get_cli() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog=FILENAME,
description=(
"Convert any ffmpeg supported format to Apple ProRes."
"The output is tagged as sRGB for viewing (no data change)."
),
)
parser.add_argument(
"output_path",
type=str,
help=(
"filesystem path to write the final prores video to."
"the path can include the following tokens: \n"
"{input_filestem},{datarate},{quality} \n"
"which value are retrieved from the arguments provided."
),
)
parser.add_argument(
"input_path",
type=Path,
help="filesystem path to a file in an ffmpeg supported format",
)
parser.add_argument(
"--ffmpeg",
type=Path,
default=os.getenv("FFMPEG"),
help=(
"filesystem path to the ffmpeg executable."
'if not provided the value is retrieved from an "FFMPEG" environment variable.'
"Note ffmpeg version must be compiled with prores codecs."
),
)
parser.add_argument(
"--datarate",
type=str,
choices=[v.name for v in FFmpegProResDataRate],
default=FFmpegProResDataRate.s422.name,
help='"Flavor" of prores influencing the quality of the data stored: '
+ FFmpegProResDataRate.__doc__,
)
parser.add_argument(
"--quality",
type=int,
default=10,
help="general quality of the prores: values between 9 - 13 give a good result, 0 being best.",
)
parser.add_argument(
"--extra",
help="extra arguments passed to ffmpeg",
nargs="*",
default=[],
)
return parser
def run_cli(argv: list[str] = None) -> Path:
"""
Args:
argv: list of command line argument for the CLI
Returns:
filesystem path to the prores file on disk
"""
cli = get_cli()
argv = argv or sys.argv[1:]
parsed = cli.parse_args(argv)
logging.basicConfig(
level=logging.DEBUG,
format="{levelname: <7} | {asctime} [{name}:{funcName}] {message}",
style="{",
stream=sys.stdout,
)
input_path: Path = parsed.input_path
output_path: str = str(parsed.output_path)
ffmpeg: Optional[Path] = parsed.ffmpeg
if ffmpeg:
ffmpeg = Path(ffmpeg)
datarate: str = parsed.datarate
datarate: FFmpegProResDataRate = getattr(FFmpegProResDataRate, datarate)
quality: int = parsed.quality
extra_args = parsed.extra
# replace tokens
dst_path: str = output_path.replace("{input_filestem}", input_path.stem)
dst_path: str = dst_path.replace("{datarate}", datarate.name)
dst_path: str = dst_path.replace("{quality}", str(quality))
dst_path: Path = Path(dst_path)
if not ffmpeg:
print(
f"❌ no FFMEG executable path could be found",
file=sys.stderr,
)
sys.exit(11)
elif not ffmpeg.exists():
print(
f"❌ given FFMEG executable path doesn't exist: {ffmpeg}",
file=sys.stderr,
)
sys.exit(10)
start_time = time.time()
LOGGER.info(f"converting '{input_path}' to '{output_path}'")
convert_to_prores(
ffmpeg_path=ffmpeg,
input_path=input_path,
output_path=dst_path,
prores_data_rate=datarate,
prores_quality=quality,
*extra_args,
)
LOGGER.info(f"conversion took {time.time() - start_time:.2f}s")
if not dst_path.exists():
print(
f"❌ unexpected issue: destination video '{dst_path}' doesn't exist on disk.",
file=sys.stderr,
)
sys.exit(100)
return dst_path
if __name__ == "__main__":
run_cli()