Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Normalizes the audio of an input file into an output file.
#!/bin/bash
# Normalizes the audio of the input to the output.
if [ "$1" = "-h" ]; then
echo "Normalizes the audio of the input into the output."
echo "normalize.sh [input] [output=normalized.ext]"
exit 0
fi
normalize() {
tempFile="$(mktemp)"
output="normalized"
if [ "$2" != "" ]; then
output="$2"
echo "Normalizing $1 to $2"
else
filename=$(basename -- "$1")
extension="${filename##*.}"
output="$output.$extension"
echo "$2 -- $filename -- $extension -- $output"
echo "Converting $1 using the default filename $output"
fi
echo "Getting audio information..."
ffmpeg -i "$1" -af loudnorm=I=-16:TP=-1.5:LRA=11:print_format=summary -f null - 2> $tempFile
integrated="$(cat $tempFile | grep 'Input Integrated:' | grep -oP '[-+]?[0-9]+.[0-9]')"
truepeak="$(cat $tempFile | grep 'Input True Peak:' | grep -oP '[-+]?[0-9]+.[0-9]')"
lra="$(cat $tempFile | grep 'Input LRA:' | grep -oP '[-+]?[0-9]+.[0-9]')"
threshold="$(cat $tempFile | grep 'Input Threshold:' | grep -oP '[-+]?[0-9]+.[0-9]')"
echo "Integrated = $integrated :: True Peak = $truepeak :: LRA = $lra :: Threshold = $threshold"
echo "Re-encoding using audio analysis..."
ffmpeg -i "$1" -loglevel panic -af loudnorm=I=-16:TP=-1.5:LRA=11:measured_I=$integrated:measured_TP=$truepeak:measured_LRA=$lra:measured_thresh=$threshold:offset=-0.3:linear=true:print_format=summary "$output"
echo "Cleaning up..."
rm $tempFile
echo "Done! $output has been normalized."
}
if [ -d "$1" ]; then
echo "Converting directory $1..."
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for fullfilename in $(find "$1" -iname "*.*"); do
filename=$(basename -- "$fullfilename")
extension="${filename##*.}"
filename="${filename%.*}"
normalized="$1/$filename.normalized.$extension"
normalize "$fullfilename" "$normalized"
done
else
normalize "$1" "$2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.