How to translate HandbrakeCLI options to FFmpeg options

ffmpeghandbrakevideo-encoding

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?

Best Answer

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. 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 is not supported in FFmpeg anymore; instead, use the built-in FFmpeg encoder with -c:a aac -b:a 128K instead.

  • -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 – it's called “Rate Factor” in Handbrake. While CRF usually gives you the best quality encodes, the VBV mode gives you a file that is better suited for streaming online or for certain devices with limited capabilities.

    To set VBV, use the -maxrate 5200K and -bufsize 5200K options in ffmpeg.

    To set CRF, use -crf 23 in ffmpeg.

  • 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

    These are options that you can set in ffmpeg additionally with -x264-params me=umh:merange=64:subme=7:8x8dct:cabac=1 – like any other x264 encoder setting that is not directly mapped to ffmpeg options.

    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 -x264-params me=umh:merange=64:subme=7:8x8dct:cabac=1 -c:a aac -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 aac -b: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 aac -b:a 128K -ar 44100 d:\output.mp4