0

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
  • 2
    I am not sure whether there is a way to map by codec, but at least you can check the index with ffprobe. Commented Feb 11, 2016 at 18:18

1 Answer 1

3

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
stream,1,vorbis
stream,2,aac
stream,3,mp3

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}'
2

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

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.