Home > Miscellaneous > The best H.264 / x264 settings for Handbrake

The best H.264 / x264 settings for Handbrake

September 12th, 2009

I wrote this up quite a while ago, and it’s been sitting as a draft since. I pulled information on each setting from a few different websites, and while I don’t have any “final results” to show you (and the write-up wasn’t quite completed), hopefully the information’s useful in helping you to better determine what each setting does (and how it will affect encode time and quality), and to come up with your own settings.

I’ve used Handbrake in the past to encode movies and various TV series from DVD source. Recently, I grabbed the latest version (0.9.3), and decided to encode a TV series from DVD.

In short, here’s what I’m looking to get from my encode:
-High quality, even if it takes a while to encode.
-File size of between 250-400mb for each 1-hour episde (about 42-44 mins).

Of course, the best way to see how well you’ve done is to find something to compare it to. Therefore, I grabbed a torrent of the same TV show, and aimed to create a better quality encode at a smaller file size.

Before dumping into the x264 settings, it’s worth noting that I used a 2-pass encode, and mixed the audio down to a 160kbps Dolby Surround encode.

Here’s a list of the x264 settings I chose in Handbrake, and why:

Reference Frames: 6
Normally, I’d go much higher here. However if you go too high, both hardware and *some* software players can choke. Unfortunately, reference frames are subject to diminishing returns. According to a document at mplayerhq.hu , while going from a setting of 1 to 2 would improve the signal-to-noise by 0.15dB, going from 6 to 12 would typically improve the signal-to-noise ratio by only 0.02dB but result in a 15-20% longer encoding time. Really high diminishing returns for something you probably won’t notice, and something that might cause issues if you ever try to play your video in a hardware player. That said, go crazy if you want and try 16. Just make sure your video plays fine afterwards.

Mixed References: Yes
You get some quality at the cost of some speed. I’ve never seen anyone give a good reason to turn it off.

B-Frames: 6
There’s another option called “adaptive b-frames” which is on by default (and there’s no setting to turn it off in Handbrake), so the encoder will use however many B-Frames it sees fit to use. Typically it won’t use more than 3 or 4. Feel free to pump this up to 16 if you want – it probably won’t make a lick of difference though unless for some reason it decides that more than 3-4 was needed.

Direct Prediciton: Automatic
None is bad, and if you’re intent on choosing one, Spacial should be better than Temporal according to the documentation. Everyone seems to point at leaving this at Auto so the encoder can decide when to use what, and I’d tend to stick with that.

Weighted B-Frames: Yes
A fun bit from mplayerhq.hu – there’s often not a lot to gain from this except during fades where it’ll save some bitrate… except that apparantly the “adaptive b-frames” that’s on by default tends to avoid b-frames during fades. So where it should help the most, it probably doesn’t. It can save some bitrate though anyway, and some people have noticed 1-2% larger file sizes without it (which means you should get an overall quality increase with a static file size in most cases, since I haven’t heard of any negative effects). It also doesn’t add much time to have enabled, so I’d leave it on. Should have a larger effect on film than it does on animation.

Pyramidal B-Frames: Yes
More quality, or a smaller bitrate. Adds time to the encode.

Motion Estimation Method: Uneven Multi-Hexagon (umh)
UMH will typically give the best quality. Exhaustive would take much longer, but probably wouldn’t give any benefit whatsoever.

Motion Estimation Range: 64
Higher is better, but takes longer to encode. More important for high-motion stuff.

Subpixel Motion Estimation: 9
Again, higher is better. Rather than just being “levels of quality”, each number turns on different features, or applies features to different/more frame types.

Analysis: All
About 10-15% longer encodes, about 0.1db improvement in signal to noise in high-motion stuff, and does almost nothing in low-motion stuff. Despite the rather small benefit, I use it anyway.

8×8 DCT: Yes
Simply adds another block size to the Analysis. If you have Analysis already set to “all”, you may as well choose this also.

Deblocking: 0 0
There’s no “best” setting for this, so the default is probably fine for most. The first number is “strength”, the second is “threshhold”. Think of it this way – the first is how much to blur, and the second is more like “when” to blur. Negative numbers will blur less and give a sharper image. Postive numbers will give more blur. If you’ve chosen an insanely low bitrate/filesize, or if the image looks too sharp or quite blocky, you may want to increase the numbers slightly to add some blur to help cover up any artifacting/blocking. If you’ve gone with a healthy bitrate and your encode is more blurry than the source, you can try going into the minuses. It’s generally accepted that going beyond +/- 3 won’t be terribly good. Really, I’d leave it at 0/0 unless you’re unhappy with the result.

Trellis: 2
2 uses Trellis more often than 1, will take longer to encode, and may increase the quality a bit.

No Fast P-Skip: Yes
Selecting this makes it take a little longer to encode, but can reduce blockiness. Leave it checked.

No DCT-Decimate: No
DCT-Decimate is usually a good thing, so leave this box unselected. It saves space where it thinks you won’t notice, which means more space is there to increase quality elsewhere (where you will notice it).

CABAC Entropy Coding: Yes
Better compression at the same quality. No Fast P-Skip also needs this to work. Playback requires a more work though, so old computers and some hardware players may choke. Keep it enabled unless you’re going to play the video on something that can’t cope.

EyeglassRetailerReviews.com ad

  1. Johan
    October 29th, 2009 at 11:46 | #1

    Thanx for this article. I’m not very good at encoding DVDs. But I have noticed that when I use Handbreak my TV series episodes will be about 1.3 Gb. But when I download an episode from the net it use to be an ~350Mb avi-file. And this avi-file use to have better quality then my 1,3 Gb file.
    I’m very happy someone is telling me what settings to use. I’m looking for the same things as you:
    -High quality, even if it takes a while to encode.
    -File size of between 250-400mb for each 1-hour episde (about 42-44 mins).

    Now I would like to try your settings. But I don’t know how. The settings you write about seems to be under the “Advanced”-tab. But what about all the other settings?

    What format? avi, mkv, mp4 etc.?
    What framerate?
    And what is your “Quality settings”?
    Do you use any picture settings?

    I don’t even know what all these words means but I would appreciate if you could tell me how you use handbreak.

    //Johan

    • October 29th, 2009 at 14:30 | #2

      Johan:

      The format is basically a “container”. AVI and MP4 are the most commonly supported types, and I generally go with MP4 (the others are fine too, but some players won’t play them).

      For framerate, choose “Same as Source”.

      Under quality, type in a Target Size (so 350 for example if you want a 350mb file). Leave the other stuff alone – they’re simply other methods you can use if you’d rather have a target bitrate or target quality (if you change them, it won’t end up being a 350mb file). Since hard drives have become so cheap, I use 400mb a little more often now, but it’s up to you – find something you’re comfortable with. The higher the filesize, the more quality can be packed in.

      For advanced encoding settings, use 2-pass. Very important, and it’ll effect quality a fair bit. You may as well select Turbo First Pass as well (it’ll encode a lot faster and the difference in quality is negligible)

      I leave the picture settings alone. If the source is interlaced for some reason, you’ll see really bad “jaggies” in your output video – if that happens you may need to choose a Deinterlace option. Usually it’s not needed though, so just leave it be.

      For the audio bitrate, use either AAC or MP3. You can drop the audio bitrate a little too (drop it to 128 instead of 160), and see if you notice the difference in sound. If you don’t notice any difference, it’ll save a little space there (which can be used for a little more video quality).

      —-

      The quality of the encode will usually be much better than the one you downloaded – more sharp and crisp, and generally pretty close to the source DVD. However, many videos that you download are encoded with DivX or XviD which both give a very smooth picture (basically it’s slightly blurred which makes it look smooth, and which some people prefer). The deblocking stuff mentioned in the article can mirror this to an extent.

      If worse comes to worse, you can try using some of the built-in presets. They’re usually pretty decent, although I prefer the manual settings.

      Best of luck!

  2. Johan
    November 2nd, 2009 at 09:04 | #3

    A big thank you to you! This have been the most helpful information I’ve ever got on this topic. Now I will encode the complete boxset of “Six Feet Under”. It will take a while but hopeflly worth it. Thanx!

  3. Victor
    November 15th, 2009 at 06:13 | #4

    Hello Matt,

    One question, when selecting Output Settings, I have been using the MKV format. Should I be using another setting?

    Thanks…

    • November 15th, 2009 at 15:45 | #5

      Victor,

      The short answer would be that I’d probably tend to use MP4 (MPEG-4) instead of MKV (Matroska), for the simple reason that more players (hardware and software) support MP4. H.264 also happens to be part 10 of the MPEG-4 standard.

      That said, if you’ve been using MKV, and it’s playing on whatever video player you’re using, there’s certainly nothing wrong with the MKV container (it’s a pretty flexible container actually and as an added bonus is an open standard), so feel free to keep using it.

      The longer answer is that all the formats listed in Handbrake are basically just “containers” and for all intents and purposes just determine what sorta stuff you can put in them. For example, if you switch through the different formats available in HB, you might notice that different video & audio codecs become available to use. As long as you’re using a container that lets you stick in H.264/x264 and the audio codec you’re wanting to use, the quality of the encode should be exactly the same, so as long as whatever video player you’re going to play the video on supports whatever container you decide to use, it really doesn’t matter.

      It might be worth taking a quick peak at the table located at http://en.wikipedia.org/wiki/Comparison_of_container_formats . It might help clear up some of the differences between each container.

      What it really boils down to though is this:
      1) Does the container let me put in the stuff I want?
      2) Is the container supported by the player I’m trying to play it on?

      If the answers to both those are yes, then whatever container you’re looking at is probably just fine.

      Hope this helps :)

  4. Stephen
    November 19th, 2009 at 16:50 | #6

    Many thanks for all of your conversion expertise!

    Nice work!

  5. Chris
    November 24th, 2009 at 16:45 | #7

    This is great! I don’t suppose you have it as the -x command line argument?

  6. February 26th, 2010 at 23:57 | #8

    subme=9:trellis=2:ref=6:bframes=6:direct=auto:b-adapt=2:me=umh:merange=64:analyse=all:no-fast-pskip=1

  7. creamyhorror
    April 15th, 2010 at 09:19 | #9

    Good guide. merange doesn’t need to be so high – apparently 32 is already too high to make a difference, so I stick with 16-24. And constant quality (CRF, i.e. quality-based encoding) is a very good method that would suit many users better than 2-pass, so you might want to consider it for future encodes. With 2-pass you have to estimate and try bitrates, but under CRF x264 determines the bitrate for you based on a quality level you specify. There’s virtually no difference in quality between CRF and 2-pass.

  8. EBGwd
    May 23rd, 2010 at 06:20 | #10

    Thanks for all the detail and answers to configuration settings for Handbrake. What version of Handbrake are you using?

    Is there a way to configure Handbrake to get the video to “fit” my LCD TV? I am copying my DVD collection and want to view then on my 46″ LCD. The LCD will “make the frame fit” but is there a way to make the convert size the video to the exact correct size?
    THX, EB

    • May 23rd, 2010 at 16:13 | #11

      EBGwd:

      I’m guessing you’re meaning 1 of 2 things by “fit”:

      -Either you’ve got black bars on the top/bottom (or left/right), or;
      -You’ve got the video showing in a small box (surrounded by black on every side)

      If it’s the bars:
      -Cropping can chop off the edges of the video, so that when it fills the TV there aren’t black bars. While it makes the video fill the screen, I’m not a big fan of this, because whatever was filmed in those edges isn’t on the TV anymore. This should be doable in Handbrake.

      If it’s the video showing in a small box on the TV (surrounded by black on every side):
      -You might be able to upscale the resolution in Handbrake, although I wouldn’t recommend it because there are better ways of dealing with it. To do it, you’d find the resolution of your LCD TV, and encode the video to that resolution. For example, if your TV is 1080p, the resolution is 1920×1080. You’d plug those numbers into Handbrake. I haven’t tried it though and I wouldn’t want to. Because 1920×1080 is significantly larger than a DVD resolution (~720×480), your file will be significantly larger, to the point where it might be larger than the original DVD itself!
      -The best option is to deal with it at the source which reads your files and outputs them to the TV. If you’re using an HTPC to play the files (Windows Media Center, playing off your Mac, etc), your TV resolution should usually be detected correctly, but if it isn’t you might have to change the resolution from within Windows/Mac. Alternately, if you’re playing your ripped movies off of a DVD/CD in your player, there might be an option there to upscale to fit the TV, although it varies from manufacturer to manufacturer. You also want to make sure you’re using a digital cable of some sort (DVI/HDMI cable for instance) regardless of the source – component outputs (RCA cables) aren’t ideal for video because they won’t tell the source&TV about each other (which can lead to the issues) and don’t offer great video quality either, and standard VGA cables are getting to be a little passe (also not great for quality).
      -Finally if nothing else works, doing it from the TV the way you’ve been doing it is probably your best option. Upscaling from Handbrake will result in massive video files, and the only other option if you’re playing from a hardware player is to buy a new hardware player that scales it correctly to fit your screen.

      Best of luck. Incidentally, the version of Handbrake I had used in the testing was 0.9.3. I’m currently using 0.9.4.

  1. No trackbacks yet.

Powered by CDN Rewrites