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 FLAC [ https://superuser.com/questions/339023#339031 ] or TrueHD (last one needs "-strict -2").
Generation loss
Transcoding from a lossy format like MP3, AAC, Vorbis, Opus, WMA, etc. to the same or different lossy format might degrade the audio quality even if the bit rate stays the same (or higher). This quality degradation might not be audible to you but it might be audible to others.
͏ This post demonstrates what will happen if you re-encode a file 100 times:
͏ 9 different codecs 100-pass recompression test
͏ https://hydrogenaud.io/?topic=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 any codec.
͏ This can be achieved by specifying "copy" as the audio codec.
͏ E.g.
͏ Transcoding a WebM file (VP8 video, Vorbis audio) to MKV (H.264 video, with the very audio):
͏ ffmpeg -i "in.webm" -c:a copy -c:v libx264 "out.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 bit rate 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.
͏ Here are some popular lossy formats with encoders listed that FFmpeg can use:
͏ Opus: "libopus"
͏ Vorbis: "libvorbis", "vorbis"
͏ AAC LC: "libfdk_aac", "aac"
͏ HE-AAC: "libfdk_aac"
͏ MP3: "libmp3lame", "libshine"
͏ Dolby Digital: "ac3"
͏ Dolby Digital Plus: "eac3"
͏ TrueHD 0xFBA: "truehd"
͏ MP2: "libtwolame", "mp2"
͏ Windows Media Audio 1: "wmav1"
͏ Windows Media Audio 2: "wmav2"
͏ Based on quality produced from high to low:
͏ "libopus" > "libvorbis" >= "libfdk_aac" > "libmp3lame" >= "eac3"/"ac3" > "aac" > "libtwolame" > "vorbis" > "mp2" > "wmav2"/"wmav1"
͏ The ">=" sign means greater or the same quality.
͏ 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 bit rates.
͏ The highest quality internal/native encoder available in FFmpeg without any external libraries is "aac".
Please note:
- It is not recommended to use the experimental "vorbis" for Vorbis encoding; use "libvorbis" instead.
- "wmav1" and "wmav2" don't seem to be able to reach transparency at any given bit rate.
Container formats
͏ Only certain audio codecs will be able to fit in your target output file:
͏ Container: Audio formats supported
͏ MKV/MKA: Opus, Vorbis, MP2, MP3, LC-AAC, HE-AAC, WMAv1, WMAv2, AC3, E-AC3, TrueHD
͏ MP4/M4A: Opus, MP2, MP3, LC-AAC, HE-AAC, AC3, E-AC3, TrueHD
͏ 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, TrueHD
͏ M2TS: AC3, E-AC3, TrueHD
͏ VOB: MP2, AC3
͏ RMVB: Vorbis, HE-AAC
͏ WebM: Vorbis, Opus
͏ OGG: Vorbis, Opus
͏ There are more container formats available than those listed above, like MXF.
͏ Also, E-AC3 is only officially (according to Dolby) supported in MP4 (for example, E-AC3 needs editlist to remove padding of initial 256 silence samples).
Recommended minimum bit rates to use
͏ The bit rates listed here assume Stereo (2 ch) and sample rate of 44.1 KHz or 48 KHz.
͏ Mono, speech, and quiet audio tend to require fewer bits.
- "libopus" - Usable range ≥ 32 Kbps. Recommended range ≥ 64 Kbps.
Transparency (music): very close at default (96 Kbps), but takes more (160+) to be real transparent.
Transparency (speech): ~ 32 Kbps.
See also: https://wiki.hydrogenaud.io/?title=Opus - "libfdk_aac" (default profile AAC LC) - Recommended range ≥ 128 Kbps; see <Encode/AAC>.
- "libfdk_aac" -profile:a aac_he_v2 - Usable range ≤ 48 Kbps CBR.
Transparency: Does not reach transparency. Use AAC LC instead to achieve transparency. - "libfdk_aac" -profile:a aac_he - Usable range ≥ 48 Kbps and ≤ 80 Kbps CBR.
Transparency: Ditto. - "libvorbis" - Usable range ≥ 96 Kbps. Recommended range "-aq 4" (≥ 128 Kbps).
Neither high quality nor widely supported, avoid. - "libmp3lame" - Usable range ≥ 128 Kbps. Recommended range "-aq 2" (≥ 192 Kbps)
- "ac3" or "eac3" - Usable range ≥ 160 Kbps. Recommended range ≥ 160 Kbps
E.g.ffmpeg -i "${In}" -c:a ac3 -b:a 160k "${Out}"
- "aac" - Usable range ≥ 32 Kbps (depending on profile and audio). Recommended range ≥ 128 Kbps
E.g.ffmpeg -i "${In}" -c:a aac -b:a 128k "${Out}"
- "libtwolame" - Usable range ≥ 192 Kbps. Recommended range ≥ 256 Kbps
- "mp2" - Usable range ≥ 320 Kbps. Recommended range ≥ 320 Kbps
͏ "vorbis" and "wmav1"/"wmav2" encoders are not worth using.
͏ "wmav1"/"wmav2" encoder does not reach transparency at any bit rate.
͏ "vorbis" encoder does not use the bit rate specified in FFmpeg:
͏ On some samples it does sound reasonable, but the bit rate is very high.
͏ To estimate the bit rate for multi-channel audio:
͏ ${Stereo Bitrate}/2 * ${Channel#}
͏ .
͏ E.g. 5.1 (6 ch) Vorbis audio: 128/2 * 6 = 384 Kbps
- When compatibility with hardware players doesn't matter then use "libopus" in a MKV container when "libfdk_aac" isn't available.
- When compatibility with hardware players does matter then use "libmp3lame" or "ac3" in a MP4/MKV container when "libfdk_aac" isn't available.
Transparency means the encoded audio sounds indistinguishable from the audio in the source file.
͏ Some codecs have a more efficient Variable Bit Rate (VBR) mode: which optimizes to a given, constant quality level; rather than having variable quality at a given, Constant Bit Rate (CBR).
͏ The info above mainly targeted CBR.
͏ VBR is more efficient than CBR, but may not be as hardware-compatible. (mainly applies to antique formats: irrelevant Opus alike)
Resources
- https://opus-codec.org/comparison/
- Google Listening Test #1 - comparing Opus, Speex, and others
https://opus-codec.org/static/comparison/GoogleTest1.pdf - Google Listening Test #2
https://opus-codec.org/static/comparison/GoogleTest2.pdf
- [2011] 64 Kbps Stereo multi-format listening test
https://web.archive.org/web/20200202080247/https://people.xiph.org/~greg/opus/ha2011/
^ Of Opus, aoTuV Vorbis, 2 HE-AAC encoders, and a 48 Kbps AAC-LC low anchor. - [201407] Results of the public multi-format listening test
https://listening-test.coresv.net/results.htm#list9
^ Opus, AAC, Vorbis at 96 Kbps against typical MP3 128 Kbps.