WIP move encoding to separate functions

This commit is contained in:
RealStickman 2022-10-24 10:08:30 +02:00
parent 828f2a275d
commit f07ccc0efd

View File

@ -94,6 +94,54 @@ def write_line(
) )
def encode_general(inputfile: str, outputfile: str, codec: str, crf: str, preset: str):
"""
General encoding function
Parameters:
inputfile (str): Path to input file
outputfile (str): Path to output file
codec (str): Codec used
crf (str): CRF value
preset (str): 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: str, preset: str):
"""
Encoding with libaom
Parameters:
inputfile (str): Path to input file
outputfile (str): Path to output file
crf (str): CRF value
preset (str): 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
if __name__ == "__main__": if __name__ == "__main__":
if not os.path.isdir("encoded"): if not os.path.isdir("encoded"):
os.mkdir("encoded") os.mkdir("encoded")
@ -115,6 +163,8 @@ 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"]:
@ -133,27 +183,20 @@ if __name__ == "__main__":
# libaom needs additional options # libaom needs additional options
if codec == "libaom-av1": if codec == "libaom-av1":
ff = ffmpy.FFmpeg( ff = encode_libaom(
inputs={"Sparks_in_Blender.webm": None}, inputfile=inputfile,
outputs={ outputfile=outputfile,
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 = ffmpy.FFmpeg( ff = encode_general(
inputs={"Sparks_in_Blender.webm": None}, inputfile=inputfile,
outputs={ outputfile=outputfile,
outputfile: "-c:v {videocodec} -crf {crf} -preset {preset} -g 240 -map 0:v:0 ".format( codec=codec,
videocodec=codec,
crf=crf, crf=crf,
preset=preset, preset=preset,
) )
},
)
starttime = now() starttime = now()
ff.run() ff.run()
@ -162,7 +205,7 @@ if __name__ == "__main__":
outputfilesize = os.path.getsize(outputfile) / 1024 / 1024 outputfilesize = os.path.getsize(outputfile) / 1024 / 1024
ffvmaf = ffmpy.FFmpeg( ff = ffmpy.FFmpeg(
inputs=OrderedDict( inputs=OrderedDict(
[(outputfile, None), ("Sparks_in_Blender.webm", None)] [(outputfile, None), ("Sparks_in_Blender.webm", None)]
), ),
@ -173,7 +216,7 @@ if __name__ == "__main__":
}, },
) )
ffvmaf.run() ff.run()
with open("vmaf.json", "r") as file: with open("vmaf.json", "r") as file:
vmaf = json.load(file) vmaf = json.load(file)