forked from CrypticSignal/video-quality-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvmaf.py
69 lines (54 loc) · 2.21 KB
/
libvmaf.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
from ffmpeg_process_factory import LibVmafArguments
from utils import line, Logger, get_metrics_list
log = Logger("libvmaf")
# Change this if you want to use a different VMAF model file.
model_file_path = "vmaf_models/vmaf_v0.6.1.json"
def run_libvmaf(
transcode_output_path,
args,
json_file_path,
fps,
original_video_path,
factory,
duration,
crf_or_preset=None,
):
characters_to_escape = ["'", ":", ",", "[", "]"]
for character in characters_to_escape:
if character in json_file_path:
json_file_path = json_file_path.replace(character, f"\{character}")
n_subsample = "1" if not args.subsample else args.subsample
model_params = filter(None, [
f"path={model_file_path}",
"enable_transform=true" if args.phone_model else ""
])
model_string = f"model='{'|'.join(model_params)}'"
features = filter(None, [
"name=psnr" if args.calculate_psnr else "",
"name=float_ssim" if args.calculate_ssim else "",
"name=float_ms_ssim" if args.calculate_msssim else ""
])
feature_string = f":feature='{'|'.join(features)}'"
vmaf_options = f"""
{model_string}:log_fmt=json:log_path='{json_file_path}':n_subsample={n_subsample}:n_threads={args.n_threads}{feature_string}
"""
libvmaf_arguments = LibVmafArguments(
fps, transcode_output_path, original_video_path, vmaf_options
)
video_filters = args.video_filters if args.video_filters else None
libvmaf_arguments.video_filters(video_filters)
process = factory.create_process(libvmaf_arguments, args)
metrics_list = get_metrics_list(args)
metric_types = metrics_list[0]
if len(metrics_list) > 1:
metric_types = f"{', '.join(metrics_list[:-1])} and {metrics_list[-1]}"
message_transcoding_mode = ""
if not args.no_transcoding_mode:
if isinstance(args.crf, list) and len(args.crf) > 1:
message_transcoding_mode += f" achieved with CRF {crf_or_preset}"
else:
message_transcoding_mode += f" achieved with preset {crf_or_preset}"
line()
log.info(f"Calculating the {metric_types}{message_transcoding_mode}...")
process.run(original_video_path, duration)
log.info("Done!")