-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrdtest.py
executable file
·945 lines (889 loc) · 25.6 KB
/
rdtest.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
#!/usr/bin/env python
"""rdtest.py module description."""
# Invalid name "get_info" (should match ^_?[A-Z][a-zA-Z0-9]*$)
# pylint: disable-msg=C0103
import argparse
import itertools
import os
import pandas as pd
import pathlib
import sys
import utils
CODEC_INFO = {
"mjpeg": {
"codecname": "mjpeg",
"extension": ".mp4",
"parameters": {},
},
"x264": {
"codecname": "libx264",
"extension": ".mp4",
"parameters": {},
"preset-name": "preset",
"presets": (
"ultrafast",
"superfast",
"veryfast",
"faster",
"fast",
"medium",
"slow",
"slower",
"veryslow",
"placebo",
),
},
"openh264": {
"codecname": "libopenh264",
"extension": ".mp4",
"parameters": {},
"preset-name": "complexity",
"presets": ("0", "1", "2"),
},
"x265": {
"codecname": "libx265",
"extension": ".mp4",
"parameters": {},
"preset-name": "preset",
"presets": (
"ultrafast",
"superfast",
"veryfast",
"faster",
"fast",
"medium",
"slow",
"slower",
"veryslow",
"placebo",
),
},
"vp8": {
"codecname": "vp8",
"extension": ".webm",
"parameters": {
# quality parameters
"quality": "realtime",
},
"preset-name": "cpu-used",
"presets": (
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
),
},
"vp9": {
"codecname": "libvpx-vp9",
"extension": ".webm",
"parameters": {
# quality parameters
"quality": "realtime",
"qmin": 2,
"qmax": 56,
},
"preset-name": "cpu-used",
"presets": ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"),
},
"libaom-av1": {
"codecname": "libaom-av1",
"extension": ".mp4",
"parameters": {
# ABR at https://trac.ffmpeg.org/wiki/Encode/AV1
# this should reduce the encoding time to manageable levels
# "cpu-used": 5,
},
"preset-name": "cpu-used",
"presets": ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"),
},
"libsvtav1": {
"codecname": "libsvtav1",
"extension": ".mp4",
"parameters": {},
"preset-name": "preset",
"presets": (
"-1",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
),
},
"libsvtav1-raw": {
"codecname": "libsvtav1-raw",
"extension": ".ivf",
# "binary": "~/work/video/av1/svt-av1/Bin/Release/SvtAv1EncApp",
"binary": os.environ.get("SVTAV1ENCAPP_BIN", None),
"parameters": {},
"preset-name": "preset",
"presets": (
"-1",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
),
},
}
# codecs
DEFAULT_CODECS = CODEC_INFO.keys()
# resolution set 1
DEFAULT_RESOLUTIONS = [
"640x360", # 200-437kbps
"480x272", # actually 480x270 100-200kbps
"320x160", # < 100kbps
]
# resolution set 2
DEFAULT_RESOLUTIONS = [
"1280x720",
"864x480",
"640x360",
"432x240",
"216x120",
# '160x90',
]
# use None for input resolution
DEFAULT_RESOLUTIONS = [
None,
]
# bitrates are defined in Kbps
# TODO(chema): add a better mechanism to define bitrates
DEFAULT_BITRATES = [
2500,
1000,
560,
280,
140,
70,
35,
]
DEFAULT_QUALITIES = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
# https://trac.ffmpeg.org/wiki/Encode/H.264
# https://trac.ffmpeg.org/wiki/Encode/H.265
DEFAULT_PRESETS = [
"ultrafast",
"superfast",
"veryfast",
"faster",
"fast",
"medium", # default preset
"slow",
"slower",
"veryslow",
]
# https://trac.ffmpeg.org/wiki/Encode/AV1
# higher numbers provide a higher encoding speed
PRESETS_AV1 = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
]
DEFAULT_RCMODES = [
"cbr",
"crf",
]
default_values = {
"debug": 0,
"cleanup": 0,
"label": "",
"ref_res": None,
"ref_pix_fmt": "yuv420p",
"vmaf_dir": "/tmp/",
"tmp_dir": "/tmp/",
"gop_length_frames": 600,
"codecs": DEFAULT_CODECS,
"resolutions": DEFAULT_RESOLUTIONS,
"bitrates": DEFAULT_BITRATES,
"qualities": DEFAULT_QUALITIES,
"presets": DEFAULT_PRESETS,
"rcmodes": DEFAULT_RCMODES,
"infile_list": [],
"outfile": None,
"logfile": None,
}
def run_experiment(options, logfd):
# check all software is ok
utils.check_software(logfd, options.debug)
# prepare output directory
pathlib.Path(options.tmp_dir).mkdir(parents=True, exist_ok=True)
df = None
for infile in options.infile_list:
df_tmp = run_experiment_single_file(
infile,
options.label,
options.codecs,
options.resolutions,
options.rcmodes,
options.presets,
options.bitrates,
options.qualities,
options.ref_res,
options.ref_pix_fmt,
options.gop_length_frames,
options.tmp_dir,
options.cleanup,
logfd,
options.debug,
)
df = df_tmp if df is None else pd.concat([df, df_tmp])
# write up the results
df.to_csv(options.outfile, index=False)
def run_experiment_single_file(
infile,
label,
codecs,
resolutions,
rcmodes,
presets,
bitrates,
qualities,
ref_res,
ref_pix_fmt,
gop_length_frames,
tmp_dir,
cleanup,
logfd,
debug,
):
# 1. in: get infile information
if debug > 0:
print("# [run] parsing file: %s" % (infile), file=logfd)
assert os.access(infile, os.R_OK), "file %s is not readable" % infile
in_basename = os.path.basename(infile)
in_resolution = utils.get_resolution(infile, logfd, debug)
in_framerate = utils.get_framerate(infile, logfd, debug)
# 2. ref: decode the original file into a raw file
ref_basename = f"{in_basename}.ref_{in_resolution}.y4m"
if debug > 0:
print(f"# [run] normalize file: {infile} -> {ref_basename}", file=logfd)
ref_filename = os.path.join(tmp_dir, ref_basename)
ref_resolution = in_resolution if ref_res is None else ref_res
ref_framerate = in_framerate
ref_pix_fmt = ref_pix_fmt
ffmpeg_params = [
"-y",
"-i",
infile,
"-s",
ref_resolution,
"-pix_fmt",
ref_pix_fmt,
ref_filename,
]
retcode, stdout, stderr, _ = utils.ffmpeg_run(ffmpeg_params, logfd, debug)
assert retcode == 0, stderr
# check produced file matches the requirements
assert ref_resolution == utils.get_resolution(
ref_filename, logfd, debug
), "Error: %s must have resolution: %s (is %s)" % (
ref_filename,
ref_resolution,
utils.get_resolution(ref_filename, logfd, debug),
)
assert ref_pix_fmt == utils.get_pix_fmt(
ref_filename, logfd, debug
), "Error: %s must have pix_fmt: %s (is %s)" % (
ref_filename,
ref_pix_fmt,
utils.get_pix_fmt(ref_filename, logfd, debug),
)
columns_init = (
"infile",
"label",
"codec",
"resolution",
"width",
"height",
"framerate",
"rcmode",
"quality",
"bitrate",
"preset",
"actual_bitrate",
)
columns_fini = ("parameters",)
df = None
# run the list of encodings
for codec, resolution, rcmode, preset in itertools.product(
codecs, resolutions, rcmodes, presets
):
if resolution is None:
resolution = in_resolution
parameters_csv_str = ""
for k, v in CODEC_INFO[codec]["parameters"].items():
parameters_csv_str += "%s=%s;" % (k, str(v))
# get bitrate/quality list
if rcmode == "cbr":
quality_bitrate_option = "bitrate"
qualities_bitrates = bitrates
elif rcmode == "crf":
quality_bitrate_option = "quality"
qualities_bitrates = qualities
for quality_bitrate in qualities_bitrates:
(
encoder_stats,
actual_bitrate,
psnr_dict,
ssim_dict,
vmaf_dict,
) = run_single_experiment(
ref_filename,
ref_resolution,
ref_pix_fmt,
ref_framerate,
codec,
resolution,
quality_bitrate_option,
quality_bitrate,
preset,
rcmode,
gop_length_frames,
tmp_dir,
logfd,
debug,
cleanup,
)
width, height = resolution.split("x")
ref_framerate = utils.get_framerate(ref_filename, logfd, debug)
if quality_bitrate_option == "bitrate":
quality = ""
bitrate = quality_bitrate
elif quality_bitrate_option == "quality":
bitrate = ""
quality = quality_bitrate
if df is None:
columns = (
columns_init
+ tuple(encoder_stats.keys())
+ tuple(psnr_dict.keys())
+ tuple(ssim_dict.keys())
+ tuple(vmaf_dict.keys())
+ columns_fini
)
df = pd.DataFrame(columns=columns)
df.loc[len(df.index)] = (
in_basename,
label,
codec,
resolution,
width,
height,
ref_framerate,
rcmode,
quality,
bitrate,
preset,
actual_bitrate,
*encoder_stats.values(),
*psnr_dict.values(),
*ssim_dict.values(),
*vmaf_dict.values(),
parameters_csv_str,
)
return df
def run_single_enc(
infile,
outfile,
codec,
resolution,
quality_bitrate_option,
quality_bitrate,
preset,
rcmode,
gop_length_frames,
logfd,
debug,
):
if debug > 0:
print("# [%s] encoding file: %s -> %s" % (codec, infile, outfile), file=logfd)
# get encoding settings
enc_tool = "ffmpeg"
enc_parms = [
"-y",
]
enc_parms += ["-i", infile]
enc_env = None
if CODEC_INFO[codec]["codecname"] == "libsvtav1-raw":
enc_tool = CODEC_INFO[codec]["binary"]
enc_parms = ["-i", infile]
# ~/work/video/av1/svt-av1/Bin/Release/SvtAv1EncApp --rc 1 --lp 1 --tbr 14000 --preset 10 --keyint 600 -i /tmp/rdtest_py_tmp/easy.mp4.ref_1728x2304.y4m --output /tmp/rdtest_py_tmp/foo.mp4.ivf
if rcmode == "cbr":
# TODO(chema): use VBR (1) instead of CBR (2) as SvtAv1EncApp is complaining
# Svt[error]: CBR Rate control is currently not supported for SVT_AV1_PRED_RANDOM_ACCESS, use VBR mode
# enc_parms += ["--rc", "2"]
enc_parms += ["--rc", "1"]
bitrate = quality_bitrate
enc_parms += ["--tbr", "%s" % bitrate]
elif rcmode == "crf":
enc_parms += ["--rc", "0"]
quality = quality_bitrate
enc_parms += ["--crf", "%s" % quality]
enc_parms += ["--preset", "%s" % preset]
# maximize CPU usage
enc_parms += ["--lp", "0"]
# set random access prediction structure
enc_parms += ["--pred-struct", "2"]
if gop_length_frames is not None:
enc_parms += ["--keyint", str(gop_length_frames)]
enc_parms += ["--output", outfile]
elif CODEC_INFO[codec]["codecname"] == "mjpeg":
enc_parms += ["-c:v", CODEC_INFO[codec]["codecname"]]
# TODO(chema): use bitrate as quality value (2-31)
assert rcmode == "crf", f"error: mjpeg only defined for {rcmode}"
quality = quality_bitrate
enc_parms += ["-q:v", "%s" % quality]
enc_parms += ["-s", resolution]
else:
enc_parms += ["-c:v", CODEC_INFO[codec]["codecname"]]
if rcmode == "cbr":
bitrate = quality_bitrate
# enc_parms += ["-maxrate", "%sk" % bitrate]
# enc_parms += ["-minrate", "%sk" % bitrate]
enc_parms += ["-b:v", "%sk" % bitrate]
# if CODEC_INFO[codec]["codecname"] in ("libx264", "libopenh264", "libx265"):
# # set bufsize to 2x the bitrate
# bufsize = str(int(bitrate) * 2)
# enc_parms += ["-bufsize", bufsize]
elif rcmode == "crf":
quality = quality_bitrate
enc_parms += ["-crf", "%s" % quality]
if CODEC_INFO[codec]["codecname"] in ("libx264", "libx265"):
# no b-frames
enc_parms += ["-bf", "0"]
#enc_parms += ["-x264opts", "keyint=1"] # force all-intra
#enc_parms += ["-x265-params", "keyint=1"] # force all-intra
# add preset (if available)
if CODEC_INFO[codec].get("preset-name", None) is not None:
preset_name = CODEC_INFO[codec].get("preset-name")
enc_parms += [f"-{preset_name}", preset]
enc_parms += ["-s", resolution]
if gop_length_frames is not None:
enc_parms += ["-g", str(gop_length_frames)]
for k, v in CODEC_INFO[codec]["parameters"].items():
enc_parms += ["-%s" % k, str(v)]
if CODEC_INFO[codec]["codecname"] in ("libaom-av1",):
# ABR at https://trac.ffmpeg.org/wiki/Encode/AV1
enc_parms += ["-strict", "experimental"]
if enc_tool == "ffmpeg":
# pass audio through
enc_parms += ["-c:a", "copy"]
enc_parms += [outfile]
# run encoder
cmd = [
enc_tool,
] + enc_parms
retcode, stdout, stderr, stats = utils.run(
cmd, env=enc_env, logfd=logfd, debug=debug, gnu_time=True
)
assert retcode == 0, stderr
return stats
def run_single_dec(infile, outfile, codec, logfd, debug):
if debug > 0:
print("# [%s] decoding file: %s -> %s" % (codec, infile, outfile), file=logfd)
# get decoding settings
dec_tool = "ffmpeg"
dec_parms = []
dec_parms += ["-i", infile]
dec_env = None
dec_parms += ["-y", outfile]
# run decoder
cmd = [
dec_tool,
] + dec_parms
retcode, stdout, stderr, _ = utils.run(cmd, env=dec_env, logfd=logfd, debug=debug)
assert retcode == 0, stderr
def run_single_experiment(
ref_filename,
ref_resolution,
ref_pix_fmt,
ref_framerate,
codec,
resolution,
quality_bitrate_option,
quality_bitrate,
preset,
rcmode,
gop_length_frames,
tmp_dir,
logfd,
debug,
cleanup,
):
if debug > 0:
print(
f"# [run] run_single_experiment codec: {codec} resolution: {resolution} "
f"{quality_bitrate_option}: {quality_bitrate} rcmode: {rcmode} preset: {preset}",
file=logfd,
)
ref_basename = os.path.basename(ref_filename)
# common info for enc, dec, and decs
gen_basename = ref_basename + f".ref_{ref_resolution}"
gen_basename += f".codec_{codec}"
gen_basename += f".resolution_{resolution}"
gen_basename += f".{quality_bitrate_option}_{quality_bitrate}"
gen_basename += f".preset_{preset}"
gen_basename += f".rcmode_{rcmode}"
# 3. enc: encode copy with encoder
enc_basename = gen_basename + CODEC_INFO[codec]["extension"]
enc_filename = os.path.join(tmp_dir, enc_basename)
encoder_stats = run_single_enc(
ref_filename,
enc_filename,
codec,
resolution,
quality_bitrate_option,
quality_bitrate,
preset,
rcmode,
gop_length_frames,
logfd,
debug,
)
# 4. dec: decode copy in order to get statistics
dec_basename = enc_basename + ".y4m"
dec_filename = os.path.join(tmp_dir, dec_basename)
run_single_dec(enc_filename, dec_filename, codec, logfd, debug)
# 5. decs: scale the decoded video to the reference resolution and
# pixel format
# This is needed to make sure the quality metrics make sense
decs_basename = dec_basename + ".scaled"
decs_basename += ".resolution_%s" % ref_resolution
decs_basename += ".y4m"
decs_filename = os.path.join(tmp_dir, decs_basename)
if debug > 0:
print(
"# [%s] scaling file: %s -> %s" % (codec, dec_filename, decs_filename),
file=logfd,
)
ffmpeg_params = [
"-y",
"-nostats",
"-loglevel",
"0",
"-i",
dec_filename,
"-pix_fmt",
ref_pix_fmt,
"-s",
ref_resolution,
decs_filename,
]
retcode, stdout, stderr, _ = utils.ffmpeg_run(ffmpeg_params, logfd, debug)
assert retcode == 0, stderr
# check produced file matches the requirements
assert ref_resolution == utils.get_resolution(
decs_filename, logfd, debug
), "Error: %s must have resolution: %s (is %s)" % (
decs_filename,
ref_resolution,
utils.get_resolution(decs_filename, logfd, debug),
)
assert ref_pix_fmt == utils.get_pix_fmt(
decs_filename, logfd, debug
), "Error: %s must have pix_fmt: %s (is %s)" % (
decs_filename,
ref_pix_fmt,
utils.get_pix_fmt(decs_filename, logfd, debug),
)
# get quality scores
psnr_dict = utils.get_psnr(decs_filename, ref_filename, None, logfd, debug)
ssim_dict = utils.get_ssim(decs_filename, ref_filename, None, logfd, debug)
vmaf_dict = utils.get_vmaf(decs_filename, ref_filename, None, logfd, debug)
# get actual bitrate
actual_bitrate = utils.get_bitrate(enc_filename, logfd, debug)
# clean up experiments files
if cleanup > 0:
os.remove(dec_filename)
os.remove(decs_filename)
if cleanup > 1:
os.remove(enc_filename)
return encoder_stats, actual_bitrate, psnr_dict, ssim_dict, vmaf_dict
def get_options(argv):
"""Generic option parser.
Args:
argv: list containing arguments
Returns:
Namespace - An argparse.ArgumentParser-generated option object
"""
# init parser
# usage = 'usage: %prog [options] arg1 arg2'
# parser = argparse.OptionParser(usage=usage)
# parser.print_help() to get argparse.usage (large help)
# parser.print_usage() to get argparse.usage (just usage line)
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-d",
"--debug",
action="count",
dest="debug",
default=default_values["debug"],
help="Increase verbosity (multiple times for more)",
)
parser.add_argument(
"--quiet",
action="store_const",
dest="debug",
const=-1,
help="Zero verbosity",
)
parser.add_argument(
"--cleanup",
action="store_const",
dest="cleanup",
const=1,
default=default_values["cleanup"],
help="Cleanup Raw Files%s"
% (" [default]" if default_values["cleanup"] == 1 else ""),
)
parser.add_argument(
"--full-cleanup",
action="store_const",
dest="cleanup",
const=2,
default=default_values["cleanup"],
help="Cleanup All Files%s"
% (" [default]" if default_values["cleanup"] == 2 else ""),
)
parser.add_argument(
"--no-cleanup",
action="store_const",
dest="cleanup",
const=0,
help="Do Not Cleanup Files%s"
% (" [default]" if not default_values["cleanup"] == 0 else ""),
)
parser.add_argument(
"--label",
action="store",
dest="label",
default=default_values["label"],
help="use LABEL to label the experiment",
)
parser.add_argument(
"-s",
"--resolution",
action="store",
dest="ref_res",
default=default_values["ref_res"],
help="force RESOLUTION for the raw video",
)
parser.add_argument(
"--pix_fmt",
action="store",
dest="ref_pix_fmt",
default=default_values["ref_pix_fmt"],
help="force PIX_FMT pix_fmt for the raw video",
)
parser.add_argument(
"--tmp-dir",
action="store",
dest="tmp_dir",
default=default_values["tmp_dir"],
help="use TMP_DIR tmp dir",
)
parser.add_argument(
"--gop-length",
action="store",
dest="gop_length_frames",
default=default_values["gop_length_frames"],
help="GoP length (in frames)",
)
parser.add_argument(
"--vmaf-dir",
action="store",
dest="vmaf_dir",
default=default_values["vmaf_dir"],
help="use VMAF_DIR vmaf dir",
)
# list of arguments
parser.add_argument(
"--codecs",
nargs="+",
dest="codecs",
default=default_values["codecs"],
help="use CODECS list (%s)" % list(CODEC_INFO.keys()),
)
parser.add_argument(
"--resolutions",
nargs="+",
dest="resolutions",
default=default_values["resolutions"],
help="use RESOLUTIONS list",
)
parser.add_argument(
"--bitrates",
nargs="+",
dest="bitrates",
default=default_values["bitrates"],
help="use BITRATES list",
)
parser.add_argument(
"--qualities",
nargs="+",
dest="qualities",
default=default_values["qualities"],
help="use QUALITIES list",
)
parser.add_argument(
"--presets",
nargs="+",
dest="presets",
default=default_values["presets"],
help="use PRESETS list",
)
parser.add_argument(
"--rcmodes",
nargs="+",
dest="rcmodes",
default=default_values["rcmodes"],
help="use RCMODES list",
)
parser.add_argument(
dest="infile_list",
type=str,
nargs="+",
default=default_values["infile_list"],
metavar="input-file",
help="input file",
)
parser.add_argument(
"-o",
"--outfile",
dest="outfile",
type=str,
default=default_values["outfile"],
metavar="output-file",
help="results file",
)
parser.add_argument(
"--logfile",
action="store",
dest="logfile",
type=str,
default=default_values["logfile"],
metavar="log-file",
help="log file",
)
# do the parsing
options = parser.parse_args(argv[1:])
# post-process list-based arguments
# support ',' and ' ' to separate list-based options
for field in (
"codecs",
"resolutions",
"bitrates",
"rcmodes",
"qualities",
"presets",
):
if len(vars(options)[field]) == 1:
for sep in (",", " "):
parameter = vars(options)[field][0]
if parameter is None:
continue
elif sep in parameter:
vars(options)[field] = parameter.split(sep)
# check valid values in options.codecs
if not all(c in CODEC_INFO.keys() for c in options.codecs):
print(
"# error: invalid codec(s): %r supported_codecs: %r"
% (
[c for c in options.codecs if c not in CODEC_INFO.keys()],
list(CODEC_INFO.keys()),
)
)
sys.exit(-1)
# check valid values in options.resolutions
if not all((r is None or "x" in r) for r in options.resolutions):
print(
"# error: invalid resolution(s): %r"
% ([r for r in options.resolutions if "x" not in r])
)
sys.exit(-1)
# check valid values in options.bitrates
if not all((isinstance(b, int) or b.isnumeric()) for b in options.bitrates):
print(
"# error: invalid bitrate(s): %r"
% (
[
b
for b in options.bitrates
if not (isinstance(b, int) or b.isnumeric())
]
),
file=logfd,
)
sys.exit(-1)
# check valid values in options.rcmodes
if not all(r in DEFAULT_RCMODES for r in options.rcmodes):
print(
"# error: invalid rcmode(s): %r supported_rcmodes: %r"
% (
[r for r in options.rcmodes if r not in DEFAULT_RCMODES],
DEFAULT_RCMODES,
)
)
sys.exit(-1)
return options
def main(argv):
# parse options
options = get_options(argv)
# get logfile descriptor
if options.logfile is None:
logfd = sys.stdout
else:
logfd = open(options.logfile, "w")
# get infile/outfile
assert options.outfile != "-"
# print results
if options.debug > 0:
print(f"debug: {options}")
# do something
run_experiment(options, logfd)
if __name__ == "__main__":
# at least the CLI program name: (CLI) execution
main(sys.argv)