Compare commits
No commits in common. "bd241c3b6fbcd3d80bc4bb39fd018671f48dc76f" and "828f2a275d8a6ba34db65f1464b9bfa41708ea60" have entirely different histories.
bd241c3b6f
...
828f2a275d
@ -14,10 +14,8 @@ from collections import OrderedDict
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from typing import Union, Any
|
|
||||||
|
|
||||||
# encoding options used
|
# encoding options used
|
||||||
encoding: dict[str, Any] = {
|
encoding = {
|
||||||
"libx264": {
|
"libx264": {
|
||||||
"crf": [10, 14, 16, 18, 20, 22, 25],
|
"crf": [10, 14, 16, 18, 20, 22, 25],
|
||||||
"presets": [
|
"presets": [
|
||||||
@ -59,8 +57,8 @@ def now():
|
|||||||
|
|
||||||
def write_line(
|
def write_line(
|
||||||
codec: str,
|
codec: str,
|
||||||
crf: int,
|
crf: str,
|
||||||
preset: Union[str, int],
|
preset: str,
|
||||||
infile: str,
|
infile: str,
|
||||||
outfilesize: float,
|
outfilesize: float,
|
||||||
enctime: int,
|
enctime: int,
|
||||||
@ -72,8 +70,8 @@ def write_line(
|
|||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
codec (str): Codec used
|
codec (str): Codec used
|
||||||
crf (int): CRF used
|
crf (str): CRF used
|
||||||
preset (str/int): Preset used
|
preset (str): Preset used
|
||||||
infile (str): Input file name
|
infile (str): Input file name
|
||||||
outfilesize (float): Size of output file
|
outfilesize (float): Size of output file
|
||||||
enctime (int): Time to encode
|
enctime (int): Time to encode
|
||||||
@ -96,89 +94,6 @@ def write_line(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def encode_general(
|
|
||||||
inputfile: str, outputfile: str, codec: str, crf: int, preset: Union[str, int]
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
General encoding function
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
inputfile (str): Path to input file
|
|
||||||
outputfile (str): Path to output file
|
|
||||||
codec (str): Codec used
|
|
||||||
crf (int): CRF value
|
|
||||||
preset (str/int): Choosen preset
|
|
||||||
"""
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def encode_libaom(inputfile: str, outputfile: str, crf: int, preset: Union[str, int]):
|
|
||||||
"""
|
|
||||||
Encoding with libaom
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
inputfile (str): Path to input file
|
|
||||||
outputfile (str): Path to output file
|
|
||||||
crf (int): CRF value
|
|
||||||
preset (str/int): Choosen preset
|
|
||||||
"""
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def score_vmaf(outputfile: str, inputfile: str) -> dict[str, float]:
|
|
||||||
"""
|
|
||||||
Calculate a file's VMAF score
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if not os.path.isdir("encoded"):
|
if not os.path.isdir("encoded"):
|
||||||
os.mkdir("encoded")
|
os.mkdir("encoded")
|
||||||
@ -200,8 +115,6 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
inputfile = "Sparks_in_Blender.webm"
|
|
||||||
|
|
||||||
for codec in encoding:
|
for codec in encoding:
|
||||||
for crf in encoding[codec]["crf"]:
|
for crf in encoding[codec]["crf"]:
|
||||||
for preset in encoding[codec]["presets"]:
|
for preset in encoding[codec]["presets"]:
|
||||||
@ -220,22 +133,28 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# libaom needs additional options
|
# libaom needs additional options
|
||||||
if codec == "libaom-av1":
|
if codec == "libaom-av1":
|
||||||
ff = encode_libaom(
|
ff = ffmpy.FFmpeg(
|
||||||
inputfile=inputfile,
|
inputs={"Sparks_in_Blender.webm": None},
|
||||||
outputfile=outputfile,
|
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,
|
crf=crf,
|
||||||
preset=preset,
|
preset=preset,
|
||||||
)
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
ff = encode_general(
|
ff = ffmpy.FFmpeg(
|
||||||
inputfile=inputfile,
|
inputs={"Sparks_in_Blender.webm": None},
|
||||||
outputfile=outputfile,
|
outputs={
|
||||||
codec=codec,
|
outputfile: "-c:v {videocodec} -crf {crf} -preset {preset} -g 240 -map 0:v:0 ".format(
|
||||||
|
videocodec=codec,
|
||||||
crf=crf,
|
crf=crf,
|
||||||
preset=preset,
|
preset=preset,
|
||||||
)
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
# execute previously defined encoding settings
|
|
||||||
starttime = now()
|
starttime = now()
|
||||||
ff.run()
|
ff.run()
|
||||||
endtime = now()
|
endtime = now()
|
||||||
@ -243,7 +162,21 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
outputfilesize = os.path.getsize(outputfile) / 1024 / 1024
|
outputfilesize = os.path.getsize(outputfile) / 1024 / 1024
|
||||||
|
|
||||||
vmaf = score_vmaf(outputfile=outputfile, inputfile=inputfile)
|
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(
|
write_line(
|
||||||
codec=codec,
|
codec=codec,
|
||||||
@ -252,8 +185,8 @@ if __name__ == "__main__":
|
|||||||
infile="Sparks_in_Blender.webm",
|
infile="Sparks_in_Blender.webm",
|
||||||
outfilesize=outputfilesize,
|
outfilesize=outputfilesize,
|
||||||
enctime=difftime,
|
enctime=difftime,
|
||||||
vmafmean=vmaf["mean"],
|
vmafmean=vmaf["pooled_metrics"]["vmaf"]["mean"],
|
||||||
vmafmin=vmaf["min"],
|
vmafmin=vmaf["pooled_metrics"]["vmaf"]["min"],
|
||||||
)
|
)
|
||||||
|
|
||||||
os.remove(outputfile)
|
os.remove(outputfile)
|
||||||
|
Loading…
Reference in New Issue
Block a user