FFmpeg: rearrange audio streams in video files

0
FFmpeg logo
FFmpeg logo

Harmonizing the order of audio streams in your video files can be done by using the free open source tool FFmpeg in the ‘Terminal’.

In order to use FFmpeg in the ‘Terminal’ of an Apple macOS computer you have to install it. Installation instructions can be found in my blog post ‘Fixing corrupted AVI indexes‘.

First run the command ‘ffprobe <your file name>’ in your ‘Terminal’. At the end of the Terminal output you will get something like the following.

 

In this example you can see the following. The multimedia file contains three streams (see highlighted lines in yellow):

  • Stream 0: video stream
  • Stream 1: audio stream, German language, set as default audio stream, forced audio stream
  • Stream 2: audio stream, English language

 

A forced audio stream will override the settings in your video player. If you defined English as your preferred audio language in your video player, with this example file the player would play the German language nevertheless.

Note on the enumartion ‘#x:y’: Counting starts with 0. The first number x represents the number of the input file. So, 0 means the first input file. The second number y represents the stream within the input file. So, 0 means the first stream. In the above example file we only have one input file, and therefore the first number will always be 0.

Now, we want to achieve the following:

  • no time-consuming re-encoding
  • Stream 0: video stream
  • Stream 1: audio stream, English language, default stream
  • Stream 2: audio stream, German language
  • no forced language stream

 

To do that we can use the following command:

 

Explanation of the parameters:

  • -c copy – will just copy the streams without re-encoding
  • -map 0:m:language:eng – will put the English stream as first audio stream
  • -map 0:m:language:ger – will put the German stream as second audio stream
  • -disposition:a:0 default – will define the first audio stream as default
  • -disposition:a:0 0 – will erase any previous disposition settings (in this example removing default and forced setting)

 

If you have a more complex stream situation in your input file you can also use the alternative syntax to achieve the same. The ‘map’ parameters will have to be used as following:

  • -map 0:v:0 – will put the first video stream of the first input file as first video stream
  • -map 0:a:1 – will put the second audio stream of the first input file as first audio stream
  • -map 0:a:0 – will put the first audio stream of the first input file as second audio stream

 

Note: the ordering of the ‘map’ parameters defines the ordering of the streams in the output file. And you can also use the S-parameter, e.g. -map 0:s:0, in order to move subtitle streams.

LEAVE A REPLY

Please enter your comment!
Please enter your name here