I ❤ ffmpeg, part 3

By far the best thing about ffmpeg is actually an entirely separate project: x264. Without a doubt, x264 is a crown jewel of the open source community. It regularly dominated H.264 encoding performance (both in terms of speed and quality), solidly trouncing commercial offerings.

A couple handy tricks with this library — as I mentioned a few posts ago, you can rapidly make a high quality copy of something using the ultrafast preset, like so:

ffmpeg -i <some_file> -c:v libx264 -crf 18 -preset ultrafast -c:a libopus <some_output_file>.mkv

-crf specifies a constant rate factor — lower means higher quality video, but higher bitrate. Higher means lower quality at the expense of bitrate. -preset specifies a bundle of encoding parameters; it’s basically a convenient short-hand.

-crf 18 is, for all intents and purposes, nearly lossless. -preset ultrafast is blisteringly quick, due to x264’s very heavy assembly optimizations.

Another thing I love is x264 has really sophisticated rate control. For example, you can ask x264 to hit a quality target but cap at a maximum bitrate. Example:

ffmpeg -i <some_file> -c:v libx264 -crf 22 -maxrate 1000k <some_output_file>

Why would you want to do this? This is awesome if you don’t know how many bits you reliably need to use to process the incoming video, but still need a capped bitrate (say, because you’re distributing video online). This dramatically cuts down the size of most files, but gives you protection against generating utterly massive files at the expense of quality.

Regarding -preset, in descending order of speed are: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo. The default preset is medium.

For most encoding, I find medium or slow to be appropriate. If you’re authoring something for long-term storage/distribution, it’s likely worth the hit for the veryslow preset. placebo shouldn’t be used. For infrequently accessed video (so not streamed often) or short-term video, faster, fast and medium are good options.

If you’re going to be processing an absurd amount of video with a common theme/style, it may be worth testing and overriding individual features set by a preset.