Convert specific video and audio track only with ffmpeg

ffmpegvideo conversion

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.

Best Answer

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.