Sajith Rahim | Almanac | Blog
 
Life Skill

ffmpeg : pan Filter

Sajith AR

ffmpeg pan filter: Exploring Upmixing (Part 1)

ffmpeg is a powerful framework that enables developers to handle audio, video, and other multimedia files with ease. I upgraded to a DD+ 5.1 setup recently and found myself in a situation where my music library was all 2.0 stereo which didn’t sound great in my new setup. I was exploring options (easy ones) to convert alteast some of my favourite tracks to surround.

After a few audacity tutorials and few other tools, I found ffmpef’s pan filter - which out to me as a developer.

Let’s explore pan filter - whether you’re downmixing, upmixing, or rearranging audio channels, it’s the swiss army knife you need.

What is the pan Filter?

The pan filter in FFmpeg is used to mix audio channels or create new audio channels by combining or manipulating existing ones. It allows you to define custom channel layouts and specify how input channels should be mapped to output channels. This makes it an versatile tool for tasks such as:

  • Downmixing: Reducing the number of audio channels (e.g., converting 5.1 surround sound to stereo).

  • Upmixing: Increasing the number of audio channels (e.g., converting mono to stereo).

  • Channel Rearrangement: Swapping or reordering audio channels.

  • Custom Mixing: Creating custom audio mixes by applying specific gains to individual channels.

Basic Syntax

The general syntax for the pan filter is as follows:

	
        
    
        
    
    pan=channel_layout:channel_definitions
  • channel_layout: Specifies the output channel layout (e.g., monostereo5.1, etc.).

  • channel_definitions: Defines how the input channels are mapped to the output channels.

Channel Layouts

Before diving into examples, it’s essential to understand the standard channel layouts and their corresponding channel names. You can view a list of standard channel layouts using the following command:

	
        
    
        
    
    ffmpeg -layouts

Common channel layouts include:

  • mono: 1 channel (e.g., FC for front center).

  • stereo: 2 channels (e.g., FL for front left and FR for front right).

  • 5.1: 6 channels (e.g., FLFRFCLFEBLBR).

Practical Examples

Let’s explore some practical examples to illustrate the power and flexibility of the pan filter.

1. Convert Stereo to Mono

If you have a stereo audio file and want to convert it to mono by combining both channels, you can use the following command:

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

Here, c0 and c1 represent the left and right channels of the input, respectively. The output is a mono channel that averages the two input channels.

2. Extract a Specific Channel

To extract the left channel from a stereo input, you can use:

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

This command creates a mono output containing only the left channel (c0) of the input.

3. Create a Stereo Output from Mono

If you have a mono audio file and want to create a stereo output by duplicating the mono channel, you can use:

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

This command maps the mono input (c0) to both the left (c0) and right (c1) channels of the stereo output.

4. Rearrange Channels

To swap the left and right channels in a stereo input, use the following command:

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

This command maps the left channel (c0) of the input to the right channel (c1) of the output and vice versa.

5. Downmix 5.1 to Stereo

Downmixing a 5.1 surround sound audio to stereo can be achieved with:

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

Here, FLFRFCBLBR, and LFE represent the front left, front right, center, back left, back right, and low-frequency effect (subwoofer) channels, respectively. The coefficients (e.g., 0.50.707) determine the mixing ratios.

6. Custom Channel Mixing

For more advanced use cases, you can create custom mixes by applying specific gains to individual channels. For example:

	
        
    
        
    
    ffmpeg -i input.wav -af "pan=stereo|c0=0.9*FL+0.1*FR|c1=0.1*FL+0.9*FR" output.wav

This command creates a stereo output where the left channel (c0) is a mix of 90% front left (FL) and 10% front right (FR), and the right channel (c1) is a mix of 10% front left (FL) and 90% front right (FR).

Conclusion

The FFmpeg pan filter is a powerful tool for audio channel manipulation, offering developers the flexibility to handle a wide range of audio processing tasks.

We’ll explore upmixing more about upmixing and one liner’s in the upcoming post.


CONNECT

 
Have something to share,
Let's Connect