2022-10-23 20:01:10 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
import csv
|
|
|
|
|
|
|
|
import ffmpy
|
|
|
|
|
|
|
|
from multiprocessing import cpu_count
|
|
|
|
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
2022-10-24 11:03:09 +02:00
|
|
|
from typing import Union, Any
|
|
|
|
|
2022-10-23 20:56:13 +02:00
|
|
|
# encoding options used
|
2022-10-24 11:03:09 +02:00
|
|
|
encoding: dict[str, Any] = {
|
2022-10-23 20:56:13 +02:00
|
|
|
"libx264": {
|
2022-10-23 21:00:59 +02:00
|
|
|
"crf": [10, 14, 16, 18, 20, 22, 25],
|
|
|
|
"presets": [
|
|
|
|
"superfast",
|
|
|
|
"veryfast",
|
|
|
|
"faster",
|
|
|
|
"fast",
|
|
|
|
"medium",
|
|
|
|
"slow",
|
|
|
|
"slower",
|
|
|
|
"veryslow",
|
|
|
|
],
|
2022-10-23 20:56:13 +02:00
|
|
|
},
|
|
|
|
"libx265": {
|
2022-10-23 21:00:59 +02:00
|
|
|
"crf": [10, 14, 16, 18, 20, 22, 25],
|
|
|
|
"presets": [
|
|
|
|
"superfast",
|
|
|
|
"veryfast",
|
|
|
|
"faster",
|
|
|
|
"fast",
|
|
|
|
"medium",
|
|
|
|
"slow",
|
|
|
|
"slower",
|
|
|
|
"veryslow",
|
|
|
|
],
|
2022-10-23 20:56:13 +02:00
|
|
|
},
|
|
|
|
"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]},
|
|
|
|
}
|
|
|
|
|
|
|
|
# program version
|
|
|
|
# make tests reproducible by tag
|
2022-10-24 21:26:58 +02:00
|
|
|
version = "v0.0.1a3"
|
2022-10-23 20:56:13 +02:00
|
|
|
|
2022-10-23 20:01:10 +02:00
|
|
|
# always round timestamp to integer
|
|
|
|
def now():
|
|
|
|
return int(time.time())
|
|
|
|
|
|
|
|
|
|
|
|
def write_line(
|
|
|
|
codec: str,
|
2022-10-24 11:03:09 +02:00
|
|
|
crf: int,
|
|
|
|
preset: Union[str, int],
|
2022-10-23 20:01:10 +02:00
|
|
|
infile: str,
|
|
|
|
outfilesize: float,
|
|
|
|
enctime: int,
|
|
|
|
vmafmean: float,
|
|
|
|
vmafmin: float,
|
2022-10-24 21:26:45 +02:00
|
|
|
ssim: float,
|
|
|
|
mse: float,
|
2022-10-23 20:01:10 +02:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Write line to data csv
|
|
|
|
|
|
|
|
Parameters:
|
2022-10-24 21:26:45 +02:00
|
|
|
codec (str): Codec used
|
|
|
|
crf (int): CRF used
|
|
|
|
preset (str/int): Preset used
|
|
|
|
infile (str): Input file name
|
|
|
|
outfilesize (float): Size of output file
|
|
|
|
enctime (int): Time to encode
|
|
|
|
vmafmean (float): Mean VMAF score
|
|
|
|
vmafmin (float): Min VMAF score
|
|
|
|
ssim (float): SSIM Score
|
|
|
|
mse (float): MSE Score
|
2022-10-23 20:01:10 +02:00
|
|
|
"""
|
|
|
|
with open(datafile, "a", newline="") as file:
|
|
|
|
write = csv.writer(file)
|
|
|
|
write.writerow(
|
|
|
|
(
|
|
|
|
codec,
|
|
|
|
crf,
|
|
|
|
preset,
|
|
|
|
infile,
|
|
|
|
outfilesize,
|
|
|
|
enctime,
|
|
|
|
vmafmean,
|
|
|
|
vmafmin,
|
2022-10-24 21:26:45 +02:00
|
|
|
ssim,
|
|
|
|
mse,
|
2022-10-23 20:01:10 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-24 11:03:09 +02:00
|
|
|
def encode_general(
|
|
|
|
inputfile: str, outputfile: str, codec: str, crf: int, preset: Union[str, int]
|
|
|
|
):
|
2022-10-24 10:08:30 +02:00
|
|
|
"""
|
|
|
|
General encoding function
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
inputfile (str): Path to input file
|
|
|
|
outputfile (str): Path to output file
|
|
|
|
codec (str): Codec used
|
2022-10-24 11:03:09 +02:00
|
|
|
crf (int): CRF value
|
|
|
|
preset (str/int): Choosen preset
|
2022-10-24 10:08:30 +02:00
|
|
|
"""
|
|
|
|
ff = ffmpy.FFmpeg(
|
|
|
|
inputs={inputfile: None},
|
|
|
|
outputs={
|
|
|
|
outputfile: "-c:v {videocodec} -crf {crf} -preset {preset} -g 240 -map 0:v:0 ".format(
|
|
|
|
videocodec=codec,
|
|
|
|
crf=crf,
|
|
|
|
preset=preset,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return ff
|
|
|
|
|
|
|
|
|
2022-10-24 11:03:09 +02:00
|
|
|
def encode_libaom(inputfile: str, outputfile: str, crf: int, preset: Union[str, int]):
|
2022-10-24 10:08:30 +02:00
|
|
|
"""
|
|
|
|
Encoding with libaom
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
inputfile (str): Path to input file
|
|
|
|
outputfile (str): Path to output file
|
2022-10-24 11:03:09 +02:00
|
|
|
crf (int): CRF value
|
|
|
|
preset (str/int): Choosen preset
|
2022-10-24 10:08:30 +02:00
|
|
|
"""
|
|
|
|
ff = ffmpy.FFmpeg(
|
|
|
|
inputs={inputfile: None},
|
|
|
|
outputs={
|
|
|
|
outputfile: "-c:v libaom-av1 -crf {crf} -b:v 0 -cpu-used {preset} -row-mt 1 -tiles 2x2 -g 240 -map 0:v:0 ".format(
|
|
|
|
crf=crf,
|
|
|
|
preset=preset,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return ff
|
|
|
|
|
|
|
|
|
2022-10-24 10:25:40 +02:00
|
|
|
def score_vmaf(outputfile: str, inputfile: str) -> dict[str, float]:
|
|
|
|
"""
|
2022-10-24 12:48:20 +02:00
|
|
|
Calculate a file's VMAF score. Higher is better
|
2022-10-24 10:25:40 +02:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
outputfile (str): Path to output file
|
|
|
|
inputfile (str): Path to input file
|
|
|
|
|
|
|
|
Return:
|
|
|
|
dict[str, float]: VMAF mean and min value
|
|
|
|
"""
|
|
|
|
ff = ffmpy.FFmpeg(
|
|
|
|
inputs=OrderedDict([(outputfile, None), (inputfile, None)]),
|
|
|
|
outputs={
|
|
|
|
"-": "-filter_complex libvmaf=log_fmt=json:n_threads={cputhreads}:log_path=vmaf.json -f null".format(
|
|
|
|
cputhreads=cpu_count()
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ff.run()
|
|
|
|
|
|
|
|
with open("vmaf.json", "r") as file:
|
|
|
|
vmafall = json.load(file)
|
|
|
|
|
|
|
|
vmaf: dict[str, float] = {
|
|
|
|
"mean": vmafall["pooled_metrics"]["vmaf"]["mean"],
|
|
|
|
"min": vmafall["pooled_metrics"]["vmaf"]["min"],
|
|
|
|
}
|
|
|
|
|
|
|
|
return vmaf
|
|
|
|
|
|
|
|
|
2022-10-24 21:26:45 +02:00
|
|
|
def parse_kv_files(inputfile: str) -> list[dict[str, Any]]:
|
|
|
|
# create list of dicts. Each dict is one line in the file
|
|
|
|
lines: list[dict[str, Any]] = []
|
|
|
|
# Steps to get mse value
|
|
|
|
with open(inputfile) as file:
|
|
|
|
for line in file:
|
|
|
|
linelist = line.rstrip().split()
|
|
|
|
# NOTE stripping the last item because ssim has values like this in the location: (26.088579)
|
|
|
|
# For PSNR, psnr_v:87.80 is affected. (YUV color space). Not really a priority to fix atm
|
|
|
|
linedict = dict(kv.split(":") for kv in linelist[:-1])
|
|
|
|
lines.append(linedict)
|
|
|
|
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
|
|
def score_ssim(outputfile: str, inputfile: str):
|
2022-10-24 12:41:45 +02:00
|
|
|
"""
|
2022-10-24 21:26:45 +02:00
|
|
|
Calculate a file's SSIM rating. TBD
|
2022-10-24 12:41:45 +02:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
outputfile (str): Path to output file
|
|
|
|
inputfile (str): Path to input file
|
|
|
|
|
|
|
|
Return:
|
|
|
|
TBD
|
|
|
|
"""
|
|
|
|
ff = ffmpy.FFmpeg(
|
|
|
|
inputs=OrderedDict([(outputfile, None), (inputfile, None)]),
|
2022-10-24 21:26:45 +02:00
|
|
|
outputs={"-": "-lavfi ssim=stats_file=ssim.log -f null"},
|
2022-10-24 12:41:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
ff.run()
|
|
|
|
|
2022-10-24 21:26:45 +02:00
|
|
|
# get average ssim value
|
|
|
|
ssim: list[dict[str, Any]] = parse_kv_files("ssim.log")
|
2022-10-24 12:48:20 +02:00
|
|
|
|
2022-10-24 21:26:45 +02:00
|
|
|
# add all ssim_avg values together
|
|
|
|
ssim_tot: float = 0.0
|
|
|
|
for i in ssim:
|
|
|
|
ssim_tot += float(i["All"])
|
2022-10-24 12:48:20 +02:00
|
|
|
|
2022-10-24 21:26:45 +02:00
|
|
|
# get mse average
|
|
|
|
ssim_avg: float = ssim_tot / len(ssim)
|
|
|
|
|
|
|
|
return ssim_avg
|
|
|
|
|
|
|
|
|
|
|
|
def score_psnr(outputfile: str, inputfile: str) -> float:
|
2022-10-24 12:48:20 +02:00
|
|
|
"""
|
2022-10-24 21:26:45 +02:00
|
|
|
Calculate a file's MSE (mean-square error) using PSNR. A lower value is better
|
2022-10-24 12:48:20 +02:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
outputfile (str): Path to output file
|
|
|
|
inputfile (str): Path to input file
|
|
|
|
|
|
|
|
Return:
|
|
|
|
TBD
|
|
|
|
"""
|
|
|
|
ff = ffmpy.FFmpeg(
|
|
|
|
inputs=OrderedDict([(outputfile, None), (inputfile, None)]),
|
2022-10-24 21:26:45 +02:00
|
|
|
outputs={"-": "-lavfi psnr=stats_file=psnr.log -f null"},
|
|
|
|
# outputs={"-": "-filter_complex psnr=stats_file=psnr.log -f null"},
|
2022-10-24 12:48:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
ff.run()
|
|
|
|
|
2022-10-24 21:26:45 +02:00
|
|
|
# get average mse value
|
|
|
|
psnr: list[dict[str, Any]] = parse_kv_files("psnr.log")
|
|
|
|
|
|
|
|
# add all mse_avg values together
|
|
|
|
mse_tot: float = 0.0
|
|
|
|
for i in psnr:
|
|
|
|
mse_tot += float(i["mse_avg"])
|
|
|
|
|
|
|
|
# get mse average
|
|
|
|
mse_avg: float = mse_tot / len(psnr)
|
|
|
|
|
|
|
|
return mse_avg
|
2022-10-24 12:48:20 +02:00
|
|
|
|
2022-10-24 12:41:45 +02:00
|
|
|
|
2022-10-23 20:01:10 +02:00
|
|
|
if __name__ == "__main__":
|
2022-11-09 19:51:31 +01:00
|
|
|
if not os.path.isdir("encodes"):
|
|
|
|
os.mkdir("encodes")
|
2022-10-23 20:01:10 +02:00
|
|
|
|
2022-10-23 20:56:13 +02:00
|
|
|
datafile = version + "-data-" + str(now()) + ".csv"
|
2022-10-23 20:01:10 +02:00
|
|
|
|
|
|
|
with open(datafile, "w", newline="") as file:
|
|
|
|
write = csv.writer(file)
|
|
|
|
write.writerow(
|
|
|
|
(
|
|
|
|
"Codec",
|
|
|
|
"CRF",
|
|
|
|
"Preset",
|
|
|
|
"Input file",
|
|
|
|
"Output file size (MiB)",
|
|
|
|
"Encode time (s)",
|
|
|
|
"VMAF Score (mean)",
|
|
|
|
"VMAF Score (min)",
|
2022-10-24 21:26:45 +02:00
|
|
|
"SSIM Score",
|
|
|
|
"MSE Score",
|
2022-10-23 20:01:10 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-11-09 19:48:42 +01:00
|
|
|
inputfile = "source/Sparks_in_Blender.webm"
|
2022-10-24 10:08:30 +02:00
|
|
|
|
2022-10-23 20:01:10 +02:00
|
|
|
for codec in encoding:
|
|
|
|
for crf in encoding[codec]["crf"]:
|
|
|
|
for preset in encoding[codec]["presets"]:
|
2022-11-09 19:52:25 +01:00
|
|
|
# TODO selection of output location with arguments?
|
2022-10-23 20:01:10 +02:00
|
|
|
outputfile = os.path.join(
|
2022-11-09 19:51:31 +01:00
|
|
|
"encodes",
|
2022-10-23 20:01:10 +02:00
|
|
|
(
|
|
|
|
"Sparks_in_Blender-codec_"
|
|
|
|
+ codec
|
|
|
|
+ "-crf_"
|
|
|
|
+ str(crf)
|
|
|
|
+ "-preset_"
|
|
|
|
+ str(preset)
|
|
|
|
+ ".mkv"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# libaom needs additional options
|
|
|
|
if codec == "libaom-av1":
|
2022-10-24 10:08:30 +02:00
|
|
|
ff = encode_libaom(
|
|
|
|
inputfile=inputfile,
|
|
|
|
outputfile=outputfile,
|
|
|
|
crf=crf,
|
|
|
|
preset=preset,
|
2022-10-23 20:01:10 +02:00
|
|
|
)
|
|
|
|
else:
|
2022-10-24 10:08:30 +02:00
|
|
|
ff = encode_general(
|
|
|
|
inputfile=inputfile,
|
|
|
|
outputfile=outputfile,
|
|
|
|
codec=codec,
|
|
|
|
crf=crf,
|
|
|
|
preset=preset,
|
2022-10-23 20:01:10 +02:00
|
|
|
)
|
|
|
|
|
2022-10-24 10:25:40 +02:00
|
|
|
# execute previously defined encoding settings
|
2022-10-23 20:01:10 +02:00
|
|
|
starttime = now()
|
|
|
|
ff.run()
|
|
|
|
endtime = now()
|
|
|
|
difftime = int(endtime - starttime)
|
|
|
|
|
|
|
|
outputfilesize = os.path.getsize(outputfile) / 1024 / 1024
|
|
|
|
|
2022-10-24 10:25:40 +02:00
|
|
|
vmaf = score_vmaf(outputfile=outputfile, inputfile=inputfile)
|
2022-10-23 20:01:10 +02:00
|
|
|
|
2022-10-24 21:26:45 +02:00
|
|
|
ssim = score_ssim(outputfile=outputfile, inputfile=inputfile)
|
|
|
|
|
|
|
|
mse = score_psnr(outputfile=outputfile, inputfile=inputfile)
|
|
|
|
|
2022-10-23 20:01:10 +02:00
|
|
|
write_line(
|
|
|
|
codec=codec,
|
|
|
|
crf=crf,
|
|
|
|
preset=preset,
|
|
|
|
infile="Sparks_in_Blender.webm",
|
|
|
|
outfilesize=outputfilesize,
|
|
|
|
enctime=difftime,
|
2022-10-24 10:25:40 +02:00
|
|
|
vmafmean=vmaf["mean"],
|
|
|
|
vmafmin=vmaf["min"],
|
2022-10-24 21:26:45 +02:00
|
|
|
ssim=ssim,
|
|
|
|
mse=mse,
|
2022-10-23 20:01:10 +02:00
|
|
|
)
|
2022-10-23 20:10:17 +02:00
|
|
|
|
2022-11-09 19:48:49 +01:00
|
|
|
# TODO make removal optional/togglable with cli switch
|
2022-10-23 20:10:17 +02:00
|
|
|
os.remove(outputfile)
|