I'm using ffmpeg to transcode some video files and for this post we'll take test.mkv. test.mkv has an audio track at index 2 that is AAC 7.1. I want to keep the track but also add a AAC stereo version.
ffmpeg -y -i test.mkv \
-map 0 \
-map 0:2 -codec:2 copy \
-map 0:2 -codec:3 libfdk_aac -ac 2 \
out.mkv
However, I've noticed that the stereo track gets converted to vorbis. This is strange because I specified -map 0:2 -codec:3 libfdk_aac -ac 2
in the command. So I did some testing, trying to extract the audio track:
ffmpeg -y -i test.mkv \
-map 0:2 -codec:2 libfdk_aac \
test.aac
#-> important parts of the output
[aac @ 0x55761a165c40] Assuming an incorrectly encoded 7.1 channel layout instead of a spec compliant 7.1(wide) layout, use -strict 1 to decode according to the specification instead.
Stream #0:2 -> #0:0 (aac (native) -> aac (native))
Stream #0:0(eng): Audio: aac (LC), 48000 Hz, 7.1, fltp, 469 kb/s (default)
This is strange because I specified libfdk_aac as the encoder (as it is way better than aac) and yet it shows [aac @ ...
in the output. So when I extract an aac audio track, it uses aac and when I transcode it in a media file, it uses vorbis. All not libfdk_aac. It seems like it is just ignoring the codec argument and deciding for it's self what it's going to convert it to.
I installed ffmpeg with libfdk_aac using this guide which is directly in the question as an edit.
My goal is that the audio track selected gets transcoded to aac. Not vorbis and not using the aac encoder; using the libfdk_aac encoder.
EDIT: Okay I was able to determine the problem, though I don't know how to fix it yet. See the following example:
#test.mkv contains opus audio track at index 1
ffmpeg -y -i test.mkv -map 0:1 -codec:1 aac output.flac
#output.flac's codec is flac and not aac
ffmpeg -y -i test.mkv -map 0:1 -codec:1 copy output.flac
#output.flac's codec is flac and not opus
ffmpeg -y -i test.mkv -map 0:1 -codec:1 aac output.mkv
#output.mkv's codec is vorbis and not aac
So what do we learn from these results? ffmpeg sets the codec to whatever fits in the container of the output file. So when the output file is .flac, it sets the codec to flac eventhough I've set it to aac. And apperently, ffmpeg has decided that vorbis is the codec for audio streams when the output container is .mkv. Basically, ffmpeg listens to the extension of the output file and not to what I say using the -codec parameter.
This should be turned around; when no codec is given for a stream, make it fit in the output container but if the codec is given for a stream, transcode it in that codec, thus ignoring the file extension of the output file.
In the very first example in this question, where I create an extra stereo audio track inside the file, I set the codec for that stereo track to libfdk_aac but the output container is .mkv so ffmpeg transcodes it to vorbis.
However, now that we know the problem, how do we fix it?
EDIT 2:
Okay I know what the problem was. If I have a file with ONE audio track at index 1,-map 0:1 -codec:a:0 aac
won't work. Eventhough the map and codec both target the same stream, the codec part will not be applied to the stream that was mapped one argument before. Conclusion: The way you refer to a stream has to be the same everywhere.
aac
instead oflibfdk_aac
? Is itaac
orvorbis
?