Fix "the volume keeps jumping around" by matching loudness, not peaks. What LUFS means, -23 LUFS (R128), and how to use loudnorm with its two measurement passes.
The sound suddenly jumping up the moment an ad comes on, or having to turn the volume up on every video, is one of the classic problems in audio production and distribution. The fix is to match not the "largest value in the waveform (the peak)" but the average size people perceive (loudness).
LUFS stands for "Loudness Units relative to Full Scale." A difference of 1 LU equals a difference of 1 dB.
Loudness measurement follows the ITU-R BS.1770 algorithm. It averages the energy of the signal after a K-weighting filter that imitates the sensitivity of the ear, and applies gating, which excludes passages that are too quiet from the calculation. The value this gives for the whole program is the integrated loudness, and this is what people mean when they say "this video is -23 LUFS."
The European Broadcasting Union's R128 specifies an integrated loudness of -23 LUFS (tolerance ±0.5 LU) and a maximum true peak of -1 dBTP for broadcast programs, and also has you report the loudness range (LRA), which describes how much the level varies. ATSC A/85, the US broadcast guideline, uses -24 LKFS as its reference. They use the same BS.1770 measurement method, but the target values differ.
Music and video platforms are known to have their own reference loudness and to turn audio up or down on playback relative to it (a figure of around -14 LUFS is often mentioned, for example). But platform policies change, so check the current guidelines of the service you are uploading to. This site does not assert platform-specific numbers.
ffmpeg has the ebur128 filter for measuring and the loudnorm filter for correcting. loudnorm's defaults are an integrated loudness target of I=-24, a loudness range of LRA=7, and a true peak of TP=-2 (from the ffmpeg filter documentation). To match R128 you have to set the values yourself.
ffmpeg -i input.mp4 -af loudnorm=I=-23:LRA=7:TP=-1:print_format=json -f null -
# Read input_i, input_lra, input_tp, input_thresh, and target_offset from the JSON at the end of the outputffmpeg -i input.mp4 \
-af "loudnorm=I=-23:LRA=7:TP=-1:\
measured_I=<input_i>:measured_LRA=<input_lra>:\
measured_TP=<input_tp>:measured_thresh=<input_thresh>:\
offset=<target_offset>:linear=true:print_format=summary" \
-ar 48000 -c:v copy -c:a aac -b:a 192k output.mp4-c:v copy.ffmpeg -nostats -i output.mp4 -filter_complex ebur128=peak=true -f null - 2>&1 | tail -n 15
# Look at Integrated loudness (I), Loudness range (LRA), and True peak in the SummaryLoudness normalization changes the overall gain; it does not make the sound any cleaner. And setting the target to -23 LUFS does not mean it will play at the same level on every platform, because a platform may readjust it to its own reference.
The loudness unit measured with BS.1770, a relative value referenced to full scale. LUFS and LKFS refer to the same value.
Loudness calculated over the whole program with gating. Usually what people mean by "this video's LUFS."
The maximum value, including peaks that occur between samples, estimated by oversampling.
Loudness Range. How widely the loudness rises and falls within a program.
The European Broadcasting Union's loudness recommendation. Specifies an integrated loudness of -23 LUFS, a true peak of -1 dBTP, and more.
ffmpeg's EBU R128-based loudness normalization filter. Supports a two-pass linear mode that takes measured values.
Why use the approach of running ffmpeg loudnorm twice (two-pass linear mode)?