Audio is hell...
-me (circa every time)
Hopefully this will give you a few tools to use when you need to modify or downmix audio in your files :)
If you've ever tried to downmix 5.1 audio into a stereo mix, typically using this:
ffmpeg -i 6channels.wav -ac 2 stereo.wav
Then you know that there are certainly some pitfalls there.
By default it just straight up drops the LFE (low frequency ) channel. The center channel also dosent seem right by just pushing it thru
The overall volume is also dropped significantly.
After going back and forth a bit, doing some reading and some testing, I ended up arriving at a certain way of negating these pitfalls. This can be modified for downmixing 7.1, 5,1 and I'm sure a bunch of other different scenarios.
In the following example, I want to downmix 5.1 to stereo, and I also want to keep the LFE, so this is the most reasonable way to go about it (that I've found so far). We get to adjust the volume back up to a reasonable level, and we keep our LFE, while balancing the center channels to our stereo mix.
ffmpeg -i input.mp4 -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, volume=1.50" -ac 2 out.mp4
-af is an audio filter, and we want it to do a couple of different things.
pan=stereo ref docs, is used to mix channels, which in this case we want to downmix to stereo.
FL, FC, FR etc. is just Front left, center and right. The rest is pretty self-explanatory. B(x) is back. LFE is low frequency (think subwoofer channel).
If you're ever wondering which one was which again, run this:
volume is well, volume, and it is just used to adjust the overall volume of the output. I want this increased, since the downmixing process produces a bit lower volume. This was a good value for me. Do your testing and tweak yours accordingly.
-ac 2 is audio channel 2, we're doing stereo, remember? ;)
Sometimes you run into audio tracks that are a bit strange. For instance some are
5.1 and some are
5.1(side). You can use
ffprobe if you ever need to know the layout, like so:
ffprobe -v error -show_entries stream=channel_layout -of csv=p=0 input.mp4
It should return something like this:
For sake of ease, here is the full output of
Individual channels: NAME DESCRIPTION FL front left FR front right FC front center LFE low frequency BL back left BR back right FLC front left-of-center FRC front right-of-center BC back center SL side left SR side right TC top center TFL top front left TFC top front center TFR top front right TBL top back left TBC top back center TBR top back right DL downmix left DR downmix right WL wide left WR wide right SDL surround direct left SDR surround direct right LFE2 low frequency 2 Standard channel layouts: NAME DECOMPOSITION mono FC stereo FL+FR 2.1 FL+FR+LFE 3.0 FL+FR+FC 3.0(back) FL+FR+BC 4.0 FL+FR+FC+BC quad FL+FR+BL+BR quad(side) FL+FR+SL+SR 3.1 FL+FR+FC+LFE 5.0 FL+FR+FC+BL+BR 5.0(side) FL+FR+FC+SL+SR 4.1 FL+FR+FC+LFE+BC 5.1 FL+FR+FC+LFE+BL+BR 5.1(side) FL+FR+FC+LFE+SL+SR 6.0 FL+FR+FC+BC+SL+SR 6.0(front) FL+FR+FLC+FRC+SL+SR hexagonal FL+FR+FC+BL+BR+BC 6.1 FL+FR+FC+LFE+BC+SL+SR 6.1(back) FL+FR+FC+LFE+BL+BR+BC 6.1(front) FL+FR+LFE+FLC+FRC+SL+SR 7.0 FL+FR+FC+BL+BR+SL+SR 7.0(front) FL+FR+FC+FLC+FRC+SL+SR 7.1 FL+FR+FC+LFE+BL+BR+SL+SR 7.1(wide) FL+FR+FC+LFE+BL+BR+FLC+FRC 7.1(wide-side) FL+FR+FC+LFE+FLC+FRC+SL+SR octagonal FL+FR+FC+BL+BR+BC+SL+SR hexadecagonal FL+FR+FC+BL+BR+BC+SL+SR+TFL+TFC+TFR+TBL+TBC+TBR+WL+WR downmix DL+DR