How do I swap the first and second audio streams in an MKV in a Linux based system?

10

8

I'm using plex. Plex seems to ignore the default audio stream (on Roku) and instead just use the first audio stream. This is per my testing using mkvpropedit.

Since Plex does not currently support the changing of audio sources on clients outside of the LAN, I'd like to change a few of my files so the current second audio source is the first?

What's the best way to swap audio streams in an MKV on a Linux based system such as Ubuntu?

user191723

Posted 2013-01-21T21:37:26.090

Reputation: 103

Answers

13

With FFmpeg, this should be rather simple. Make sure you download a static build from their download page and don't use the Ubuntu repository version, which is quite old.

Here's the command:

ffmpeg -i input.mkv -map 0:v:0 -map 0:a:1 -map 0:a:0 -c copy output.mkv

Here's what -map does:

  • The first part before the colon is the input ID. Since we only have one input, it's 0.
  • The second part specfies the type of stream, video or audio. This is optional, but it's always a good idea to specify the type as well, in case that video and audio streams are not correctly multiplexed.
  • The third part is the ID of the input stream. 0 will be first, and 1 the second, i.e. the first video stream and the second and first audio stream.
  • The order of the -map options determines the order of the streams in the output file.

This means we'll leave the video bitstream as the first stream, then take the second audio stream, and then the first—in essence, we're swapping the audio streams.

Using the -c copy option ensures that the bitstreams are copied and not re-encoded.

A few examples on how to use the -map option can be found on the FFmpeg wiki.

slhck

Posted 2013-01-21T21:37:26.090

Reputation: 182 472

If there's only one input you can actually leave out the input ID: -map v:0 -map a:1 -map a:0. – dessert – 2017-12-30T22:56:51.130

7

Just use mkvtool to avoid, repack, re-encode ... wasting time.

mkvpropedit -v movie.mkv -v --edit track:2 --set track-number=3 --edit track:3 --set track-number=2

this should be enough to swap stream.

Whoo

Posted 2013-01-21T21:37:26.090

Reputation: 71