Use FFmpeg Pan Filter to Route Audio Channels
The FFmpeg pan audio filter is a powerful tool used for
routing, remapping, mixing, and splitting audio channels in media files.
This article provides a direct, practical guide on how to use the
pan filter, explaining its basic syntax, channel mapping
rules, and common real-world examples such as swapping stereo channels,
downmixing multi-channel audio, and isolating specific tracks.
Understanding the Pan Filter Syntax
The basic syntax for the pan filter is as follows:
-af "pan=layout|outputs"layout: The output channel configuration (e.g.,mono,stereo,5.1, or a specific number of channels like1cor2c).outputs: The definitions of how the input channels are routed to the output channels. Each output definition is separated by a semicolon or a space.
Output channels are labeled from left to right as c0,
c1, c2, etc. Input channels are also
referenced as c0, c1, c2, etc.,
based on their order in the input stream. Alternatively, you can use
named descriptors (like FL for front left, FR
for front right, FC for front center) if the input has a
defined layout.
Practical Examples of Audio Routing
1. Swapping Left and Right Stereo Channels
To swap the left and right channels of a stereo audio track, map the
first input channel (c0) to the second output channel
(c1), and the second input channel (c1) to the
first output channel (c0).
ffmpeg -i input.mp4 -af "pan=stereo|c0=c1|c1=c0" output.mp42. Downmixing Stereo to Mono
To mix both the left and right channels of a stereo track into a single mono channel, combine them at equal volume (0.5 or 50% each) to prevent clipping.
ffmpeg -i input.mp4 -af "pan=mono|c0=0.5*c0+0.5*c1" output.mp43. Extracting a Single Channel
If you have a stereo file but only want to keep the left channel and output it as mono, assign the first input channel to the single mono output.
ffmpeg -i input.mp4 -af "pan=mono|c0=c0" output.mp4To extract only the right channel to mono:
ffmpeg -i input.mp4 -af "pan=mono|c0=c1" output.mp44. Splitting Mono to Stereo (Dual Mono)
To duplicate a single mono channel into both the left and right
channels of a stereo output, route the single input channel
(c0) to both output channels (c0 and
c1).
ffmpeg -i input.mp4 -af "pan=stereo|c0=c0|c1=c0" output.mp45. Routing Specific Channels in a Multi-Channel File
If you have a 5.1 surround sound input (6 channels) and want to
create a stereo output containing only the Front Left (c0)
and Front Right (c1) channels, discarding the center, LFE,
and surround channels, use:
ffmpeg -i input.mp4 -af "pan=stereo|c0=c0|c1=c1" output.mp4To custom mix a 5.1 stream into stereo while retaining the center
(c2) and surround channels (c4 and
c5), use a weighted mix:
ffmpeg -i input.mp4 -af "pan=stereo|c0=1.0*c0+0.707*c2+0.707*c4|c1=1.0*c1+0.707*c2+0.707*c5" output.mp4Important Usage Tips
- Volume Normalization: When mixing multiple input
channels into a single output channel, the sum of the volume
coefficients should generally equal
1.0(e.g.,0.5*c0 + 0.5*c1). Exceeding1.0can cause digital clipping and audio distortion. - Audio Codecs: Ensure you specify an audio codec
(like
-c:a aacor-c:a pcm_s16le) when exporting your file to ensure the routed audio is correctly encoded.