Ffmpeg Audio Spatial
Context: $ARGUMENTS
Quick start
- 5.1/7.1 → binaural headphones: Step 1 (probe layout) → Step 2 (pick
headphoneorsofalizer) → Step 3 (build filter, ClockWise channel order) - Stereo → 5.1 upmix: Step 1 (probe) → Step 2 (pick
surround) → Step 3 (chl_out=5.1) - Stereo widen / crossfeed / earwax: Step 2 (pick stereo FX) → Step 3 (one-liner
-af) - Split / remap / join channels: Step 2 (pick
channelsplit/pan/amerge) → Step 3 (build graph)
When to use
- You need a binaural headphone downmix from a 5.1/7.1 film or game mix.
- You are given a stereo master and asked to "make it wider," "add space," "reduce ping-pong fatigue on earbuds," or "trick a stereo file into 5.1."
- You need to split a multichannel master into one-mono-per-channel files (stem prep).
- You need to remap, reroute, or merge separate mono tracks into a laid-out multichannel bus.
Step 1 — Identify the channel layout
Every spatial op depends on the input layout. Probe first:
ffprobe -v error -select_streams a:0 \
-show_entries stream=channels,channel_layout,sample_rate \
-of default=nw=1 INPUT
Interpret:
channels=2 channel_layout=stereo→ stereo-only operations (widen, earwax, crossfeed, upmix).channels=6 channel_layout=5.1(or5.1(side)) → binaural, downmix, or split.channels=8 channel_layout=7.1→ binaural (extend channel map) or split.channels=1 channel_layout=mono→ duplicate to stereo first, or upmix.- Layout missing? Use
scripts/spatial.py detect— it falls back to FFmpeg's layout table and warns when the layout is unset so you can force it with-channel_layouton input.
Step 2 — Pick the operation
| Goal | Filter | Notes |
| --- | --- | --- |
| 5.1/7.1 → binaural (built-in HRTF) | headphone | Channels must be listed in ClockWise order (see Gotchas). |
| 5.1/7.1 → binaural (custom HRTF) | sofalizer | Needs --enable-libmysofa build flag + a .sofa file. |
| Stereo → 5.1/7.1 upmix | surround | Invents center/surrounds — sounds plausible, not true 5.1. |
| Stereo widen | stereowiden | Delay+feedback — physical widening. |
| Boost stereo difference | extrastereo | Amplifies L-R, can exaggerate artifacts. |
| M/S stereo tools | stereotools | Mid/Side level, phase, mute-L/mute-R. |
| Headphone listening fatigue | crossfeed | Blends L/R back — opposite of extrastereo. |
| 2-ch CD → headphone cues | earwax | Fixed 44.1 kHz stereo only. |
| Split channels to streams | channelsplit | Produces N mono pads. |
| Remap channel layout | channelmap or pan | pan is expression-based. |
| Join N monos into a layout | amerge + pan | amerge interleaves; pan assigns names. |
Step 3 — Build the filter
5.1 → binaural stereo (built-in HRTF)
ffmpeg -i in.mkv -af "headphone=FL|FR|FC|LFE|BL|BR" -c:a libmp3lame -q:a 2 out.mp3
7.1 → binaural (ClockWise: FL FR FC LFE BL BR SL SR)
ffmpeg -i in.mkv -af "headphone=FL|FR|FC|LFE|BL|BR|SL|SR" -c:a aac -b:a 192k out.m4a
5.1 → binaural with custom SOFA
ffmpeg -i in.mkv -af "sofalizer=sofa=mit_kemar.sofa:type=freq:radius=1" out.flac
Stereo → 5.1 upmix
ffmpeg -i stereo.wav -af "surround=chl_out=5.1" -c:a ac3 -b:a 448k surround.ac3
Stereo widen
ffmpeg -i in.wav -af "stereowiden=delay=20:feedback=0.3:crossfeed=0.3:drymix=0.8" out.wav
Extra-stereo (L-R amplification)
ffmpeg -i in.wav -af "extrastereo=m=2.5" out.wav
Crossfeed (headphone fatigue reducer)
ffmpeg -i in.wav -af "crossfeed=strength=0.5:range=0.5" out.wav
Earwax (CD-stereo → headphone cues)
ffmpeg -i in.wav -af "earwax" out.wav
Stereotools M/S matrix
ffmpeg -i in.wav -af "stereotools=mlev=0.8:slev=1.2" out.wav
Split 5.1 into six mono WAVs
ffmpeg -i in.mkv -filter_complex \
"[0:a]channelsplit=channel_layout=5.1[FL][FR][FC][LFE][BL][BR]" \
-map "[FL]" FL.wav -map "[FR]" FR.wav -map "[FC]" FC.wav \
-map "[LFE]" LFE.wav -map "[BL]" BL.wav -map "[BR]" BR.wav
Remap — stereo → mono (left only)
ffmpeg -i in.wav -af "pan=mono|c0=c0" left.wav
# equivalent via channelmap:
ffmpeg -i in.wav -af "channelmap=channel_layout=mono:map=FL-FC" left.wav
Remap — mono → stereo (duplicate)
ffmpeg -i in.wav -af "pan=stereo|c0=c0|c1=c0" stereo.wav
# equivalent via channelmap:
ffmpeg -i in.wav -af "channelmap=channel_layout=stereo:map=0-FL|0-FR" stereo.wav
Join two mono files into stereo
ffmpeg -i L.wav -i R.wav -filter_complex \
"[0:a][1:a]amerge=inputs=2,pan=stereo|c0=c0|c1=c1[a]" \
-map "[a]" stereo.wav
Step 4 — Verify the output layout
Always re-probe the result:
ffprobe -v error -select_streams a:0 \
-show_entries stream=channels,channel_layout -of default=nw=1 OUT
- Binaural should report
channels=2 channel_layout=stereo. - Upmix should report
channels=6 channel_layout=5.1. - If
channel_layout=unknown, add-channel_layouton input or useaformat=channel_layouts=...before encoding.
Available scripts
scripts/spatial.py— Stdlib-only helper withdetect,binaural,upmix,widen,split,remap,joinsubcommands. Prints the exact ffmpeg command, supports--dry-runand--verbose.
Examples:
uv run ${CLAUDE_SKILL_DIR}/scripts/spatial.py detect --input movie.mkv
uv run ${CLAUDE_SKILL_DIR}/scripts/spatial.py binaural --input movie.mkv --output bin.m4a
uv run ${CLAUDE_SKILL_DIR}/scripts/spatial.py binaural --input movie.mkv --output bin.flac --sofa mit_kemar.sofa
uv run ${CLAUDE_SKILL_DIR}/scripts/spatial.py upmix --input stereo.wav --output surround.ac3 --layout 5.1
uv run ${CLAUDE_SKILL_DIR}/scripts/spatial.py widen --input in.wav --output wide.wav --strength 0.5
uv run ${CLAUDE_SKILL_DIR}/scripts/spatial.py split --input in.mkv --output-pattern "ch_%d.wav"
uv run ${CLAUDE_SKILL_DIR}/scripts/spatial.py remap --input in.wav --output out.wav --map "mono:c0=0.5*c0+0.5*c1"
uv run ${CLAUDE_SKILL_DIR}/scripts/spatial.py join --inputs L.wav R.wav --output stereo.wav --layout stereo