ffmpeg: select (-map) audio track by codec id

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).

Azevedo

Posted 2016-02-11T17:38:28.257

Reputation: 511

1I am not sure whether there is a way to map by codec, but at least you can check the index with ffprobe. – Tom Yan – 2016-02-11T18:18:53.830

Answers

2

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.

llogan

Posted 2016-02-11T17:38:28.257

Reputation: 31 929

I'm on windows. Since ffmpeg can't do it directly (yet) you got the closest. Thanks. – Azevedo – 2016-02-11T20:48:21.200