Convert specific video and audio track only with ffmpeg

29

7

I have an MKV file (vidui.mkv) with 6 tracks in it.

Track 1 - video - xvid - 1920x1080
Track 2 - video - xvid - 720x576
Track 3 - audio -  AAC - 1240kbps - English
Track 4 - audio -  AAC - 648kbps - Spanish
Track 5 - audio -  AAC - 648kbps - Commentary 1
Track 6 - audio -  AAC - 648kbps - Commentary 2

I want to convert the above file to a mp4 format with one h264 video and one AC3 audio. Also I want to convert track 1 (video) and track 5(audio).

If I use

ffmpeg.exe -i vidui.mkv -f mp4 -vcodec libx264 -acodec ac3 -crf 20 -sn -n vidui.mp4

it converts the first video track and first audio track, but what I would like it to do is convert track 1 and track 5.

hbelouf

Posted 2013-09-01T04:38:58.673

Reputation: 319

Answers

46

You can use the -map option (full documentation) to select specific input streams and map them to your output.

The most simple map syntax you can use is -map i:s, where i is the input file ID and s is the stream ID, both starting with 0. In your case, that means we select track 0 and 4:

ffmpeg -i vidui.mkv -c:v libx264 -c:a ac3 -crf 20 -map 0:0 -map 0:4 vidui.mp4

If you want to choose video, audio or subtitle tracks specifically, you can also use stream specifiers:

ffmpeg -i vidui.mkv -c:v libx264 -c:a ac3 -crf 20 -map 0:v:0 -map 0:a:1 vidui.mp4

Here, 0:v:0 is the first video stream and 0:a:1 is the second audio stream.

slhck

Posted 2013-09-01T04:38:58.673

Reputation: 182 472

Is there a way to do this dynamically? Say I want to do this only to audio tracks of language X, and have ffmpeg pick this up? – Freedo – 2017-10-20T07:31:16.250

@Freedo Yes, please see the documentation. For example -map 0:m:language:eng – slhck – 2017-10-20T13:55:46.293