DEV Community

Nguyen Bin
Nguyen Bin

Posted on

Two-Pass Loudnorm in Python + FFmpeg: Hit Broadcast -14 LUFS Without Background Pumping

If you have ever run FFmpeg's single-pass loudnorm filter on a talking-head video, podcast, or voiceover, you have probably heard the dreaded background pumping effect: every time the speaker pauses for half a second, room tone and AC hum swell up like an airplane cabin, then slam back down on the next syllable.

Why? Because single-pass loudnorm runs in dynamic mode, constantly adjusting gain frame-by-frame.

The fix takes ~40 lines of zero-dependency Python: run Pass 1 to measure your track's true integrated loudness (input_i, input_lra, input_tp, input_thresh, target_offset) as JSON, then feed those exact measurements into Pass 2 with linear=true for transparent, zero-pumping broadcast loudness.


Target Standards Quick Reference

Platform / Use Case Target Integrated (I) True Peak (TP) Loudness Range (LRA)
YouTube / Shorts / Reels / TikTok -14.0 LUFS -1.5 dBTP 11 LU
Apple Podcasts / Spotify (Stereo) -16.0 LUFS -1.5 dBTP 7 LU
Mono Spoken Podcast -19.0 LUFS -1.5 dBTP 7 LU

Complete Zero-Dependency Python Script (two_pass_loudnorm.py)

Works on any .wav, .mp3, or .mp4 (copies the video stream losslessly with -c:v copy if you pass a video file!):

import json
import re
import subprocess
import sys
from pathlib import Path

TARGET_I = -14.0    # -14.0 for YouTube/Shorts, -16.0 for stereo podcasts
TARGET_TP = -1.5    # True peak ceiling in dBTP
TARGET_LRA = 11.0   # Loudness range target


def measure_loudness(src: Path) -> dict:
    cmd = [
        "ffmpeg", "-hide_banner", "-i", str(src),
        "-af", f"loudnorm=I={TARGET_I}:TP={TARGET_TP}:LRA={TARGET_LRA}:print_format=json",
        "-f", "null", "-",
    ]
    proc = subprocess.run(cmd, capture_output=True, text=True, check=True)
    # FFmpeg writes loudnorm JSON stats to stderr
    match = re.search(r"\{\s*\"input_i\"[\s\S]*?\}", proc.stderr)
    if not match:
        raise RuntimeError("Could not parse FFmpeg loudnorm JSON output.")
    return json.loads(match.group(0))


def apply_linear_master(src: Path, dst: Path, stats: dict) -> None:
    af = (
        f"loudnorm=I={TARGET_I}:TP={TARGET_TP}:LRA={TARGET_LRA}:"
        f"measured_I={stats['input_i']}:"
        f"measured_LRA={stats['input_lra']}:"
        f"measured_TP={stats['input_tp']}:"
        f"measured_thresh={stats['input_thresh']}:"
        f"offset={stats['target_offset']}:"
        f"linear=true:print_format=summary"
    )
    is_video = src.suffix.lower() in {".mp4", ".mov", ".mkv"}
    cmd = ["ffmpeg", "-y", "-hide_banner", "-i", str(src), "-af", af]
    if is_video:
        cmd += ["-c:v", "copy", "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart"]
    else:
        cmd += ["-ar", "48000", "-c:a", "pcm_s24le"]
    cmd.append(str(dst))
    subprocess.run(cmd, check=True)


if __name__ == "__main__":
    src = Path(sys.argv[1])
    dst = src.with_stem(f"{src.stem}_mastered")
    stats = measure_loudness(src)
    print(f"Measured: {stats['input_i']} LUFS | Peak: {stats['input_tp']} dBTP | Offset: {stats['target_offset']} dB")
    apply_linear_master(src, dst, stats)
    print(f"Saved clean linear master -> {dst}")
Enter fullscreen mode Exit fullscreen mode

3 Subtle Gotchas That Trip Up Production Pipelines

  1. Always check normalization_type in the Pass-2 summary: If a recording has an extreme dynamic spike (like a sudden clap or mic bump 15 dB louder than speech), loudnorm will silently fall back from linear to dynamic mode to protect the -1.5 dBTP ceiling. Placing a gentle soft limiter or highpass=f=75,acompressor=threshold=-18dB:ratio=2.5:attack=10:release=120 before Pass 1 keeps Pass 2 locked cleanly in true linear mode!
  2. Don't ignore offset={stats['target_offset']}: Skipping target_offset leaves your final output off by 0.3–1.2 LU from target on short clips. Passing all 5 measured keys guarantees dead-on accuracy within ±0.1 LU.
  3. Sample Rate Safety (-ar 48000): Internally, FFmpeg's loudnorm upsamples to 192 kHz to calculate true inter-sample peaks. Always pin -ar 48000 (or 44100) on output so you don't accidentally ship giant 192 kHz WAV files!

Want a Turnkey Offline Creator & Dev Toolkit?

Feel free to copy and modify the script above for any personal or client project! If you'd like a ready-to-run ZIP containing a batch 9:16 FFmpeg reframer, 12 copy-paste FFmpeg creator recipes, and our 20KB zero-dependency Offline HTML PDF Invoice Generator, you can grab the Offline Dev & Creator Starter Code Kit ($2.99 one-time, zero subscriptions) here:

👉 Offline Dev & Creator Starter Code Kit ($2.99 on Payhip)

Shout-out to everyone who brought up two-pass loudnorm on my last FFmpeg post — what other audio/video automation pain points are you scripting with Python right now?

Top comments (0)