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
|
#!/usr/bin/env python3
# E.g. `./gen_combos.py [--write] color_quads/720p.png`
import concurrent.futures
import pathlib
import subprocess
import sys
ARGS = sys.argv
SRC_PATH = pathlib.Path(ARGS.pop())
assert SRC_PATH.exists(), "gen_combos.py [--flags] <src file path>"
DIR = SRC_PATH.parent
# crossCombine([{a:false},{a:5}], [{},{b:5}])
# [{a:false}, {a:true}, {a:false,b:5}, {a:true,b:5}]
def cross_combine(*args):
args = list(args)
def cross_combine2(listA, listB):
listC = []
for a in listA:
for b in listB:
c = dict()
c.update(a)
c.update(b)
listC.append(c)
return listC
res = [dict()]
while True:
try:
next = args.pop(0)
except IndexError:
break
res = cross_combine2(res, next)
return res
def keyed_combiner(key, vals):
res = []
for v in vals:
d = dict()
d[key] = v
res.append(d)
return res
# -
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# -
OGG = []
WEBM_CODECS = ["av1", "vp9"]
if "--all" in ARGS:
OGG = cross_combine(
[{"ext": "ogg"}], keyed_combiner("vcodec", ["theora", "vp8", "vp9"])
)
WEBM_CODECS += ["vp8"]
MP4 = cross_combine([{"ext": "mp4"}], keyed_combiner("vcodec", ["av1", "h264", "vp9"]))
WEBM = cross_combine([{"ext": "webm"}], keyed_combiner("vcodec", WEBM_CODECS))
# -
FORMAT_LIST = set(
[
"yuv420p",
"yuv420p10",
# 'yuv420p12',
# 'yuv420p16be',
# 'yuv420p16le',
"gbrp",
]
)
if "--all" in ARGS:
FORMAT_LIST |= set(
[
"yuv420p",
"yuv420p10",
"yuv420p12",
"yuv420p16be",
"yuv420p16le",
"yuv422p",
"yuv422p10",
"yuv422p12",
"yuv422p16be",
"yuv422p16le",
"yuv444p",
"yuv444p10",
"yuv444p12",
"yuv444p16be",
"yuv444p16le",
"yuv411p",
"yuv410p",
"yuyv422",
"uyvy422",
"rgb24",
"bgr24",
"rgb8",
"bgr8",
"rgb444be",
"rgb444le",
"bgr444be",
"bgr444le",
# 'nv12', # Encoding not different than yuv420p?
# 'nv21', # Encoding not different than yuv420p?
"gbrp",
"gbrp9be",
"gbrp9le",
"gbrp10be",
"gbrp10le",
"gbrp12be",
"gbrp12le",
"gbrp14be",
"gbrp14le",
"gbrp16be",
"gbrp16le",
]
)
FORMATS = keyed_combiner("format", list(FORMAT_LIST))
RANGE = keyed_combiner("range", ["tv", "pc"])
CSPACE_LIST = set(
[
"bt709",
# 'bt2020',
]
)
if "--all" in ARGS:
CSPACE_LIST |= set(
[
"bt709",
"bt2020",
"bt601-6-525", # aka smpte170m NTSC
"bt601-6-625", # aka bt470bg PAL
]
)
CSPACE_LIST = list(CSPACE_LIST)
# -
COMBOS = cross_combine(
WEBM + MP4 + OGG,
FORMATS,
RANGE,
keyed_combiner("src_cspace", CSPACE_LIST),
keyed_combiner("dst_cspace", CSPACE_LIST),
)
# -
print(f"{len(COMBOS)} combinations...")
todo = []
for c in COMBOS:
dst_name = ".".join(
[
SRC_PATH.name,
c["src_cspace"],
c["dst_cspace"],
c["range"],
c["format"],
c["vcodec"],
c["ext"],
]
)
src_cspace = c["src_cspace"]
vf = f"scale=out_range={c['range']}"
vf += f",colorspace=all={c['dst_cspace']}"
vf += f":iall={src_cspace}"
args = [
"ffmpeg",
"-y",
# For input:
"-color_primaries",
src_cspace,
"-color_trc",
src_cspace,
"-colorspace",
src_cspace,
"-i",
SRC_PATH.as_posix(),
# For output:
"-bitexact", # E.g. don't use true random uuids
"-vf",
vf,
"-pix_fmt",
c["format"],
"-vcodec",
c["vcodec"],
"-crf",
"1", # Not-quite-lossless
(DIR / dst_name).as_posix(),
]
if "-v" in ARGS or "-vv" in ARGS:
print("$ " + " ".join(args))
else:
print(" " + args[-1])
todo.append(args)
# -
with open(DIR / "reftest.list", "r") as f:
reftest_list_text = f.read()
for args in todo:
vid_name = pathlib.Path(args[-1]).name
if vid_name not in reftest_list_text:
print(f"WARNING: Not in reftest.list: {vid_name}")
# -
if "--write" not in ARGS:
print("Use --write to write. Exiting...")
exit(0)
# -
def run_cmd(args):
dest = None
if "-vv" not in ARGS:
dest = subprocess.DEVNULL
try:
subprocess.run(args, stderr=dest)
except FileNotFoundError:
print("FileNotFoundError, is ffmpeg not in your PATH?")
raise
with concurrent.futures.ThreadPoolExecutor() as pool:
fs = []
for cur_args in todo:
f = pool.submit(run_cmd, cur_args)
fs.append(f)
done = 0
for f in concurrent.futures.as_completed(fs):
f.result() # Raise if it raised
done += 1
sys.stdout.write(f"\rEncoded {done}/{len(todo)}")
|