Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bunch of commandline settings that work for handbrake, but sometimes on some videos (I have no idea why) handbrake throws "Out of memory exception". I was wondering if I can find the same option keys and do it with FFmpeg, but it seems not so easy thing to do.

FFmpeg sometimes either doesn't have those options or does stuff by default. For example I don't know how to do custom anamorphic with FFmpeg. For me very is important that resulting videos will be as good as if they're done with handbrake. Here's the line for Handbrake:

HandBrakeCLI.exe --input "d:\input.mpg" --output "d:\output.mp4" --rate 29.97 --cfr --arate 44.1 --format mp4 --width 1920 --height 1080 --custom-anamorphic --display-width 1920 --keep-display-aspect --modulus 8 --crop 0:0:0:0 --encoder x264 --vb 5200 --aencoder faac --ab 128 --mixdown stereo -x bitrate=5200:vbv-bufsize=5200:vbv-maxrate=5200:level=42:bframes=2:min-keyint=60:keyint=60:ref=4:me=umh:merange=64:subme=7:8x8dct:cabac=1  -v3 -2 -a none

How do I turn this to an FFmpeg command?

share|improve this question
up vote 10 down vote accepted

Handbrake uses the x264 encoder for video conversion. There's a site with an overview of all the x264 options and their FFmpeg counterparts. However, these are a little deprecated and some of these options don't work anymore or might have been renamed. You should use the x264 profiles for most of the detail options—but we'll get there.

Many of the basic Handbrake options (e.g. frame rate) aren't specific to x264, and they are scattered across the FFmpeg online documentation.

Before we start, please check that you use a recent FFmpeg version, not any of the broken kind they deliver with Ubuntu. Their download site has static builds for all major operating systems.

Let's go:

  • --input "d:\input.mpg" --output "d:\output.mp4"

    The FFmpeg syntax is ffmpeg -i input.mpg … d:\output.mp4.

  • --rate 29.97

    Specify the video frame rate with -r 29.97. (Handbrake's --cfr just sets a constant frame rate. Not needed here).

  • --arate 44.1

    Set the audio sample rate with -ar 44100 (in Hz).

  • --format mp4

    Not needed because FFmpeg knows you want MP4.

  • --width 1920 --height 1080 --custom-anamorphic --display-width 1920 --keep-display-aspect --modulus 8 --crop 0:0:0:0

    This doesn't do anything apart from setting the size to 1920×1080. In FFmpeg, do it with -s:v 1920x1080, or if you want to use the filters, -filter:v "scale=1920:1080".

  • --encoder x264 --vb 5200

    Set the encoder with -c:v libx264 and the bit rate to constant with -b:v 5200K. Note that we're not there yet when it comes to quality settings.

  • --aencoder faac --ab 128 --mixdown stereo

    Similar to above, with -c:a libfaac -b:a 128K -ac 2. Note that FAAC has a better mode than constant bitrate for encoding: Variable bitrate. You invoke it by replacing -b:a 128K with -q:a 100, where the default quality is 100%.

    If you use a static build, note that these do not have FAAC for licensing reasons. You need another encoder here, e.g. the built-in one. Use -c:a aac -strict experimental -b:a 192K instead. Higher bit rate chosen because the native AAC encoder doesn't give as good results as FAAC.

  • -x bitrate=5200:vbv-bufsize=5200:vbv-maxrate=5200

    This sets the encoding mode to VBV which targets a streaming scenario instead of just a constant bitrate or constant quality. Constant quality is also called CRF, the default for x264. While CRF usually gives you the best quality encodes, the VBV mode gives you a file that is better suited for streaming online.

    To set this, use the -maxrate 5200K and -bufsize 5200K options.

  • level=42

    Use -level 42. This is the H.264 Level.

  • bframes=2:min-keyint=60:keyint=60:ref=4

    Set the number of B-Frames with -bf 2. The minimum keyframe interval is set with -keyint_min 60, and the maximum interval with -g 60. The number of reference pictures is set with -refs 4.

  • me=umh:merange=64:subme=7:8x8dct:cabac=1

    Here's the version with the old-style options: The motion estimation method is set with -me_method umh, and the search range with -me_range 64. Subpixel motion searching is set with -subq 7. The 8×8 DCT allows x264 to choose a different transform matrix size. Set it with -8x8dct (although I didn't get this to work). CABAC should be enabled by default.

    Here's the "TL;DR" version if you're lazy, don't care about the details, or if things don't work: Use -preset:v medium (or leave it out, as the medium preset is default). The x264 presets choose a set of options that trade encoding speed for compression efficiency. They're conveniently named from ultrafast, superfast, veryfast, faster, fast, medium to slow, slower and veryslow. Choose whatever you feel comfortable with.

So, to summarize, the long version:

ffmpeg -i input.mpg -c:v libx264 -r 29.97 -s:v 1920x1080 -b:v 5200K -maxrate 5200K -bufsize 5200K -level 42 -bf 2 -keyint_min 60 -g 60 -refs 4 -me_method umh -me_range 64 -subq 7 -c:a libfaac -b:a 128K -ar 44100 d:\output.mp4

Or the short one I'd use (with VBV encoding and a few detail options removed):

ffmpeg -i input.mpg -c:v libx264 -r 29.97 -s:v 1920x1080 -b:v 5200K -maxrate 5200K -bufsize 5200K -c:a libfaac -q:a 128K -ar 44100 d:\output.mp4

Changing to constant quality encoding for an all-purpose conversion, use this and change the -crf parameter from 18 to 28, where lower means better quality and 23 is default:

ffmpeg -i input.mpg -c:v libx264 -r 29.97 -s:v 1920x1080 -crf 23 -c:a libfaac -q:a 128K -ar 44100 d:\output.mp4
share|improve this answer
Haha @slhck, just as I was putting the finishing touches on my answer, you go and give a better one. – evilsoup Feb 13 '13 at 21:12
@evilsoup Oh, shoot. I hope that wasn't a big waste of time :/ If you spot any mistake here let me know though. – slhck Feb 13 '13 at 21:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.