FFmpeg x264 encoding guide

來源:互聯網
上載者:User
FFmpeg x264 encoding guide

Encoding h.264 in FFmpeg

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 hex -subq 5 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 1

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.

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 -rc_eq 'blurCplx^(1-qComp)' -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 epzs -subq 1 -trellis 0 -refs 1 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 OUTPUT.mp4

ffmpeg -i INPUT -acodec libfaac -ab 128k -pass 2 -vcodec libx264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 5 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 OUTPUT.mp4

Note: I’ve altered the settings from the x264 CLI defaults. 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.

Some general notes for x264 options in FFmpeg that are not self-explanatory:

* ME methods {for -me name}: epzs (also known as dia but called epzs in FFmpeg), hex, umh, full (also known as esa)
* 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)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.