|Version 6 (modified by MarkZV, 4 months ago) (diff)|
FFmpeg and AAC Encoding Guide
Advanced Audio Coding (AAC) is the successor format to MP3, and is defined in MPEG-4 part 3 (ISO/IEC 14496-3). It is often used within an MP4 container format; for music the .m4a extension is customarily used. The second-most common use is within MKV (Matroska) files because it has better support for embedded text-based soft subtitles than MP4. The examples in this guide will use the extensions MP4 and M4A.
FFmpeg can support four AAC-LC encoders (aac, libfaac, libfdk_aac, libvo_aacenc) and two AAC-HE encoders (libaacplus and libfdk_aac). libaacplus, libfaac, and libfdk_aac are considered "non-free", and are therefore non-redistributable, meaning you may have trouble finding a build that supports these. Of course you can resolve this by compiling ffmpeg.
Fraunhofer FDK AAC codec library. This is currently the highest-quality AAC encoder available with ffmpeg. Requires ffmpeg to be configured with --enable-libfdk_aac --enable-nonfree.
Variable Bit Rate (VBR) mode
These settings target a quality, rather than a specific bit rate. 1 is lowest quality (though still pretty good) and 5 is highest quality. For some people with some files claim even a VBR of 1 is indistinguishable from CD-quality audio; test it out and use the lowest setting that works for you. This mode is not compatible with AAC-HE, but for AAC-LC (the default for ffmpeg, and most compatible AAC profile) it should probably be preferred, since it allows the encoder greater flexibility to distribute bits as it sees fit: high-tempo, large dynamic range audio (like dubstep or rock music) requires more bits to encode then low-tempo, low dynamic range audio (like whalesong, or a recorded lecture - though there are special speech codecs that you should consider for the latter). Set the VBR level with the -vbr flag.
According to this hydrogenaudio post, the VBR modes (on average, over a number of files) give the following bit rates per channel (so for stereo, double the bit rate; for 5.1 surround sound, multiply it by six):
Convert an audio file to AAC in an M4A (MP4) container:
ffmpeg -i input.wav -c:a libfdk_aac -vbr 3 output.m4a
Convert the audio only of a video:
ffmpeg -i input.mp4 -c:v copy -c:a libfdk_aac -vbr 3 output.mp4
Convert the video with libx264, and mix down audio to two channels:
ffmpeg -i input.mp4 -c:v libx264 -crf:v 22 -preset:v veryfast \ -ac 2 -c:a libfdk_aac -vbr 3 output.mp4
Constant Bit Rate (CBR) mode
These settings target a specific bit rate, with less variation between samples. It will get you slightly lower quality for the bit rate it gives than the VBR mode would; but it gives you greater control over file size, and it is compatible with the HE-AAC profile. As a rule of thumb, for audible transparency, use 64kb/s for each channel (so 128kb/s for stereo, 384 kb/s for 5.1 surround sound). Set the bit rate with the -b:a flag.
Convert and audio file to AAC in an M4A (MP4) container:
ffmpeg -i input.wav -c:a libfdk_aac -b:a 128k output.m4a
Convert 5.1 surround sound audio of a video, leaving the video alone:
ffmpeg -i input.mp4 -c:v copy -c:a libfdk_aac -b:a 384k output.mp4
Convert the video with libx264, with a target of fitting a 90-minute movie on a 700MB(=5734400kb) CD-ROM, mixing the audio down to two channels (Windows users should use NUL rather than /dev/null):
ffmpeg -y -i input.mp4 -c:v libx264 -b:v 933k -preset:v veryfast -pass 1 -an /dev/null && \ ffmpeg -i input.mp4 -c:v libx264 -b:v 933k -preset:v veryfast -pass 2 \ -ac 2 -c:a libfdk_aac -b:a 128k output.mp4
This is a pair of AAC profiles tailored for low bit rates (version 1 and version 2). AAC-HE version 1 is suited for bit rates below 64kb/s (for stereo audio) down to about 48 kb/s, while AAC-HE version 2 is suited for bit rates as low as 32 kb/s (again, for stereo).
Unfortunately, many devices that can play AAC-LC (the default profile for libfdk_aac) simply cannot play either version of AAC-HE, so this is not recommended for surround sound audio, which normally needs to be compatible with such hardware players. If you are only going to play it on your computer, or you are sure that your hardware player supports AAC-HE, you can aim for a bit rate of 160kb/s for version 1, or 128kb/s for version 2. As always, experiment to see what works for your ears.
The following examples are adapted from this ubuntuforums post.
AAC-HE version 1
ffmpeg -i input.wav -c:a libfdk_aac -profile:a aac_he -b:a 64k output.m4a
AAC-HE version 2
ffmpeg -i input.wav -c:a libfdk_aac -profile:a aac_he_v2 -b:a 32k output.m4a
Native FFmpeg AAC encoder
The native FFmpeg AAC encoder is included with ffmpeg and is does not require an external library like the other AAC encoders described here. Note that you will not get as good results as with libfdk_aac. The -cutoff option may improve quality according to Quality Assessment of FFmpeg AAC. This encoder does not currently work with -q:a/-qscale:a (see ticket #1346).
ffmpeg -i input.wav -strict experimental -c:a aac -cutoff 15000 -b:a 128k output.m4a
VisualOn AAC encoding library. Requires ffmpeg configuration with --enable-libvo-aacenc. This has the advantage of not being non-free, and is included by some distributors, but is a rather poor encoder compared to libfdk_aac and even the native FFmpeg AAC encoder according to Quality Assessment of FFmpeg AAC. This encoder does not work with -q:a/-qscale:a.
ffmpeg -i input.wav -c:a libvo_aacenc -b:a 128k output.m4a
Freeware Advanced Audio Coder. Requires ffmpeg configuration with the ironic options of --enable-libfaac --enable-nonfree.
ffmpeg -i input.wav -c:a libfaac -q:a 100 output.m4a
Range for -q:a is 0-250 and is similar to using the -q option in standalone faac.
ffmpeg -i input.wav -c:a libfaac -b:a 128k output.m4a
You can add metadata to any of the examples on this guide:
ffmpeg -i input ... -metadata author="FFmpeg Bayou Jug Band" -metadata title="Decode my Heart" output.mp4
Which encoder should I use? What provides the best quality?
It depends. For AAC-LC the likely answer is: libfdk_aac > libfaac > Native FFmpeg AAC ≥ libvo_aacenc.
Should I use AAC-LC or AAC-HE?
It also depends. If you require a low audio bitrate, such as ≤ 32kbs/channel, then AAC-HE would be worth considering if your player or device can support AAC-HE decoding. Anything higher may benefit more from AAC-LC due to less processing. If in doubt use AAC-LC. All players supporting AAC-HE also support AAC-LC.