The Computer Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities

--

Chapters
00:00 How Do I Swap The First And Second Audio Streams In An Mkv In A Linux Based System?
00:35 Accepted Answer Score 24
01:51 Answer 2 Score 8
02:06 Answer 3 Score 0
03:07 Thank you

--

Full question
https://superuser.com/questions/539640/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #videoediting #matroska

#avk47



ACCEPTED ANSWER

Score 24


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.




ANSWER 2

Score 8


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.




ANSWER 3

Score 0


If you want to combine input files (video + audio) and also set a default audio track.

Input files:

1.mkv // video + Japanese audio
2.mka // English audio
3.mka // Russian audio

Let's say you want to combine all these files and set English track as a default.

Default means - on the first place (by default in most players if they ignore metadata).

ffmpeg -i 1.mkv -i 2.mka -i 3.mka -map 0:v -map 1:a -map 0:a -map 2:a -c:v copy -shortest out.mkv

-map 0:v - take first input file, take video from it and put it as a first video in output

-map 1:a - take second input file, take audio from it and put it as a first audio in output

-map 0:a - take first input file, take audio from it and put it as a second audio in output

-map 2:a - take third input file, take audio from it and put it as a third audio in output

as you can see, order of -map matters!

-c:v copy - copy video

-shortest - in case of audio/video length difference, cut other by the shortest