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.
--input "d:\input.mpg" --output "d:\output.mp4"
The FFmpeg syntax is
ffmpeg -i input.mpg … d:\output.mp4.
Specify the video frame rate with
-r 29.97. (Handbrake's
--cfr just sets a constant frame rate. Not needed here).
Set the audio sample rate with
-ar 44100 (in Hz).
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,
--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.
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. This is the H.264 Level.
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
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
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