Video Encoding

“When I say, ‘Quality cannot be defined,’ I’m really saying formally, ‘I’m stupid about Quality’.”

FFmpeg VBR Settings

There are various FFmpeg encoders that support variable bit rate / constant quality encoding. This gives you a much better overall quality when file size or average bit rate are not constrained (e.g. in a streaming scenario). Variable bit rate is usually achieved by setting -q:v (or -q:a for audio) instead of -b:v (or -b:a), which just sets a constant bit rate.

The problem is that every encoder uses a different range of values to set the quality—and they're hard to memorize. This is an attempt to summarize the most important ones.

Notes for reading this table:

  • Qmin stands for the setting to be used for achieving lowest quality and Qmax for highest. These are not just lowest and highest values.
  • Qdef is the default value chosen if no other is specified. This means that (most?) encoders will use one or the other VBR mode by default, e.g. libx264. I wasn't able to research whether this applies to all encoders.
  • Some encoders use private options instead of the regular -q. Read the second column Param for the correct option to use.
  • Rows highlighted with green refer to encoders that allow you to use VBR. Rows in yellow aren't really VBR or I simply couldn't find out whether they support it. Rows in red mean: No VBR support.
Video
Encoder Param Qmin Qmax Qdef Recommended Notes
libx264 -crf 51 0 23 18–28 Values of ±6 result in double/half avg. bitrate. 0 is lossless.
Specifying -profile:v lets you adjust coding efficiency. See H.264 Encoding Guide.
libx265 -crf 51 0 28 24–34 Values of ±6 result in double/half avg. bitrate. 0 is lossless.
Specifying -profile:v lets you adjust coding efficiency. See H.265 Encoding Guide and x265 docs.
libxvid -q:v 31 1 n/a 3–5 2 is visually lossless. Doubling the value results in half the bitrate. Don't use 1, as it wastes space.
No VBR by default—it uses -b:v 200K unless specified otherwise.
libtheora -q:v 0 10 n/a 7 No VBR by default—it uses -b:v 200K unless specified otherwise.
libvpx -qmin
-qmax
-crf
-b:v
63 0 10 -qmin: 0–4
-qmax: 50–63
-crf: 10
-b:v sets target bitrate, or maximum bitrate when -crf is set (enables CQ mode). Default target is 1 MBit/s.
See also VBR, CBR and CQ Mode and FFmpeg Wiki. Setting -maxrate and -bufsize is also possible.
mpeg1, mpeg2, mpeg4, flv, h263, h263+, msmpeg+ -q:v 31 1 ? 3–5 2 is visually lossless. Doubling the value results in half the bitrate.
-q:v works for mpeg4, but haven't tested others.
prores -profile:v 0 3 2 Depends Not VBR. Corresponds to the profiles Proxy, LT, Std, HQ.
ProRes might support -q:v?
Target bitrates are in the ProRes Whitepaper.
Audio
Encoder Param Qmin Qmax Qdef Recommended Notes
libfdk_aac -vbr 1 5 ? 4 (~128kbps) Currently the highest quality encoder.
libfaac -q:a 10 500 100 100 (~120kbps) A good quality encoder.
libvorbis -q:a 0 10 3 4 (~128kbps) Make sure not to use vorbis, which is the (bad) internal encoder.
libmp3lame -q:a 9 0 4 2 (~190kbps) Corresponds to lame -V. See FFmpeg Wiki.
aac -q:a 0.1 ? ? 1 (~128kbps) Can only be used with -strict experimental flag
The setting is still broken, so do some listening tests after encoding.
libopus -vbr ? ? ? ? You need to set -vbr on or -vbr constrained, otherwise Opus defaults to 96 kBit/s. Currently VBR is not working.
libaacplus VBR not available?
libvo_aacenc VBR not available.

Please let me know through slhck at me.com if there's anything wrong or if you're missing some values.
Thanks to @LordNeckbeard and @evilsoup on Super User for providing additional input on this.


Articles

Some important external resources for x264 and FFmpeg:


FFmpeg Command Examples

First port of call: The always up-to-date FFmpeg documentation. The documentation is split into multiple files, see Encoders and Decoders and Muxers and Demuxers for the most important stuff.

Overwriting the audio track of an input file:

ffmpeg -i input.mp4 -i input.wav -c:v copy -c:a libfaac -map 0:0 -map 1:0 output.mp4

Resizing, keeping aspect ratio (-1 is the important part here):

ffmpeg -i input.mp4 -c:v libx264 -c:a copy -filter:v "scale=-1:720" output.mp4

Cutting video, possibly inaccurate version:

ffmpeg -ss 00:01:00.123 -i input.mp4 -ss 0 -t 00:05:00.456 -c copy -map 0 output.mp4

Cutting video, more accurate version:

ffmpeg -i input.mp4 -ss 00:01:00.123 -t 00:05:00.456 -c copy -map 0 output.mp4

Removing subtitles from any file:

ffmpeg -i input.mp4 -c copy -sn output.mp4

Adding subtitles to a file (MKV likes everything):

ffmpeg -i input.mp4 -i subtitles.srt -c copy -map 0 -map 1 output.mkv

Links to other examples: