x264 reprint

 x264 is an H.264/MPEG4 AVC encoder, and this guide will teach newbies how to create high-quality H.264 videos.

    For ordinary users, there are usually two code rate control modes: crf (Constant Rate Factor) and Two pass ABR. Rate control is a method of determining how many bits are allocated to each video frame, which determines the allocation of file size and quality.

    If you need help compiling and installing libx264, check out the ffmpeg and x264 compilation guide:

    http://ffmpeg.org/trac/ffmpeg/wiki/CompilationGuide

crf(Constant Rate Factor):

    This method can achieve a specific video quality for the entire file when the size of the output file is not important. This encoding mode provides maximum compression efficiency in single-pass encoding mode, and each frame can obtain the number of bits it requires according to the required video quality. The downside is that you can't get a video file of a specific size, or control the output bitrate to a specific size.

    1 Select a CRF value

    The range of quantization ratio is 0~51, where 0 is lossless mode, 23 is the default value, and 51 is probably the worst. The smaller the number, the better the image quality. Subjectively, 18~28 is a reasonable range. 18 is often considered to be visually lossless, and its output video quality is exactly the same or almost the same as the input video. But from a technical point of view, it is still lossy compression.  

    If the Crf value is increased by 6, the output code rate is approximately reduced by half; if the Crf value is reduced by 6, the output code rate is doubled. Usually, the maximum Crf value is selected while ensuring acceptable video quality. If the output video quality is good, then try a larger value. If it looks bad, then try a smaller value.

    Note: The quantization ratio mentioned in this article only applies to 8-bitx264 (the quantization ratio of 10-bit x264 is 0~63). You can use the x264 --help command to view the output bit depth in the Output bit depth option. In various Among versions, 8bit is the most common.

   2 Select a preset

     A preset is a set of parameters that provide a trade-off between encoding speed and compression ratio. A preset with a slightly slower encoding speed will provide higher compression efficiency (compression efficiency is measured in file size). This means that if you want to get a specific file size or use constant bitrate encoding mode, you can use a slower preset to get better quality. Likewise, for constant quality encoding mode, you can easily save bitrate by choosing a slower preset.

     If you're patient, the usual advice is to use the slowest preset. All current presets, in descending order of encoding speed, are: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo

 The default is medium, please ignore placebo as it is useless (see Q&A below). You can use --preset to view the preset list, or you can use x264 --fullhelp to view the parameter configuration used by the preset.

    You can change the parameter settings based on the uniqueness of the input content by using --tune. The current tunes include: film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolantency. If your suppressed content is animation, you can use animation, or if you want to retain the texture, use grain. If you are not sure which option to use or your input does not match all tunes, you can omit the --tune option. You can use --tune to view the tune list, or you can use x264 --fullhelp to view the parameter configuration used by tune.

    Another optional parameter is --profile, which limits your output to a specific H.264 profile. This option can be ignored unless your playback device only supports a certain profile. All current profiles include: baseline, main.high, high10, high422, high444. Note that using the --profile option is incompatible with lossless encoding.

    As shown below, as a shortcut, you can enumerate all possible internal presets and tunes for ffmpeg by not declaring the content of preset and tune.

   ffmpeg -i input -c:v libx264 -preset -tune dummy.mp4

  3 Use your default

   Once you select a preset, apply it to your remaining unencoded videos to ensure they have the same video quality.   

CRF example:

Next we'll encode a video using x264, using a preset that's slightly slower than the normal preset, which will give us slightly better video quality than the default settings.

ffmpeg -i input -c:v libx264 -preset slow -crf 22-c:a copy output.mkv

Note that in this example, the audio stream from the input file is simply copied to the output and is not re-encoded.

Two pass mode:

This method works well if your target is a file of a certain size and the video quality between frames is not important. This is best explained with an example. Your video has a duration of 10 minutes (600 seconds) and requires an output of 50MB, because bitrate = file size/duration,

   50MB*8192 (MB to kilobits)/600 seconds = 683 kbps (global bit rate)

   ,683kbps-128kbps(audio bitrate)=555kbps(video bitrate),

Example of encoding on both sides:

  ffmpeg -y -i input -c:v libx264 -preset medium -b:v 555k -pass 1 -an -f mp4 /dev/null &&

  ffmpeg -i input -c:v libx264 -preset medium -b:v 555k -pass 2 -c:a libfdkaac -b:a 128k mp4 output.mp4

Note that Windows users should use NUL instead of /dev/null

When using CRF, choose the slowest preset you can tolerate.

  At the same time, it is recommended that you take a look at "Producing High-Quality MPEG4 DVD Movie Video Clips", which is an MPEG4 encoder encoding guide. It will give you a deep understanding of the effective use of both sides of the encoding mode when you are faced with limited storage space. How important every bit is.

 Lossless H.264

You can use -qp 0 or -crf 0 to encode a lossless output. For lossless compression we recommend using -qp over -crf. Because crf in 8 bitx264 and 10 bitx264 uses different values ​​for lossless mode. Ultrafast and veryslow are two very useful presets for this, as fast encoding speed and excellent compression ratio are usually two very important factors. Most non-ffmpeg players cannot play lossless mode, so if compatibility issues are a concern, you may not be able to use lossless mode.

 Example of lossless compression (fast encoding)

 ffmpeg -i input -c:v libx264 -preset ultrafast -qp 0 output.mkv 

 Example of lossless compression (high compression ratio)

 ffmpeg -i input -c:v libx264 -preset veryslow -qp 0 output.mkv 

Override default presets

You can use -x264opts to override the default or use libx264's private options (you can view libx264 options completely with ffmpeg -h). We don't recommend doing this unless you know what you're doing. All presets are created by x264 developers, and trying to fine-tune parameters to improve output quality is usually a waste of time.

example:

ffmpeg -i input -c:v libx264 -preset slow -crf 22 -x264opts   keyint=123:min-keyint=20 -c:a copy output.mkv 

Additional information :

ABR(Average Bit Rate)

ffmpeg -i input -c:v libx264 -b:v 1000k ....

It provides some kind of "running average" target, with the ultimate goal being that the final file size matches this "global average" number (so basically if the encoder encounters a lot of black frames with very little bitrate overhead, it will end up with a low encoding at the required bitrate, but non-black frames in the next few seconds will be encoded in a high-quality manner that reverts the bitrate to the mean) Using both sides encoding mode makes this method more efficient, you can use "max bit" rate ” is used together to prevent fluctuations in bit rate.

CBR(Constant Bit Rate)

In fact there is no such mode as CBR, but you can "simulate" a constant bitrate setting by supplementing the ABR parameters, such as:

ffmpeg -i input -c:v libx264 -b:v 4000k -minrate 4000k -maxrate 4000k -bufsize 1835k out.m2v

In this example, -bufsize is a "rate control buffer", so it will force an average you require within each useful 1835k video data (here 4000k), so basically we will think that the receive The end/end player will buffer that much data, so there is no problem with fluctuations within this data.

Of course, if there are only black or blank frames, it will cost less bitrate than the required bitrate (but it will improve the quality level as much as possible, up to crf).

CRF mode for maximum bitrate

You can use crf mode with maximum bitrate by specifying the -crf and -maxrate settings, such as:

ffmpeg -i input -c:v libx264 -crf 20 -maxrate 400k  -bufsize 1835k  

This will effectively lock the crf value at 20, but if the output bitrate exceeds 400kbps, in which case the encoder will reduce the quality below crf 20.

low latency

x264 adds a -tune zerolatency option.

compatibility:


If you want your video to be maximally compatible with the target playback device (such as older versions of iOS or all Android devices), then you can do this:

-profile:v baseline 

This will turn off many advanced features, but it will provide good compatibility. You may not need these settings, because once you use them, there will be a slight increase in bitrate compared to higher encoding levels at the same video quality.

For a list of profiles and descriptions about them, you can run x264 --fullhelp

Keep in mind that Apple Quick Time only supports the YUV 420 color space for x264-encoded videos, and does not support any encoding level higher than the main profile. This leaves only two compatible options for quick time: baseline and main. Other encoding levels are not supported by qucik time, although they can be played back on other playback devices.

Use the -ss and -t options to encode a paragraph instead of the entire video, so you can quickly understand the video encoding output.

-ss offset time from starting value, this value can be in seconds or in HH:MM:SS format

-t output delay, this value can be in seconds or in HH:MM:SS format



Questions and Answers:

1 Can two-pass encoding mode provide better quality than CRF mode?

  No, but it allows more precise control of the target file size.

2 Why is placebo a waste of time?

 Compared with veryslow, it exchanges about 1% video quality improvement at the cost of extremely high encoding time. This is a principle of diminishing returns. veryslow has an improvement of 3% compared with slower; slower has an improvement of 5% compared with slow. %; slow has increased by 5%~10% compared with medium.

3 Why does my lossless output look lossless?

This is due to the conversion of rgb->yuv, if you convert to yuv444, it is still lossless.

4. Can the graphics card accelerate x264 encoding?

No, x264 is not used (at least not yet), there are some private encoders that use the GPU to speed up encoding, but that doesn't mean they are well optimized. It is also possible that it is not as good as x264, perhaps even slower. In general, ffmpeg does not support GPU so far.

 Translation note: x264 has begun to support opencl-based graphics card acceleration in the 2013 version for frame type determination.

5 Compress videos for Quick time player

You need to use -pix_fmt yuv420p to make your output support QT player. This is because Apple's Quick time only supports the YUV420 color space for H.264 video editing. Otherwise, ffmpeg will output a video format incompatible with Quick time or a video that is not based on ffmpeg based on your video source.


Original address: http://blog.csdn.net/vblittleboy/article/details/8982857

Guess you like

Origin blog.csdn.net/gaopeng1111/article/details/85598624