How to Use FFmpeg Pan Filter for Audio Channels

This guide explains how to use the FFmpeg pan audio filter to map and assign specific input audio channels to precise output positions. You will learn the basic syntax of the filter, understand how channel layouts work, and explore practical command examples for common tasks like swapping stereo channels, downmixing multi-channel audio, and extracting specific channels.

Understanding the Pan Filter Syntax

The pan filter allows you to mute, route, and mix audio channels. The basic syntax for the filter is:

pan=layout|outputs_definition

Output channels can be defined using names (like FL for Front Left, FR for Front Right) or indexes (like c0, c1, c2).

Step-by-Step Configuration Examples

1. Swapping Stereo Channels

To swap the left and right channels of a stereo audio file, you define a stereo output where the first output channel (c0 or FL) receives the second input channel (c1 or FR), and vice versa:

ffmpeg -i input.mp3 -af "pan=stereo|c0=c1|c1=c0" output.mp3

Alternatively, using channel names:

ffmpeg -i input.mp3 -af "pan=stereo|FL=FR|FR=FL" output.mp3

2. Extracting a Single Channel to Mono

If you want to extract a specific channel from a multi-channel file and output it as a single mono track, you can assign that specific input channel to the single mono output (c0):

To extract the second channel (index c1):

ffmpeg -i input.wav -af "pan=mono|c0=c1" output.wav

3. Merging (Downmixing) Stereo to Mono

To combine both the left and right channels of a stereo file into a single mono channel without clipping, you can assign 50% of each input channel to the mono output:

ffmpeg -i input.mp3 -af "pan=mono|c0=0.5*c0+0.5*c1" output.mp3

4. Downmixing 5.1 Surround Sound to Stereo

When downmixing 5.1 surround sound to stereo, you need to map the front, center, subwoofer (LFE), and surround channels to the Left (FL) and Right (FR) stereo outputs. This example uses standard downmix coefficients to balance the levels:

ffmpeg -i input.mkv -af "pan=stereo|FL=FL+0.707*FC+0.707*BL|FR=FR+0.707*FC+0.707*BR" output.wav

5. Creating a Custom Multi-Channel Layout

If you need a specific number of output channels that do not fit a standard layout name, you can specify the channel count directly (e.g., 4c for four channels) and map the inputs explicitly:

ffmpeg -i input.wav -af "pan=4c|c0=c0|c1=c1|c2=c2|c3=c3" output.wav

Key Rules for the Pan Filter