Skip to content

Instantly share code, notes, and snippets.

@kana
Last active January 1, 2023 11:04
Show Gist options
  • Save kana/02740ae631b16f2e38298edd4fd46ab5 to your computer and use it in GitHub Desktop.
Save kana/02740ae631b16f2e38298edd4fd46ab5 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euxo pipefail
# These parameters are taken from:
# https://developers.google.com/assistant/tools/audio-loudness
TARGET_LUFS=-16
TP=-1.5
LRA=11
function analyze_lufs()
{
ffmpeg -i "$1" \
-af "
loudnorm=I=$TARGET_LUFS
:TP=$TP
:LRA=$LRA
:dual_mono=true
:print_format=json
" \
-f null - 2>&1 |
tee >(cat >&2) |
sed -n '/^{$/,/^}$/p'
}
json="$(analyze_lufs "$1")"
# Example content:
# {
# "input_i": "-24.32",
# "input_tp": "-12.34",
# "input_lra": "2.20",
# "input_thresh": "-34.33",
# "output_i": "-14.27",
# "output_tp": "-2.19",
# "output_lra": "2.10",
# "output_thresh": "-24.28",
# "normalization_type": "dynamic",
# "target_offset": "0.27"
# }
measured_i="$(echo "$json" | jq -r .input_i)"
measured_tp="$(echo "$json" | jq -r .input_tp)"
measured_lra="$(echo "$json" | jq -r .input_lra)"
measured_thresh="$(echo "$json" | jq -r .input_thresh)"
offset="$(echo "$json" | jq -r .target_offset)"
dir="$(dirname "$1")"
basename="$(basename "$1")"
ext=".${basename/*./}"
ffmpeg -i "$1" \
-c:v copy \
-af "
loudnorm=I=$TARGET_LUFS
:TP=$TP
:LRA=$LRA
:measured_I=$measured_i
:measured_TP=$measured_tp
:measured_LRA=$measured_lra
:measured_thresh=$measured_thresh
:offset=$offset
:linear=true
:print_format=summary
" \
"$dir/,${basename/$ext/}-lufs$TARGET_LUFS$ext"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment