FFmpeg and AAC Encoding Guide

= FFmpeg and AAC Encoding Guide =
 
[[PageOutline(2-3, Contents)]]
 
[[http://en.wikipedia.org/wiki/Advanced_Audio_Coding|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`). The licenses of `libaacplus`, `libfaac`, and `libfdk_aac` are not compatible with the GPL, so the GPL does not permit distribution of binaries containing code licensed under these licenses when GPL-licensed code is also included. Therefore these encoders have been designated as "non-free", and you cannot download a pre-built ffmpeg that supports them. This can be resolved by [[CompilationGuide|compiling ffmpeg]] yourself.
 
See also [[GuidelinesHighQualityAudio]] for general guidelines on FFmpeg audio encoding.
 
----
 
== libfdk_aac ==#fdk_aac
 
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` (and additionally `--enable-nonfree` if you're also using `--enable-gpl`). But beware, it defaults to a low-pass filter of around 14kHz. If you want to preserve higher frequencies, use `-cutoff 18000`.  Adjust the number to the upper frequency limit you prefer.
 
=== Constant Bit Rate (CBR) mode ===#fdk_cbr
 
These settings target a specific bit rate, with less variation between samples. 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.
 
==== Examples ====
 
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 [[x264EncodingGuide#twopass|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
}}}
 
=== Variable Bit Rate (VBR) mode ===#fdk_vbr
 
VBR encoding in libfdk-aac is '''experimental''' and only works with some parameter combinations. Target a ''quality'', rather than a specific ''bit rate''. 1 is lowest quality and 5 is highest quality. This mode is not compatible with [[#fdk_he|AAC-HE]]. Set the VBR level with the `-vbr` flag.
 
According to [[http://www.hydrogenaudio.org/forums/index.php?showtopic=95989|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):
 
||VBR||kb/s/channel||
||1||32||
||2||40||
||3||48-56||
||4||64||
||5||80-96||
 
==== Examples ====
 
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 [[x264EncodingGuide#crf|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
}}}
 
=== High-Efficiency AAC ===#fdk_he
 
This is a pair of AAC profiles tailored for low bit rates (version 1 and version 2). VBR mode is not compatible with [[#fdk_he|AAC-HE]] so a bitrate is declared. 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).
 
NOTE: AAC-HE version 2 only handles stereo. If you have mono, or want to down-mix to mono, use AAC-HE version 1.
 
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.
 
 
==== 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
}}}
 
=== Streaming and AAC Player Compatibility ===#fdk_stream
 
By default when encoding AAC files using libfdk_aac the metadata ('moov' atom) is written after the audio stream ('mdat' atom) at the end of the file.
In order to enable streaming of the encoded file the 'moov' atom has to be moved before the 'mdat' atom. In addition some AAC player implementations have issues decoding such files.
 
FFmpeg offers the option '-movflags +faststart' covering that functionality which can be used during encoding:
 
{{{
ffmpeg -i input.wav -c:a libfdk_aac -movflags +faststart output.m4a
}}}
 
Existing m4a files can be modified with the "qt-faststart' program which is distributed with FFmpeg in the 'tools' directory
 
{{{
qt-faststart input.m4a 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`. This is considered an experimental encoder, so `-strict experimental` or `-strict -2` is required.
 
==== Example using `-b:a` ====
 
{{{
ffmpeg -i input.wav -strict experimental -c:a aac -b:a 240k output.m4a
}}}
 
  '''Note:''' `-cutoff 15000` was previously recommended for this encoder, but this is now automatically applied since 2012 July.
 
 
==== Example using `-q:a` ====
 
{{{
ffmpeg -i input.wav -strict experimental -c:a aac -q:a 6 output.m4a
}}}
 
Effective range for `-q:a` is around 0.1-10. This VBR is experimental and likely to get even worse results than the CBR. If ffmpeg ignores `-q:a` then get a newer build (see ticket #[ticket:1346]).
 
 
----
 
== libvo_aacenc ==
 
!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 [http://d.hatena.ne.jp/kamedo2/20120729/1343545890 Quality Assessment of FFmpeg AAC] and [http://ffmpeg.org/pipermail/ffmpeg-devel/2013-June/144589.html a mailing list post]. This encoder supports up to 2 channels, is CBR only, and does not work with `-q:a`/`-qscale:a`.
 
{{{
ffmpeg -i input.wav -c:a libvo_aacenc -b:a 128k output.m4a
}}}
 
----
 
== libfaac ==
 
Freeware Advanced Audio Coder. Requires ffmpeg configuration with `--enable-libfaac --enable-nonfree`. Note that you will not get as good results as with `libfdk_aac`.
 
==== Variable Bit Rate (VBR) Example ====
{{{
ffmpeg -i input.wav -c:a libfaac -q:a 100 output.m4a
}}}
 
Range for `-q:a` is 10-500 and is similar to using the `-q` option in standalone `faac`. 100 is a good value to try.
 
==== Average Bit Rate (ABR) Example ====
{{{
ffmpeg -i input.wav -c:a libfaac -b:a 192k output.m4a
}}}
 
`libfaac` does not support a true Constant Bit Rate (CBR) mode.
 
----
 
== Metadata ==
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 (Let's Mux)" output.mp4
}}}
 
----
 
== FAQ ==
=== Which encoder should I use? What provides the best quality? ===
 
For AAC-LC the likely answer is: `libfdk_aac` > `libfaac` > Native FFmpeg AAC encoder (`aac`) > `libvo_aacenc`.
 
=== Should I use AAC-LC or AAC-HE? ===
 
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.
 
=== I get an error about "experimental codecs". What does this mean? ===
 
Some encoders, such as the native FFmpeg AAC encoder (`aac`), are considered experimental and require the addition of `-strict experimental` or `-strict -2` (same thing, different name) to your command as an output option. Otherwise you may see:
 
{{{
The encoder 'aac' is experimental but experimental codecs are not enabled, add '-strict -2' if you want to use it.
}}}
 
So, for example, given a command line such as:
 
{{{
ffmpeg -i file.avi file.mov
}}}
 
you may need to invoke `ffmpeg` as follows for the experimental codec to be enabled:
 
{{{
ffmpeg -i file.avi -strict -2 file.mov
}}}
 
----
 
== Also See ==
 
* [[GuidelinesHighQualityAudio|Guidelines for high quality lossy audio encoding]]
 
 
{{{#!comment
todo:  
Find out if libfdk_aac and libaacplus can be installed/supported at the same time. I can't remember. If not then add error(s) and any trac ticket # to FAQ and/or each encoder's section.
 
List available metadata options for the MP4 container.
 
Determine actual `-q:a` range for `aac`
}}}
 
{{{#!comment
For libfdk_aac, cbr may be better than vbr. vbr 3 sets lowpass around 14.4k, which is too low and the effect is very audible. So I flipped the cbr and the vbr so that the cbr comes first. Maybe I should do a formal blind test later.
}}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值