How can I use ffmpeg -map switch to select the audio track by its codec?

Say there is a video.mkv with one video stream (0) and three audio tracks (1, 2, 3).

The audio tracks have different codecs, say DTS, AAC and AC-3 (in any order).

I want to produce an output file with the video stream -map 0:0 and only the AAC track, something like:

ffmpeg -i video.mkv -map 0:0 -map 0:aac -codec copy output_with_aac_audio.mkv

In the real scenario I don't know which one is the AAC track (1, 2 or 3).

  • 1
    I am not sure whether there is a way to map by codec, but at least you can check the index with ffprobe. – Tom Yan Feb 11 '16 at 18:18

As Tom Yan mentioned in his comment you can use ffprobe to get the stream indexes:

$ ffprobe -loglevel error -select_streams a -show_streams -show_entries stream=index,codec_name:tags=:disposition= -of csv input.mkv

I'll assume you're using Linux, so adding awk will give you just the index(es) of the aac stream(s). In this example the result show the aac stream as stream index 2:

$ ffprobe -loglevel error -select_streams a -show_streams -show_entries stream=index,codec_name:tags=:disposition= -of csv input.mkv | awk -F',' '/aac/ {print $2}'

Then use -map 0:2 in your ffmpeg command.

  • I'm on windows. Since ffmpeg can't do it directly (yet) you got the closest. Thanks. – Azevedo Feb 11 '16 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.