Compare commits

..

4 Commits

Author SHA1 Message Date
929fdf2466 Add gitignore 2022-10-03 21:40:13 +02:00
5406d84fd7 Convert function 2022-10-03 21:39:53 +02:00
c84c927cf2 Importing json 2022-10-03 21:39:38 +02:00
983c63a3f3 Finish loudness_info to return dict 2022-10-03 21:39:19 +02:00
2 changed files with 58 additions and 9 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.DS_Store
.idea
*.log
tmp/

61
main.py
View File

@ -10,6 +10,9 @@ import argparse
# executing some commands # executing some commands
import subprocess import subprocess
# parsing json output of loudnorm
import json
""" """
parser = argparse.ArgumentParser(description="") parser = argparse.ArgumentParser(description="")
@ -31,10 +34,11 @@ ffmpeg -i $FILE -c:v libx264 -b:v 4000k -pass 2 -filter:a loudnorm=linear=true:m
inputfile = ( inputfile = (
'/home/marc/Downloads/FalKKonE - 01 Aria (From "Berserk: The Golden Age Arc").flac' '/home/marc/Downloads/FalKKonE - 01 Aria (From "Berserk: The Golden Age Arc").flac'
) )
inputfile = "/home/marc/Downloads/test441.opus" # inputfile = "/home/marc/Downloads/test441.opus"
outputfile = "/home/marc/Downloads/test441_out.opus"
def get_format(inputfile): def get_format(inputfile) -> str:
# get codec format # get codec format
# https://stackoverflow.com/a/29610897 # https://stackoverflow.com/a/29610897
# this shows the codecs of all audio streams present in the file, which shouldn't matter unless you have more than one stream # this shows the codecs of all audio streams present in the file, which shouldn't matter unless you have more than one stream
@ -62,7 +66,7 @@ def get_format(inputfile):
return format return format
def loudness_info(inputfile): def loudness_info(inputfile) -> dict[str, str]:
# get format from file # get format from file
# inputformat = get_format(inputfile) # inputformat = get_format(inputfile)
# NOTE format is actually unnecessary here # NOTE format is actually unnecessary here
@ -72,13 +76,52 @@ def loudness_info(inputfile):
global_options=("-y"), global_options=("-y"),
) )
# print(ff.cmd)
proc = subprocess.Popen(
ff.cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE
)
# NOTE get loudness info from subprocess
# rstrip: remove trailing newline
# decode: convert from binary string to utf8
# splitlines: list of lines (only 12 last ones, length of the output json)
# join: reassembles the list of lines and separates with "\n"
loudness_json: str = "\n".join(
proc.stdout.read().rstrip().decode("utf8").splitlines()[-12:]
)
# decode json to dict
loudness: dict[str, str] = json.loads(loudness_json)
# print(loudness_json)
return loudness
def convert(inputfile, outputfile, loudness):
ff = ffmpy.FFmpeg(
inputs={inputfile: None},
outputs={
outputfile: "-pass 2"
" "
"-filter:a"
" "
"loudnorm=I=-30.0:"
"measured_I={input_i}:"
"measured_LRA={input_lra}:"
"measured_tp={input_tp}:measured_thresh={input_thresh}:"
"print_format=json"
" "
"-c:a libopus"
" "
"-b:a 320k".format(
input_i=loudness["input_i"],
input_lra=loudness["input_lra"],
input_tp=loudness["input_tp"],
input_thresh=loudness["input_thresh"],
)
},
)
print(ff.cmd) print(ff.cmd)
ff.run()
def convert():
# ff = ffmpy.FFmpeg()
pass
if __name__ == "__main__": if __name__ == "__main__":
loudness_info(inputfile) loudness = loudness_info(inputfile=inputfile)
convert(inputfile=inputfile, outputfile=outputfile, loudness=loudness)