[How To] Downmix Surround Sound To AAC Stereo Properly (using FFmpeg Batch Converter)

Hi all,

I have decided to make available my FFmpeg commands for FFmpeg Batch Converter that have taken me so long to come up with, but which will downmix surround sound to AAC stereo properly :slight_smile:

There are many FFmpeg commands sprinkled throughout the Plex forums and the internet for that matter.
What makes these commands different?

These commands result in an MKV file that has an extra audio track added (at top “default” position) to it in the format of AAC stereo 160Kbps that has been downmixed without drowning out the center channel (dialogue), has dynamic range compression applied appropriately and also has high device compatibility - no more transcoding!


Why FFmpeg Batch Converter?
I prefer to work with GUIs rather than CLIs, and this is the best frontend I could find for FFmpeg. What’s more, is it can batch process and multi-process!
Make sure to thank the developer of this nice little app.

Why AAC stereo at 160Kbps?
Our aim is to prevent transcoding, so device compatibility is a high priority. AAC stereo is the most compatible, and 160Kbps is the highest bitrate with the highest device compatibility.

Why not just use the default stereo downmix in Handbrake / VidCoder?
When encoding from surround sound to AAC stereo using the default FFmpeg downmix algorithm (-AC 2), the center channel (all the dialogue in movies/TV shows) is usually drowned out by loud left and right channels and sometimes even the surround channels.
Also, the dynamic range of surround sound tracks is quite large, meaning that the resulting downmixed stereo track’s volume needs to be forever turned up and down during playback.


I dream of a proper 7.2, 7.1 or even just 5.1 surround sound setup, but alas, I only have a 2.0 soundbar at the moment.
However, I do not wish to delete the surround track just in case that dream ever comes true.
To that end, these commands keep the original audio tracks, but shifts them all down by one position and adds the AAC stereo track on top of them, making it the default track - happy days!

These commands will shift a maximum of 10 existing audio tracks and 10 existing subtitles tracks, as well as the chapters - hopefully that will cover most needs.

I have played for many hours getting these settings right, but now that I am happy with them, I have run them on every single one of my MKV files.
The result on our 2.0 soundbar is brilliant!

After running my MKV files through the above procedure, I then Pre-encode them to MP4:
[How To] Pre-encode to avoid Transcoding (using VidCoder Presets)


These commands will work on all Dolby and DTS surround sound formats:

  • Dolby Digital, Dolby Digital Plus, Dolby TrueHD, Dolby Atmos, etc
  • DTS, DTS-HD, DTS-HD Master Audio, DTS-X, etc

[How To]

  1. Download, install and run FFmpeg Batch Converter

  2. Download ff_batch.zip (602 Bytes) and unzip.

  3. In FFmpeg Batch Converter, open “Settings” and then “Restore settings” (note, this will overwrite any previous saved presets)

  4. Browse to the unzipped “ff_batch.ini” file from step 2 and click “Open” (you should get a message “Configuration was restored successfully”)

  5. Add source MKV files to the file list

  6. Choose one of the three presets to run from the preset drop-down list (according to which audio track you want to downmix)

  7. Adjust the number of CPU cores that you want to use, should you want to use “multi-file” processing

  8. Encode (either “Start sequential” or “Start multi-file”)


Notes:

For dynamic range compression, I did try loudnorm as per this thread, but I was getting mixed results. Instead I went with @OttoKerner’s suggestion of acompressor. It worked a treat!

The following channels and gains are used in the downmix:

Left channel
FC (front center) x 1.414 (+3dB gain)
FL (front left) x 0.707 (-3dB gain)
FLC - front left-of-center x 0.5 (-6dB gain)
BL - back left x 0.5 (-6dB gain)
SL - side left x 0.5 (-6dB gain)
LFE - low frequency x 0.5 (-6dB gain)

Right channel
FC (front center) x 1.414 (+3dB gain)
FR (front right) x 0.707 (-3dB gain)
FRC - front right-of-center x 0.5 (-6dB gain)
BR - back right x 0.5 (-6dB gain)
SR - side right x 0.5 (-6dB gain)
LFE - low frequency x 0.5 (-6dB gain)

Feel free to modify the gains using the following in the commands:
1.414 +3dB gain
1.189 +1.5dB gain
1.000 0dB gain
0.841 -1.5dB gain
0.707 -3.0dB gain
0.595 -4.5dB gain
0.500 -6.0dB gain


I hope this helps someone or two :slight_smile:


The code for each command:

  • Track 1 - Uses the first audio track as source for downmix
    -map 0:0 -c:v copy -map 0:a:0? -c:a:0 aac -b:a:0 160k -filter:a:0 "pan=stereo|FL=1.414*FC+0.707*FL+0.5*FLC+0.5*BL+0.5*SL+0.5*LFE|FR=1.414*FC+0.707*FR+0.5*FRC+0.5*BR+0.5*SR+0.5*LFE,acompressor=ratio=4" -metadata:s:a:0 title="Custom DRC" -metadata:s:a:0 language=eng -disposition:a:0 default -map 0:a:0? -c:a:1 copy -disposition:a:1 none -map 0:a:1? -c:a:2 copy -disposition:a:2 none -map 0:a:2? -c:a:3 copy -disposition:a:3 none -map 0:a:3? -c:a:4 copy -disposition:a:4 none -map 0:a:4? -c:a:5 copy -disposition:a:5 none -map 0:a:5? -c:a:6 copy -disposition:a:6 none -map 0:a:6? -c:a:7 copy -disposition:a:7 none -map 0:a:7? -c:a:8 copy -disposition:a:8 none -map 0:a:8? -c:a:9 copy -disposition:a:9 none -map 0:s:0? -c:s:0 copy -map 0:s:1? -c:s:1 copy -map 0:s:2? -c:s:2 copy -map 0:s:3? -c:s:3 copy -map 0:s:4? -c:s:4 copy -map 0:s:5? -c:s:5 copy -map 0:s:6? -c:s:6 copy -map 0:s:7? -c:s:7 copy -map 0:s:8? -c:s:8 copy -map 0:s:9? -c:s:9 copy

  • Track 2 - Uses the second audio track as source for downmix
    -map 0:0 -c:v copy -map 0:a:1? -c:a:0 aac -b:a:0 160k -filter:a:0 "pan=stereo|FL=1.414*FC+0.707*FL+0.5*FLC+0.5*BL+0.5*SL+0.5*LFE|FR=1.414*FC+0.707*FR+0.5*FRC+0.5*BR+0.5*SR+0.5*LFE,acompressor=ratio=4" -metadata:s:a:0 title="Custom DRC" -metadata:s:a:0 language=eng -disposition:a:0 default -map 0:a:0? -c:a:1 copy -disposition:a:1 none -map 0:a:1? -c:a:2 copy -disposition:a:2 none -map 0:a:2? -c:a:3 copy -disposition:a:3 none -map 0:a:3? -c:a:4 copy -disposition:a:4 none -map 0:a:4? -c:a:5 copy -disposition:a:5 none -map 0:a:5? -c:a:6 copy -disposition:a:6 none -map 0:a:6? -c:a:7 copy -disposition:a:7 none -map 0:a:7? -c:a:8 copy -disposition:a:8 none -map 0:a:8? -c:a:9 copy -disposition:a:9 none -map 0:s:0? -c:s:0 copy -map 0:s:1? -c:s:1 copy -map 0:s:2? -c:s:2 copy -map 0:s:3? -c:s:3 copy -map 0:s:4? -c:s:4 copy -map 0:s:5? -c:s:5 copy -map 0:s:6? -c:s:6 copy -map 0:s:7? -c:s:7 copy -map 0:s:8? -c:s:8 copy -map 0:s:9? -c:s:9 copy

  • Track 3 - Uses the third audio track as source for downmix
    -map 0:0 -c:v copy -map 0:a:2? -c:a:0 aac -b:a:0 160k -filter:a:0 "pan=stereo|FL=1.414*FC+0.707*FL+0.5*FLC+0.5*BL+0.5*SL+0.5*LFE|FR=1.414*FC+0.707*FR+0.5*FRC+0.5*BR+0.5*SR+0.5*LFE,acompressor=ratio=4" -metadata:s:a:0 title="Custom DRC" -metadata:s:a:0 language=eng -disposition:a:0 default -map 0:a:0? -c:a:1 copy -disposition:a:1 none -map 0:a:1? -c:a:2 copy -disposition:a:2 none -map 0:a:2? -c:a:3 copy -disposition:a:3 none -map 0:a:3? -c:a:4 copy -disposition:a:4 none -map 0:a:4? -c:a:5 copy -disposition:a:5 none -map 0:a:5? -c:a:6 copy -disposition:a:6 none -map 0:a:6? -c:a:7 copy -disposition:a:7 none -map 0:a:7? -c:a:8 copy -disposition:a:8 none -map 0:a:8? -c:a:9 copy -disposition:a:9 none -map 0:s:0? -c:s:0 copy -map 0:s:1? -c:s:1 copy -map 0:s:2? -c:s:2 copy -map 0:s:3? -c:s:3 copy -map 0:s:4? -c:s:4 copy -map 0:s:5? -c:s:5 copy -map 0:s:6? -c:s:6 copy -map 0:s:7? -c:s:7 copy -map 0:s:8? -c:s:8 copy -map 0:s:9? -c:s:9 copy


Resources:
https://ffmpeg.org/ffmpeg-all.html
https://trac.ffmpeg.org/wiki/AudioChannelManipulation
https://superuser.com/questions/852400/properly-downmix-5-1-to-stereo-using-ffmpeg
https://forums.plex.tv/t/sharing-some-ffmpeg-scripts-i-made-to-down-convert-audio-in-batch-as-well-a-night-mode-drc/323820

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.