Last active
June 27, 2020 08:24
-
-
Save maakcode/dfff9004595efde78381710cc71cfc25 to your computer and use it in GitHub Desktop.
Normalizing audio loudness using ffmpeg-python(https://github.com/kkroening/ffmpeg-python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ffmpeg | |
import json | |
import subprocess | |
import os | |
import sys | |
import time | |
def loudnorm(name, ext): | |
singlePass = subprocess.Popen( | |
( | |
ffmpeg | |
.input(f"{name}{ext}") | |
.audio.filter("loudnorm", I=-16, TP=-1.5, LRA=11, print_format="json") | |
.output("-", format="null") | |
.global_args("-loglevel", "info") | |
.global_args("-nostats") | |
.global_args("-hide_banner") | |
.compile() | |
), | |
stderr=subprocess.PIPE | |
) | |
output = "\n".join(singlePass.communicate()[-1].decode("UTF-8").split("\n")[-13:-1]) | |
data = json.loads(output) | |
measured_I = data["input_i"] | |
measured_TP = data["input_tp"] | |
measured_LRA = data["input_lra"] | |
measured_thresh = data["input_thresh"] | |
offset = data["target_offset"] | |
( | |
ffmpeg | |
.input(f"{name}{ext}") | |
.audio.filter("loudnorm", I=-16, TP=-1.5, LRA=11, measured_I=f"{measured_I}", measured_TP=f"{measured_TP}", measured_LRA=f"{measured_LRA}", measured_thresh=f"{measured_thresh}", offset=f"{offset}") | |
.output(f"output/{name}.m4a", format="mp4", acodec="aac") | |
.global_args("-ar", "48k") | |
.global_args("-vbr", "5") | |
.global_args("-loglevel", "quiet") | |
.global_args("-nostats") | |
.global_args("-hide_banner") | |
.run() | |
) | |
return | |
if len(sys.argv) < 2: | |
sys.exit("[Error] Path required!") | |
try: | |
os.chdir(sys.argv[1]) | |
except: | |
sys.exit(f"[Error] Not a directory: {sys.argv[1]}") | |
os.makedirs("./output", exist_ok=True) | |
audioFiles = [".mp3", ".m4a", ".wav", ".aiff", ".flac", ".ogg"] | |
files = os.listdir(sys.argv[1]) | |
print("ffmpeg loudnorm started") | |
for file in files: | |
name, ext = os.path.splitext(file) | |
if ext in audioFiles: | |
print(f"Processing {name}{ext}", end=" ", flush=True) | |
startTime = time.time() | |
loudnorm(name, ext) | |
endTime = time.time() | |
print(f"... Complete! ({round(endTime - startTime, 1)} sec)") | |
else: | |
print(f"{name}{ext} is not audio files. skipped.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python loudnorm.py <path to audio directory>