7298 Logo

Better compression with x264

x264 logo

If you’re going to do compression, FFMPEG and x264 is the bomb dot com. x264 the most highly praised H.264 encoder, used by the big guns like Amazon, Encoding.com, Zencoder, YouTube, Netflix and so on. 1 x264 is far superior to commercial offerings from Apple or Adobe, and is even better than MainConcept’s encoder.

x264, through FFMPEG, gives you a lot of power and a range of options in an technically documented, complicated command line. While you could hotrod Compressor by installing the x264 codec (and I’ve done it), the install is a few years out of date, and it lacks the total control you’d need to set all the x264 parameters. Thankfully, there’s another way to get at the mighty x264 encoder, in a cross-platform, free and easy to use GUI: Handbrake.

Handbrake comes with a whole truckload of presets, for encoding to all kinds of devices. Handbrake presents itself a video ripping tool, but it’s also a great tool for encoding for the web. It can create the leanest files to conserve bandwidth and storage space. If you’re like me and you want your videos to look good too, you’re going to have to contend with the eternal three-way dilemma: fast, good and cheap (in this case, cheap means lean files), and you can pick only two. In this case, we’re picking good and lean, so these encodes won’t be fast.

Not to say that the encodes will be dog slow. It’s just that they won’t be as fast as Apple’s Compressor, or Adobe’s Media Encoder, both of which make a lot of compromises in quality to make speedy encodes. The nice thing is, Handbrake and it’s underlying FFMPEG/x264 engine is extremely processor efficient. It’ll gobble up as much CPU as you can throw at it.

If you look at the advanced settings in Handbrake, you’ll see all the x264 encoder’s options. To really use x264 effectively, you’ve got to play those options like a master pianist.

I’m not a master pianist, or a master encoder. But I do have the Web connected to my computer, and on the web is this guy, Fabio Sonnati, who just happens to be the Master Encoder. 2

So, with Fabio as a guide, 3 I’ve built a set of Handbrake presets, using the optimal settings in x264 to produce the cleanest, highest fidelity, and leanest compressed MPEG4 files I can make.

Just take a look at what they can do. Click each image to enlarge.

Casey
original

Original file, for comparison.6

Casey
compressor

Encoded with Apple Compressor, 1280x720, 2209kbps. Lots of macroblocks around the sparkler bit by her hair, macroblocks on chin and cheek as the light falls off, and in the blue sky.

Casey Adobe Media
Encoder

Encoded with Adobe Media Encoder, 1280x720, 2209kbps. Smoother than Compressor, but the sparkler is still pretty fuzzy and blurred around the edges. Still some ‘blocking around the chin, cheek and sky.

Casey
Fabio

Encoded with Handbrake, using Fabio preset, 1280x720, 2209kbps. Much more detail in the sparkler and smoother in the shaded cheek, chin and sky.

I call them the Fabio presets. They come in two flavours: multi-bitrate, for making the best quality per pixel own encodes and YouTube, for the best quality for upload to a service that will do a re-encode like YouTube, Vimeo, Kaltura and the like.4 You can get them here.

Multi-Bitrate

The multi-bitrate presets are set up for multiple image sizes and bitrates, and for two different frame rates: 29.97 and 23.974. Image sizes are:

Data rates are determined by the bit-per-pixel values using Jan Ozer’s suggestions of values from 0.080 to 0.175. 5 Use these presets as a starting point, because every video is different; some with more detail and more or less movement. If your video gets artifacted and you need to bump up the data rate, use my Bitrate calcuator, and plug in a new ratio (0.080, 0.100, 0.125 or 0.150), according to Jan’s chart. Remember to set your frame rate, 29.97 ot 23.976, as well as your frame size as well.

Handbrake does offer some tuning of the FFMPEG engine, under video -> advanced options, to add in all of Fabio's recommended settings,3 but the encoder speed presets do pretty much the same thing. So for ease and reliability, I've set everything to very slow, which adds in all the bframes, 8x8's, trellising, etc., and set the profiles according to Fabio's recommendations.

YouTube

The YouTube presets are set up for:

Both these presets use a high (but not outrageously high) data rate that seems like a sweet spot for quality vs bandwith (because we all got to wait for upload).

I warn you, these presets will take time to render a file, but good encoding takes time: time to do all the 2-pass through the video to find the best place to put the bits, time to do all the complicated calculations, time to do the slight de-noise and time to do the image resizing.

So there you go. For the low, low price of no cost at all and if you’re willing to be patient while waiting for the progress bar, you can encode videos leaner and better looking than YouTube if you're hosting your own, or if you're uploading to a service, giving them the best of the best to demolish with their encode.

Notes

  1. See “Buyer's Guide to Cloud-Based Video Encoding and Transcoding 2015”, especially the section, “HOW'S THE QUALITY?”, http://www.streamingmedia.com/Articles/Editorial/Featured-Articles/Buyers-Guide-to-Cloud-Based-Video-Encoding-and-Transcoding-2015-102483.aspx

  2. If x264 is the bomb, Fabio is the igniter. Check out his blog at https://sonnati.wordpress.com. You won’t be sorry. 

  3. I’m using Fabio’s recommendations on efficent encoding from “Pushing H.264 to the limit - Part II”, hhttps://sonnati.wordpress.com/2008/02/13/pushing-h-264-to-the-limit-part-ii/, and “FFmpeg - the swiss army knife of Internet Streaming - part III”, https://sonnati.wordpress.com/2011/08/19/ffmpeg-–-the-swiss-army-knife-of-internet-streaming-–-part-iii/ , and from his presentation at Adobe Max Europe, Encoding video for the highest Quality.

  4. Why you might not want to use all of Multi-Bitrate: Encoding multiple bitrates and image dimensions is a common approach to scaling video delivery for multiple platforms and variations on bandwidth. But it sucks. It sucks storage space (multiple files), it sucks encoding time (multiple passes to encode all the variations on the same video), and it sucks on delivery (each file is a compromise to the best image that you want to deliver, the best looking, best resolution one.) Fabio’s idea: why not just encode for performance (highest image size and lowest bandwidth with lowest compression artifacts) on targeted devices (mobile and desktop) So, why not pick the best two? One for mobile and one for Desktop. See “Encoding for performance on multiple devices”, Adobe Max 2011 presentation, available at, https://sonnati.wordpress.com/2011/10/10/my-presentation-at-max2011-is-available-on-adobe-tv/

  5. See Jan Ozer’s article, “How to Produce High-Quality H.264 Video Files”, on streamingmedia.com, http://www.streamingmedia.com/Articles/Editorial/Featured-Articles/How-to-Produce-High-Quality-H.264-Video-Files-94216.aspx. Jan suggests the following data rate ratios: 0.080, 0.100, 0.125, 0.150, and 0.175. 

  6. Footage is of Casey Burgess from “Afterglow”, shot by John Brawley as a demo of the Blackmagic Cinema Camera, http://forum.blackmagicdesign.com/viewtopic.php?f=2&t=15. That’s my own color grade of the RAW footage, using Color Finesse in After Effects.