Compare commits

..

3 Commits

Author SHA1 Message Date
7a3d439211 ignore vmaf.json and any csv file 2022-10-23 20:03:44 +02:00
183336c4b2 Python script run through various encoding scenarios automatically
- Saves results in a csv file
        - VMAF scoring (mean, min)
        - File size
2022-10-23 20:01:10 +02:00
e338651ad6 Add notes on crf and preset values for encoders 2022-10-23 18:48:06 +02:00
3 changed files with 237 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vmaf.json
*.csv

172
Sparks_in_Blender_encode.py Executable file
View File

@ -0,0 +1,172 @@
#!/usr/bin/env python3
import os
import time
import csv
import ffmpy
from multiprocessing import cpu_count
from collections import OrderedDict
import json
# always round timestamp to integer
def now():
return int(time.time())
def write_line(
codec: str,
crf: str,
preset: str,
infile: str,
outfile: str,
outfilesize: float,
enctime: int,
vmafmean: float,
vmafmin: float,
):
"""
Write line to data csv
Parameters:
codec (str): Codec used
crf (str): CRF used
preset (str): Preset used
infile (str): Input file name
outfile (str): Output file name
outfilesize (float): Size of output file
enctime (int): Time to encode
vmafmean (float): Mean VMAF score
vmafmin (float): Min VMAF score
"""
with open(datafile, "a", newline="") as file:
write = csv.writer(file)
write.writerow(
(
codec,
crf,
preset,
infile,
outfile,
outfilesize,
enctime,
vmafmean,
vmafmin,
)
)
if __name__ == "__main__":
encoding = {
"libx264": {
"crf": [15, 20, 25, 30, 35],
"presets": ["fast", "medium", "slow", "veryslow"],
},
"libx265": {
"crf": [15, 20, 25, 30, 35],
"presets": ["fast", "medium", "slow", "veryslow"],
},
"libaom-av1": {"crf": [20, 25, 30, 35, 40], "presets": [0, 2, 4, 6]},
"libsvtav1": {"crf": [20, 25, 30, 35, 40], "presets": [0, 4, 8, 12]},
}
if not os.path.isdir("encoded"):
os.mkdir("encoded")
datafile = "data-" + str(now()) + ".csv"
with open(datafile, "w", newline="") as file:
write = csv.writer(file)
write.writerow(
(
"Codec",
"CRF",
"Preset",
"Input file",
"Output file",
"Output file size (MiB)",
"Encode time (s)",
"VMAF Score (mean)",
"VMAF Score (min)",
)
)
for codec in encoding:
for crf in encoding[codec]["crf"]:
for preset in encoding[codec]["presets"]:
outputfile = os.path.join(
"encoded",
(
"Sparks_in_Blender-codec_"
+ codec
+ "-crf_"
+ str(crf)
+ "-preset_"
+ str(preset)
+ ".mkv"
),
)
# libaom needs additional options
if codec == "libaom-av1":
ff = ffmpy.FFmpeg(
inputs={"Sparks_in_Blender.webm": None},
outputs={
outputfile: "-c:v {videocodec} -crf {crf} -b:v 0 -cpu-used {preset} -row-mt 1 -tiles 2x2 -g 240 -map 0:v:0 ".format(
videocodec=codec,
crf=crf,
preset=preset,
)
},
)
else:
ff = ffmpy.FFmpeg(
inputs={"Sparks_in_Blender.webm": None},
outputs={
outputfile: "-c:v {videocodec} -crf {crf} -preset {preset} -g 240 -map 0:v:0 ".format(
videocodec=codec,
crf=crf,
preset=preset,
)
},
)
starttime = now()
ff.run()
endtime = now()
difftime = int(endtime - starttime)
outputfilesize = os.path.getsize(outputfile) / 1024 / 1024
ffvmaf = ffmpy.FFmpeg(
inputs=OrderedDict(
[(outputfile, None), ("Sparks_in_Blender.webm", None)]
),
outputs={
"-": "-filter_complex libvmaf=log_fmt=json:n_threads={cputhreads}:log_path=vmaf.json -f null".format(
cputhreads=cpu_count()
)
},
)
ffvmaf.run()
with open("vmaf.json", "r") as file:
vmaf = json.load(file)
write_line(
codec=codec,
crf=crf,
preset=preset,
infile="Sparks_in_Blender.webm",
outfile=outputfile,
outfilesize=outputfilesize,
enctime=difftime,
vmafmean=vmaf["pooled_metrics"]["vmaf"]["mean"],
vmafmin=vmaf["pooled_metrics"]["vmaf"]["min"],
)

63
notes.md Normal file
View File

@ -0,0 +1,63 @@
# Notes
`ffmpeg -i almatrailer2016a.mp4 -map 0:v:0`
`ffmpeg -i eso1902a.mp4 -map 0:v:0`
`ffmpeg -i Sparks_in_Blender.webm -map 0:v:0`
```
$crf: CRF, see encoder documentation for valid values
$preset: Encoder preset, see documentation for values
$keyint: Keyframe interval. Set to (fps * 10) for consistency
```
## H.264
`-c:v libx264 -crf $crf -preset $preset -g $keyint`
CRF: 051
Presets:
```
ultrafast
superfast
veryfast
faster
fast
medium
slow
slower
veryslow
placebo
```
## H.265
`-c:v libx265 -crf $crf -preset $preset -g $keyint`
CRF: 0-51
Presets:
```
ultrafast
superfast
veryfast
faster
fast
medium
slow
slower
veryslow
placebo
```
## AV1
### libaom
`-c:v libaom-av1 -crf $crf -b:v 0 -cpu-used $preset -row-mt 1 -g $keyint -tiles 2x2`
CRF: 0-63
Presets: 0-6
### SVT-AV1
`-c:v libsvtav1 -crf $crf -preset $preset -g $keyint`
CRF: 0-63
Presets: 0-12