FFmpeg - Audio Channel Layout: Difference between revisions

From PiRho Knowledgebase
Jump to navigationJump to search
Dex (talk | contribs)
No edit summary
Dex (talk | contribs)
No edit summary
 
Line 8: Line 8:


== Summary ==
== Summary ==
This article explains how FFmpeg understands, manipulates, converts, and remaps audio channels.
This article explains how FFmpeg understands, manipulates, converts, and remaps audio channels (e.g., Stereo, 5.1, 7.1). It covers layouts, downmixing, upmixing, channel mapping, and how to explicitly control audio routing using FFmpeg filters such as ''pan'', ''channelmap'', and ''aresample''.


== Why Channel Layout Matters ==
== Why Channel Layout Matters ==
Modern multimedia workflows often involve mixed devices and formats.
Modern multimedia workflows often involve a mixture of devices and formats:
* Surround sound sources (5.1 / 7.1)
* Stereo-only playback systems
* Mobile devices with mono speakers
* Incorrectly labelled or inconsistently ordered tracks
* Broadcast, film, and online workflows requiring predictable channel order
 
A correct channel layout ensures voices remain centred, surround effects are preserved, LFE is handled appropriately, and no channels are lost or duplicated.


== Understanding FFmpeg’s Channel Layouts ==
== Understanding FFmpeg’s Channel Layouts ==
Common layouts include mono, stereo, 5.1, 7.1.
FFmpeg recognises channel layouts by both name and bitmask.
 
=== Common Layout Names ===
* mono
* stereo (FL, FR)
* 3.0 (L, R, C)
* 4.0 (L, R, SL, SR)
* 5.0 (L, R, C, SL, SR)
* 5.1 (L, R, C, LFE, SL, SR)
* 7.1 (L, R, C, LFE, SL, SR, BL, BR)
 
=== Inspecting an Input File ===
ffmpeg -i input.mkv


== Downmixing Surround to Stereo ==
== Downmixing Surround to Stereo ==
Examples of downmix commands.
=== Default Downmix ===
ffmpeg -i input.mkv -ac 2 output.mp4
 
=== Custom Downmix Matrix ===
ffmpeg -i input-5.1.wav -filter_complex "pan=stereo|FL = 0.90*FL + 0.32*FC + 0.22*SL + 0.22*BL|FR = 0.90*FR + 0.32*FC + 0.22*SR + 0.22*BR" output.wav


== Upmixing Stereo to Surround ==
== Upmixing Stereo to Surround ==
Example: stereo to 5.1.
=== Basic Upmix ===
ffmpeg -i stereo.wav -af "channelmap=channel_layout=5.1" output.wav
 
=== Enhanced Upmix ===
ffmpeg -i stereo.wav -filter_complex "pan=5.1|FL=FL|FR=FR|C=0.5*FL+0.5*FR|SL=FL|SR=FR|LFE=0" output.wav


== Reordering Channels (channelmap) ==
== Reordering Channels (channelmap) ==
Example: correcting wrong order.
ffmpeg -i wrong-order.wav -filter_complex "channelmap=map=0|1|4|5|2|3:channel_layout=5.1" fixed.wav


== Extracting Individual Channels ==
== Extracting Individual Channels ==
Examples using -map_channel.
ffmpeg -i input.wav -map_channel 0.0.0 FL.wav
ffmpeg -i input.wav -map_channel 0.0.1 FR.wav
ffmpeg -i input.wav -map_channel 0.0.2 FC.wav
ffmpeg -i input.wav -map_channel 0.0.3 LFE.wav


== Creating Silent Placeholder Channels ==
== Creating Silent Placeholder Channels ==
Example using anullsrc.
ffmpeg -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 silence.wav


== Common Pitfalls ==
== Common Pitfalls ==
Includes layout mismatches and LFE issues.
=== Invalid Layouts ===
Occurs when channel count mismatches layout.
 
=== LFE Issues ===
LFE may be discarded or reduced in downmixes.
 
=== Wrong Surround Ordering ===
Rear vs side channels may be swapped.


== Design & Architecture Considerations ==
== Design & Architecture Considerations ==
Always explicitly declare layouts.
* Always explicitly declare layouts.
* Avoid relying on defaults.
* Document downmix matrices.
* Test with both headphones and speakers.


== Troubleshooting Checklist ==
== Troubleshooting Checklist ==
Use ffprobe to inspect layouts.
ffprobe -show_streams input.wav


== Related Topics ==
== Related Topics ==

Latest revision as of 14:40, 14 March 2026


References:

Summary

This article explains how FFmpeg understands, manipulates, converts, and remaps audio channels (e.g., Stereo, 5.1, 7.1). It covers layouts, downmixing, upmixing, channel mapping, and how to explicitly control audio routing using FFmpeg filters such as pan, channelmap, and aresample.

Why Channel Layout Matters

Modern multimedia workflows often involve a mixture of devices and formats:

  • Surround sound sources (5.1 / 7.1)
  • Stereo-only playback systems
  • Mobile devices with mono speakers
  • Incorrectly labelled or inconsistently ordered tracks
  • Broadcast, film, and online workflows requiring predictable channel order

A correct channel layout ensures voices remain centred, surround effects are preserved, LFE is handled appropriately, and no channels are lost or duplicated.

Understanding FFmpeg’s Channel Layouts

FFmpeg recognises channel layouts by both name and bitmask.

Common Layout Names

  • mono
  • stereo (FL, FR)
  • 3.0 (L, R, C)
  • 4.0 (L, R, SL, SR)
  • 5.0 (L, R, C, SL, SR)
  • 5.1 (L, R, C, LFE, SL, SR)
  • 7.1 (L, R, C, LFE, SL, SR, BL, BR)

Inspecting an Input File

ffmpeg -i input.mkv

Downmixing Surround to Stereo

Default Downmix

ffmpeg -i input.mkv -ac 2 output.mp4

Custom Downmix Matrix

ffmpeg -i input-5.1.wav -filter_complex "pan=stereo|FL = 0.90*FL + 0.32*FC + 0.22*SL + 0.22*BL|FR = 0.90*FR + 0.32*FC + 0.22*SR + 0.22*BR" output.wav

Upmixing Stereo to Surround

Basic Upmix

ffmpeg -i stereo.wav -af "channelmap=channel_layout=5.1" output.wav

Enhanced Upmix

ffmpeg -i stereo.wav -filter_complex "pan=5.1|FL=FL|FR=FR|C=0.5*FL+0.5*FR|SL=FL|SR=FR|LFE=0" output.wav

Reordering Channels (channelmap)

ffmpeg -i wrong-order.wav -filter_complex "channelmap=map=0|1|4|5|2|3:channel_layout=5.1" fixed.wav

Extracting Individual Channels

ffmpeg -i input.wav -map_channel 0.0.0 FL.wav
ffmpeg -i input.wav -map_channel 0.0.1 FR.wav
ffmpeg -i input.wav -map_channel 0.0.2 FC.wav
ffmpeg -i input.wav -map_channel 0.0.3 LFE.wav

Creating Silent Placeholder Channels

ffmpeg -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 silence.wav

Common Pitfalls

Invalid Layouts

Occurs when channel count mismatches layout.

LFE Issues

LFE may be discarded or reduced in downmixes.

Wrong Surround Ordering

Rear vs side channels may be swapped.

Design & Architecture Considerations

  • Always explicitly declare layouts.
  • Avoid relying on defaults.
  • Document downmix matrices.
  • Test with both headphones and speakers.

Troubleshooting Checklist

ffprobe -show_streams input.wav

Related Topics

  • FFmpeg – Resampling & Formats
  • FFmpeg – Filter Graph Basics
  • Audio Fundamentals – Channel Concepts