-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmaf.py
More file actions
51 lines (38 loc) · 1.02 KB
/
vmaf.py
File metadata and controls
51 lines (38 loc) · 1.02 KB
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
from rich.console import Console
from pathlib import Path
from shutil import which
import subprocess
import re
console = Console()
def get_ffmpeg() -> str:
path = which("ffmpeg")
if path is None:
console.print("[bold red]ffmpeg not found in PATH[/bold red]")
raise SystemExit(1)
return path
def build_vmaf(distorted: Path, reference: Path) -> list:
cmd = [
get_ffmpeg(),
"-i",
distorted,
"-i",
reference,
"-lavfi",
"libvmaf=n_threads=0",
"-f",
"null",
"-",
]
return cmd
def run_vmaf(command: list) -> float | None:
output = subprocess.run(command, capture_output=True, text=True)
if output is None:
return 0.0
for line in output.stderr.splitlines():
match = re.search(r"VMAF score: (\d+\.\d+)", line)
if match:
return round(float(match.group(1)), 2)
def vmaf(distorted, reference):
cmd = build_vmaf(distorted, reference)
score = run_vmaf(cmd)
return score