How do I swap the first and second audio streams in an MKV in a Linux based system?
Solution 1:
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,
v
ideo ora
udio. 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, and1
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.
Solution 2:
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.