= Guidelines for high quality lossy audio encoding = This guide is meant to help people new to encoding lossy audio to use the correct encoders and settings. == When should I transcode audio? == Avoid transcoding from a lossy format to the same or another lossy format when possible. Transcode to lossy from the lossless source (if you have it), or just copy the lossy source audio track instead of transcoding. Another option if you have a lossless source is to transcode it to another lossless codec, like [[http://superuser.com/a/339031/39364|flac ]]. == Generation loss == Transcoding from a lossy format like [[Encoding VBR (Variable Bit Rate) mp3 audio|MP3]], [[AACEncodingGuide|AAC]], Vorbis, Opus, WMA, etc. to the same or different lossy format might degrade the audio quality even if the bitrate stays the same (or higher). This quality degradation might not be audible to you but it might be audible to others.[[BR]] This post on hydrogenaudio.org demonstrates what will happen if you re-encode a file 100 times: [[http://www.hydrogenaudio.org/forums/index.php?showtopic=100067]] == Copying audio tracks == If the target container format supports the audio codec of the source file then consider just muxing it into the output file without re-encoding. MKV supports virtually any audio codec. This can be achieved by specifying 'copy' as the audio codec. Example:[[BR]] Transcoding a WebM file (with VP8 video/Vorbis audio) to a MKV file (with H.264 video/unaltered Vorbis audio):[[BR]] {{{ ffmpeg -i someFile.webm -c:a copy -c:v libx264 outFile.mkv }}} In some cases this might not be possible, because the target device/player doesn't support the codec or the target container format doesn't support the codec. Another reason to transcode might be that the source audio track is too big (it has a higher bitrate than what you want to use in the output file). == Audio encoders FFmpeg can use == FFmpeg can encode to a wide variety of lossy audio formats.[[BR]] Here are some popular lossy formats with encoders listed that FFmpeg can use:[[BR]] {{{ Dolby Digital: ac3 Dolby Digital Plus: eac3 MP2: libtwolame, mp2 Windows Media Audio 1: wmav1 Windows Media Audio 2: wmav2 LC-AAC: libfdk_aac, libfaac, aac, libvo_aacenc HE-AAC: libfdk_aac, libaacplus Vorbis: libvorbis, vorbis MP3: libmp3lame, libshine Opus: libopus }}} Based on quality produced from high to low:[[BR]] {{{ libopus > libfdk_aac = libvorbis > libmp3lame >= libfaac >= eac3/ac3 > aac > libtwolame > vorbis > mp2 > wmav2/wmav1 > libvo_aacenc }}} {{{#!comment I think we should do a listening test between libfaac and eac3/ac3, because from my experience libfaac can't really handle 128kbps or 160kbps as well as ac3 libopus >= libvorbis >= libfdk_aac > libmp3lame > eac3/ac3 >= libfaac > libtwolame >= aac > mp2 > vorbis > wmav2/wmav1 > libvo_aacenc Both libfaac and ac3 have annoying sounds at 128Kbps. It probably depends on what one prefers, but ac3 is definitely better than libtwolame at 128Kbps. libtwolame completely breaks down at that rate. Now I see that even aac has been ranked higher quality than ac3?? Also unless I had a broken libvo_aacenc encoder I doubt libvo_aacenc beats mp2 @ 256Kbps. It just doesn't make sense. There really needs to be a public listening test. }}} For AAC only: (Because it is a little bit confusing, with 4 encoders available)[[BR]] {{{ libfdk_aac > libfaac > aac > libvo_aacenc }}} {{{#!comment Source for above hierarchy? I based it on which encoder's would reach transparency first based on bitrate. libopus/libfdk_aac/libvorbis/libmp3lame/(ac3 most of the times) should all be transparent at 128Kbps. The encoders listed to the right don't achieve that. Any ideas where I could get sources? http://www.hydrogenaudio.org/forums/index.php?showtopic=102699 http://www.hydrogenaudio.org/forums/index.php?showtopic=102875 http://www.hydrogenaudio.org/forums/index.php?showtopic=102876 }}} The '''>=''' sign means greater or the same quality.[[BR]] Even though AAC should generally give better quality than MP3 at the same bitrates, libmp3lame should give better quality output than `libfaac`.[[BR]] This list is just a general guide and there may be cases where a codec listed to the right will perform better than one listed to the left at certain bitrates. [[BR]] The highest quality internal/native encoder available in FFmpeg without any external libraries is eac3/ac3. [[BR]] Please note it is '''not recommended''' to use `vorbis` for Vorbis encoding; use `libvorbis` instead.[[BR]] Please note that `wmav1`,`wmav2` and `libvo_aacenc` don't seem to be able to reach transparency at any given bitrate.[[BR]] {{{#!comment Please explain why it is not recommended to use these encoders or link to other wiki pages that do explain. Would personal ABX logs be enough? Encoded with: ffmpeg.exe -i original.wav -acodec wmav2 -b:a 520k test.wma foo_abx 1.3.4 report foobar2000 v1.1.15 2013/04/09 19:18:48 File A: C:\ffmpeg-20121125-git-26c531c-win32-static\bin\exactlylikeyou\original.wav File B: C:\ffmpeg-20121125-git-26c531c-win32-static\bin\exactlylikeyou\test.wma 19:18:48 : Test started. 19:19:31 : 01/01 50.0% 19:19:48 : 02/02 25.0% 19:20:07 : 03/03 12.5% 19:20:26 : 04/04 6.3% 19:20:48 : 05/05 3.1% 19:21:05 : 06/06 1.6% 19:21:36 : 07/07 0.8% 19:22:04 : 08/08 0.4% 19:22:34 : 09/09 0.2% 19:22:59 : 10/10 0.1% 19:23:25 : Test finished. ---------- Total: 10/10 (0.1%) The max bitrate that ffmpeg allowed me to use for libvo_aacenc was 320k: ffmpeg.exe -i original.wav -acodec libvo_aacenc -b:a 320k test.m4a foo_abx 1.3.4 report foobar2000 v1.1.15 2013/04/09 19:32:18 File A: C:\ffmpeg-20121125-git-26c531c-win32-static\bin\exactlylikeyou\original.wav File B: C:\ffmpeg-20121125-git-26c531c-win32-static\bin\exactlylikeyou\test.m4a 19:32:18 : Test started. 19:32:43 : 01/01 50.0% 19:33:19 : 02/02 25.0% 19:33:34 : 03/03 12.5% 19:33:59 : 04/04 6.3% 19:34:27 : 05/05 3.1% 19:34:50 : 06/06 1.6% 19:35:08 : 07/07 0.8% 19:35:36 : 08/08 0.4% 19:35:51 : 09/09 0.2% 19:36:10 : 10/10 0.1% 19:36:13 : Test finished. ---------- Total: 10/10 (0.1%) The first song I tested wasn't even a problematic sample... The reason for not recommending vorbis, is because it doesn't respect the bitrate given. }}} == Container formats == Only certain audio codecs will be able to fit in your target output file. ||'''Container'''||'''Audio formats supported'''|| ||'''MKV/MKA'''||Vorbis, MP2, MP3, LC-AAC, HE-AAC, WMAv1, WMAv2, AC3, eAC3, Opus|| ||'''MP4/M4A'''||MP2, MP3, LC-AAC, HE-AAC, AC3|| ||'''FLV/F4V'''||MP3, LC-AAC, HE-AAC|| ||'''3GP/3G2'''||LC-AAC, HE-AAC|| ||'''MPG'''||MP2, MP3|| ||'''PS/TS Stream'''||MP2, MP3, LC-AAC, HE-AAC, AC3|| ||'''M2TS'''||AC3, eAC3|| ||'''VOB'''||MP2, AC3|| ||'''RMVB'''||Vorbis, HE-AAC|| ||'''WebM'''||Vorbis, Opus|| ||'''OGG'''||Vorbis, Opus|| Please note that there are more container formats available than those listed above. == Recommended minimum bitrates to use == The bitrates listed here assume 2-channel stereo and a sample rate of 44.1kHz or 48kHz. Mono may require fewer bits.[[BR]] - '''libopus''' Usable range >= 80Kbps. Recommended range '''>= 128Kbps'''[[BR]] - '''libfdk_aac default LC-AAC profile.''' Recommended range '''>= 128Kbps''' (http://ffmpeg.org/trac/ffmpeg/wiki/AACEncodingGuide). [[BR]] - '''(libfdk_aac or libaacplus) -profile:a aac_he_v2''' Usable range '''<= 48Kbps CBR'''. Transparency: Does not reach transparency. Use LC-AAC instead to achieve transparency[[BR]] - '''(libfdk_aac or libaacplus) -profile:a aac_he''' Usable range '''>= 48Kbps and <= 80Kbps CBR'''. Transparency: Does not reach transparency. Use LC-AAC instead to achieve transparency[[BR]] - '''libvorbis''' Usable range >= 96Kbps. Recommended range '''-aq 4 (>= 128Kbps)'''[[BR]] - '''libmp3lame''' Usable range >= 128Kbps. Recommended range '''-aq 2 (>= 192Kbps)'''[[BR]] - '''ac3 or eac3''' Usable range >= 160kbps. Recommended range '''>= 160Kbps'''[[BR]] - '''libfaac''' Usable range >= 160kbps. Recommended range '''-q:a 330 -cutoff 15000 (>= 192Kbps)'''.[[BR]] Example of usage: {{{ ffmpeg -i input.wav -c:a libfaac -q:a 330 -cutoff 15000 output.m4a }}} - '''aac''' Usable range >= 192kbps. Recommended rate '''240Kbps'''[[BR]] Example of usage: {{{ ffmpeg -i input.wav -c:a aac -b:a 240k -strict -2 output.m4a }}} {{{#!comment The cutoff is automatically applied since 2012 July, so end users don't need to set -cutoff 15000 manually. Currently(2014 October), higher bitrate such as 320kbps causes trouble. 240kbps is safe. https://ffmpeg.org/trac/ffmpeg/ticket/2686 }}} - '''libtwolame''' Usable range >= 192kbps. Recommended range '''>= 256Kbps'''[[BR]] - '''mp2''' Usable range >= 320kbps. Recommended range '''>= 320Kbps'''[[BR]] The '''vorbis''', '''wmav1/wmav2''' and '''libvo_aacenc''' encoders are not worth using.[[BR]] The '''wmav1/wmav2''' and '''libvo_aacenc''' encoders '''do not reach transparency at any bitrate'''.[[BR]] The '''vorbis''' encoder does not use the bitrate specified in ffmpeg. On some samples it does sound reasonable, but the bitrate is very high.[[BR]][[BR]] To calculate the bitrate to use for multi-channel audio: (bitrate for stereo) x (channels / 2).[[BR]] Example for 5.1(6 channels) Vorbis audio: 128Kbps x (6 / 2) = 384Kbps[[BR]] ---- When compatibility with hardware players doesn't matter then use `libvorbis` in a MKV container rather than `libfaac` when `libfdk_aac` isn't available[[BR]] When compatibility with hardware players does matter then use libmp3lame or ac3 in a MP4/MKV container rather than `libfaac` when `libfdk_aac` isn't available[[BR]] Transparency means the encoded audio sounds indistinguishable from the audio in the source file.[[BR]] [[BR]]