FFmpeg x264 encoding guide
Encoding h.264 in FFmpeg
UPDATE: Presets!!!
Preset files are available from the FFmpeg source code in the ffpresets subdirectory. You can either copy *.ffpreset from that directory into ~/.ffmpeg/ or you can point to a preset file directly on your command line.
Rate control methods available:
- constant quantisation parameter
- constant rate factor
- one pass average bit rate
- two pass variable bit rate
CQP mode is mostly deprecated by CRF as CRF maintains more constant quality which was usually the aim of someone using CQP mode. If you care more about quality than bit rate then I would recommend using one pass CRF to save yourself a bit of time. Evaluate various rate factors between about 25 and 15 (a lower value is higher quality and higher bit rate) until you find a quality level/approximate bit rate with which you’re happy and then you can use that value forever more.
One pass average bit rate is good if you need to aim for an approximate bit rate but don’t have time to run two passes, but if you have the time then two passes are recommended as the bits will be better distributed to maintain constant quality.
For all rate control methods you will probably want libx264 to select the number of threads to use to maximise its performance on your CPU(s) and you should also specify the number of b-frames to use:
-bf 16 -threads 0
Note: Most software decoders can handle up to 16 consecutive b-frames. However, if you encounter playback issues, check to see if your playback device imposes limitations or try a more sensible number like 3.
To use one pass CQP:
-cqp <int>
To use one pass CRF:
-crf <float>
To use one pass ABR or two pass VBR:
-b BIT_RATE -bt BIT_RATE
So if you wanted to encode using two-pass VBR, the command line would be something like:
ffmpeg -i INPUT -an -pass 1 -vcodec libx264 -vpre fastfirstpass -b BIT_RATE -bt BIT_RATE -bf 16 -threads 0 OUTPUT.mp4
ffmpeg -i INPUT -acodec libfaac -ab 128k -pass 2 -vcodec libx264 -vpre hq -b BIT_RATE -bt BIT_RATE -bf 16 -threads 0 OUTPUT.mp4
Some general notes for x264 options in FFmpeg that are not self-explanatory:
* ME methods {for -me_method name}: dia, hex, umh, tesa
* Entropy coders {name (n for -coder n)}: CAVLC (0), CABAC (1)
* i_qfactor = i_frame_qp/p_frame_qp = 1/ipratio (note that this is not the same as ‘ipratio’ from the x264 CLI)
* Bit rate tolerance: bt = BIT_RATE*ratetol (’ratetol’ is the related parameter from the x264 CLI)
Note: If you think of any others, please contact me.
Also, see my x264 <-> FFmpeg option mapping and an externally maintained list of mappings with descriptions of what the options mean.
Those of you looking for the iPod/PSP information, I felt it was a little out of place here and have subsequently moved them to their own pages. | iPod guide | PSP guide |
THE OLD MANUAL WAY OF DOING THINGS FOLLOWS. IT’S RECOMMENDED TO USE THE PRESETS AS DISCUSSED ABOVE BECAUSE IT WILL MAKE YOUR LIFE MUCH EASIER!!!
The defaults for x264 in FFmpeg are currently significantly detrimental to the quality of the encoded video. This is due to the internal behaviour of FFmpeg with respect to codec defaults. Depending on the rate control method used, a number of different switches need applying to bring the settings into line with the x264 command line interface defaults. These are generally OK settings as they have been suggested by the author(s). The necessary switches for FFmpeg to ‘correct’ the defaults until a proper solution is completed are detailed below, however these may be altered as required.
Apply these settings for all rate control methods:
-coder 1 -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me_method hex -subq 6 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 1 -threads 0
To use one pass CQP:
-cqp <int>
To use one pass CRF:
-crf <float>
To use one pass ABR or two pass VBR:
-b BIT_RATE -bt BIT_RATE -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4
So if you wanted to encode using two-pass VBR, the command line would be something like:
ffmpeg -i INPUT -an -pass 1 -vcodec libx264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me_method dia -subq 1 -trellis 0 -refs 1 -bf 16 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -threads 0 OUTPUT.mp4
ffmpeg -i INPUT -acodec libfaac -ab 128k -pass 2 -vcodec libx264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti8x8+parti4x4+partp8x8+partp4x4+partb8x8 -flags2 +dct8x8+wpred+bpyramid+mixed_refs -me_method umh -subq 7 -trellis 1 -refs 6 -bf 16 -directpred 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -threads 0 OUTPUT.mp4
Note: I’ve altered the settings from the x264 CLI defaults according to Dark Shikari’s recommendations for x264. The quality settings can be considerably reduced for the first pass of two as it has little impact on the output of the second (or nth) pass.