Preset files are available from the FFmpeg source code in the ffpresets subdirectory. Using the 0.5 release or current svn trunk of FFmpeg, running make install will install the preset files to ${prefix}/share/ffmpeg and just specifying -vcodec libx264 -vpre <preset> is sufficient for ffmpeg to find the preset.
Otherwise, you can either copy *.ffpreset from the FFmpeg source into ~/.ffmpeg/ or you can point to a preset file directly in the argument of -fpre in your command line.
Basic FFmpeg Usage
To construct an ffmpeg command line, you need to understand a few fairly simple things. The basic command line format is:
ffmpeg [input options] -i [input file] [output options] [output file]
- input options – these are usually mandatory information that is missing about the source file (for example, in the case of a sequence of image files this could be the frame rate), or the seek position corresponding to the desired start point of the stream
- output options – audio and video filtering (alteration of the sample rate, number of channels, frame dimensions, frame rate), audio and video codecs and their corresponding options, container format and related options
Normally the minimum one should specify in an ffmpeg command line would be something like:
ffmpeg -i INFILE -acodec ACODEC -ab 96k -vcodec VCODEC -b 500k OUTFILE
…replacing the capitalised values as appropriate. When using x264, we need to add some extra bits as the defaults are bad.
x264 – Rate Control Choices
Firstly, you need to decide on a rate control method. This controls how bits are allocated within the stream. At a rudimentary level, you want the quality to remain constant but this implies that more complex frames use more bits and less complex frames use less bits.
Rate control methods available:
- constant quantisation parameter – not recommended anymore unless you know you want it
- constant rate factor – good for one pass when the priority is quality and file size/bit rate is not really a concern
- one pass average bit rate – good for streaming purposes or targeting a bit rate when two-pass is unfeasible
- two pass variable bit rate – good for targeting a bit rate when you have the time to spend on two passes (though the first pass can be quite fast) and are writing to a file
Or in prose, 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. If you have the time then two passes are recommended as the bits will be better distributed to maintain constant quality.
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
x264 – FFmpeg Preset Choices
Secondly, you need to choose an appropriate preset from those available.
Presets available (for lossy compression):
- quality related:
- ultrafast
- superfast
- veryfast
- faster
- fast
- medium – x264 CLI default
- slow
- slower
- veryslow
- placebo
- first pass:
- <preset from above>_firstpass – for example medium_firstpass
- profile constraints:
- baseline
- main
Note: Profile constraint presets need to be specified with a second -vpre option after the quality/first pass preset, e.g. -vpre slower -vpre main
x264 – Recommended General Options
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):
-threads 0
Two-Pass Example
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 slow_firstpass -b BIT_RATE -bt BIT_RATE -threads 0 OUTPUT.mp4
Note: We don’t encode the audio in the first pass because we will not be using the data that was output.
ffmpeg -i INPUT -acodec libfaac -ab 128k -pass 2 -vcodec libx264 -vpre slow -b BIT_RATE -bt BIT_RATE -threads 0 OUTPUT.mp4
Single-Pass Constant Rate Factor (CRF) Example
Or for a single pass CRF encode:
ffmpeg -i INFILE -acodec libfaac -ab 96k -vcodec libx264 -vpre slow -crf 22 -threads 0 OUTPUT.mp4
Sign up for our daily email newsletter:
{ 2 trackbacks }