Skip to content

Instantly share code, notes, and snippets.

@hdsdi3g
Created November 7, 2016 23:39
Show Gist options
  • Save hdsdi3g/184a7416751c9d45a564c371248b351f to your computer and use it in GitHub Desktop.
Save hdsdi3g/184a7416751c9d45a564c371248b351f to your computer and use it in GitHub Desktop.
Apply a loudness correction for a video file with ffmpeg and libebur128, without re-transcoding the video stream
#!/bin/sh
# This script needs ffmpeg v3.2 (--enable-libebur128) + jq
SOURCE=$1;
JSON_ANALYST_FILE="$SOURCE.json"
DEST_I="-23"
DEST_LRA="15"
DEST_TRUE_PEAK="-3"
RAW_FFMPEG_ANALYST=$(ffmpeg -hide_banner -nostats -i "$SOURCE" -af loudnorm=print_format=json -f null - 2>&1);
echo "$RAW_FFMPEG_ANALYST" | tail -12 > $JSON_ANALYST_FILE
LOUDNESS_input_i=$(jq -c -r -M .input_i "$JSON_ANALYST_FILE");
LOUDNESS_input_tp=$(jq -c -r -M .input_tp "$JSON_ANALYST_FILE");
LOUDNESS_input_lra=$(jq -c -r -M .input_lra "$JSON_ANALYST_FILE");
LOUDNESS_input_thresh=$(jq -c -r -M .input_thresh "$JSON_ANALYST_FILE");
LOUDNESS_input_offset=$(jq -c -r -M .target_offset "$JSON_ANALYST_FILE");
AF_FILTER="loudnorm=I=$DEST_I:TP=$DEST_TRUE_PEAK:LRA=$DEST_LRA:measured_I=$LOUDNESS_input_i:measured_LRA=$LOUDNESS_input_lra:measured_TP=$LOUDNESS_input_tp:measured_thresh=$LOUDNESS_input_thresh:offset=$LOUDNESS_input_offset:linear=true:print_format=summary";
ffmpeg -y -i "$SOURCE" -codec:v copy -codec:a aac -b:a 320k -af $AF_FILTER -f mov "$SOURCE-CORRECTED.mp4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment