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
  • layout: The output channel layout (e.g., stereo, mono, 5.1) or the specific number of output channels (e.g., 2c, 6c).
  • outputs_definition: A list of specifications detailing how each output channel is constructed from the input channels, separated by commas or semicolons.

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

  • Gain Values: If the sum of the input channel gains for an output channel is greater than 1.0, FFmpeg may clip the audio. You can scale down the coefficients (e.g., 0.5*c0 + 0.5*c1) to prevent distortion.
  • Channel Order: Input channels are indexed sequentially starting from c0 based on their order in the input stream.
  • Output Specifications: Always separate each output channel definition with a pipe symbol (|) within the filter argument.