FFmpeg and x264 (H.264/MPEG-4) cheat sheet

Discussion in 'Multimedia Encoding' started by MultiBam, Feb 10, 2014.

  1. MultiBam

    MultiBam Administrator Staff Member

    Some useful commands

    -strict -2 (must be included when using experimental codecs like aac)
    -threads 0 (autodetect number of cores)
    -vcodec libx264 (video codec)
    -r 25 (default framerate)
    -crf 28 (quality of output video, the lower the number the higher quality, crf 0 = lossless)
    -crf 20 -maxrate 800k -minrate 400k (crf with max and min bitrate)
    -me_method hex
    -pix_fmt yuv420p (safe colors)
    -preset veryslow -qp 0 (lossless best compression, presets ultrafast ,superfast, veryfast, faster, fast, medium, slow, slower, veryslow)
    -acodec copy (stream copy audio) (ffmpeg -i input.mp4 -movflags faststart -vcodec copy -acodec copy output.mp4)
    -ar 44100 -ab 128k (stereo at 128 kbit/s)

    You will have to look into the audio and video encoder options for common codecs in the FFmpeg online documentation, or check the output of ffmpeg -h full for a complete list of supported options. For example, x264 lists its options under libx264 AVOptions in the full help output.

    Control quality
    Quality is controlled either by specifying a bitrate through -b:v (for video) or -b:a (for audio), or by specifying any other encoding method the codec might support.

    For x264, there are various encoding methods, with the Constant Rate Factor method being the most sophisticated. It results in variable bitrate, but overall good quality in one single pass. CRF values range from 0 to 51, but sane values are somewhere between 19 and 26, depending on your source and what quality you want. 23 is default, so you could for example choose 18 for "high quality" and 28 for "low quality", whatever that means for you.

    ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4

    x264 has other encoding methods as well, but this is out of the scope here.

    Constrain the H.264 profile
    These profiles define a feature set the encoder may use to match the capabilities of a certain decoder. In recent FFmpeg, use the following syntax to specify a profile, where profile could be baseline, mainor high:

    ffmpeg -i input.mp4 -c:v libx264 -profile:v baseline output.mp4

    For more info and when you should use which profile, see: What are the differences between H.264 Profiles?

    Choose an x264 encoding preset
    These presets affect the encoding speed. Using a slower preset gives you better compression, or quality per filesize, whereas faster presets give you worse compression. In general, you should just use the preset you can afford to wait for. Presets can be ultrafast, superfast, veryfast, faster,fast, medium (default), slow and veryslow. Here's an example:

    ffmpeg -i input.mp4 -c:v libx264 -preset slow output.mp4

    Encode lossless video
    This is possible by specifying a CRF of 0, so simply use -crf 0:

    ffmpeg -i input.mp4 -c:v libx264 -crf 0 output.mp4

    Finally, let's talk about ProRes quickly. ProRes accepts either a fixed bitrate with -b:v, or you can specify the profile, which should be a value between 0 and 3, where the bit rates are chosen according to the profile. Higher means better:

    ffmpeg -i input.mp4 -c:v prores -profile:v 0 output.mov

    The ffmbc Wiki suggests that names of profiles can be used
    Last edited by a moderator: Feb 15, 2014

Share This Page