Trao đổi với tôi

http://www.buidao.com

10/31/13

[Encoding] x264 Encoder Settings - Understanding the Command Line

You may never need to actually use the command line directly, but programs that use the CLI behind the scenes may show you the command iine that will be used for encoding. You may even be able to add your own options if they're not presented graphically in the form of a checkbox or list. Here's a (oversimplified) command example:
x264 --bframes 2 --ref 3 --bitrate 2000 --output "OutputFile.mkv" "SourceVideo.avs" 720x480 


Reading the Command


You can think of this command as a sentence, which can be read like this:
x264 Encode with the x264 encoder --bframes 2 using no more than 2 consecutive B frames at any place in the video stream --ref 3 with prediction for P and B frames based on 3 reference frames (or 3 in each direction for B frames) --bitrate 2000 at an average bitrate of 2000kbps, -o Outputfile.mkv storing the output video in the Matroska file Outputfile.mkv SourceVideo.avs from the video file SourceVideo.avs 720x480 which has a resolution of 720x480.

Notice that the source file options (name and resolution) aren't preceded by -. Instead they're the last 2 options on the command line. Also note the quotes around filenames.



x264 Profiles


x264 profiles, perhaps more accurately called MeGUI profiles, are supported by a number of encoding applications that use the x264 CLI encoder. The advantage to using profiles is that they can be used to apply the same default settings to repeated encodes and its relatively little effort to keep using them if you switch to another application, such as from AutoMKV to StaxRip or MeGUI. Since they're simple text (XML) files you don't even need special software on your computer to view or edit them. Either use a text editor like Notepad to open them. Rather than starting building your own profiles from scratch, it's best to start with one that's similar to the settings you intend to use and edit it to make whatever changes are necessary.

Profile Formatting


In case you're not familiar with XML syntax, here's an example of what the top of an x264 profile might look like. Profile options don't necessarily correspond directly to x264 command line options, but rather are considerations for an encoder front end like MeGUI to use when writing the command for you. For example, EncodingMode of 4 means multipass ABR (average bitrate) encoding will be used. That will requre that two (or more) commands will have to be generated and executed with the --pass option on each one. You can create your own profiles from scratch, but it's recommended to start with one that's already been written and modify it. You can find profiles for everything from iPods to game consoles to Blu-ray
  
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  HQ-Slow


4
1000
250
16
10
51


AVC Profiles and Levels


Like other MPEG standards, AVC is defined as a system of profiles and levels. These are not the same as x264 profiles, which are basically just lists of encoder settings. However, x264 does have a --levels option, which should be chosen based on the hardware or software which will playback Instead, a GUI that uses the x264 CLI may have a list of profiles and levels, allowing it to limit you to settings consistent with your selection. If you're using a program that supports MeGUI-style profiles you're probably best off at least starting with one that's been written with your playback device in mind.

AVC profiles are more or less designed around particular types of devices, such as mobile phones, HDTV receivers, or high definition DVDs. From Baseline to High1, each successive profile adds features like B frames, interlaced encoding, and higher compression levels. Levels simply define resolution and bitrate limitations. Typically when you're encoding for playback on a particular piece of hardware you'll want to match its restrictions in your encoded video. The profile will be determined by which encoder options you use.





x264 Options


Most of the options on this page are equally useful for individual command lines or profiles. A few, like the input filename, apply only to individual encoding jobs, and are therefore not appropriate for inclusion in a profile. Each option will have a simple explanation, which will include an example of use on the command line, the type of data (true/false, numbers, text) you must supply, and default value (when appropriate) if the option isn't specified. Below that will be the same option (with the default value specified) as a line of XML for use in a profile:


--option <bool>

true

Description

Read the description for each option to get find out what it does and when to use it.

true

The data type noted for each option tells you what sort of input you need to provide. The types are bool (Boolean) for true/false, int (Integer) for numbers with no fractional (decimal) portion, and text for text characters.


Source and Output Files


In addition to the actual encoding settings, x264 also requires general information on the source and output files. The name of the source file should be the last thing specified on the command line (followed by resolution when necessary). Input and output file information isn't appropriate for an XML profile as it varies from one encode to the next.


--output OutputFilename

Output File

Specify the name of the output file. Make sure to include the file extension of .264 for a raw bitstream, .mkv for output to the Matroska container, or .mp4 to put the encoded video in the (MPEG-4 standard) MP4 container2.




SourceFilename.avs;

Source Video

The standard input for x264 is an AviSynth script. Your script output must be in the YV12 colorspace, which is the default for AviSynth. Make sure to specify the video's resolution (see next option).




horizontal_x_vertical

Source Resolution

x264 needs to know the resolution of your source video. make sure not to use spaces. Use 1280x720 rather than 1280 x 720.




Profile and Level


Although you may be using a GUI that allows you to explicitly set a profile, on the command line it's simply a matter of using the correct options. For example, if you want to have video compatible with AVC's Baseline Profile you won't want to use any B frames. The AVC profile can be set in a x264 profile. This will allow an encoder frontend that supports this setting to allow only the features appropriate for the selected AVC profile.


--level <float>

no restrictions

AVC Profile and Level

AVC levels are designated with decimal numbers, such as 3 and 3.1. This is the notation that x264's CLI uses, but not how it's noted in a x264 profile. Instead you should specify a number between 0 and 11 using the table below.

The Profile setting uses 0 for Baseline, 1 for Main, and 2 for High Profile. This doesn't correspond to a particular CLI option, but rather tells an encoder GUI which options should be available. Implementation of this feature is up to the GUI developer.
int

int

AVC Level11.11.21.322.12.233.13.244.14.255.1
x264 Profile Setting ()01234567891011121314



Bitrate and Quality


There are a number of strategies used to encode, including two different 1 pass methods optimized for quality rather than file size, variable bitrate encoding optimized to reach a predetermined size in as little as 1 pass or as many as you want to use, and even a (rarely used) constant bitrate option. Your choice of strategy will depend largely on whether you need your encoded video to meet a particular size requirement, such as fitting on a single recordable DVD, or if you want a specific quality at whatever size results3.


--qp <int>

Single Pass Constant Quantizer

Use this option to specify a quantizer to use for every block. Each block of pixels in an AVC frame is "estimated", and the amount of detail stored is (partly) dependent on the value of the quantizer. However using the same quantizer doesn't automatically mean equal quality between blocks. Some blocks can be encoded with a higher quantizer without losing detail, while others have so much detail that they require more bits (a lower quantizer) to avoid visible loss4.

Constant quantizer encoding isn't recommended unless you need lossless output.
1
int




--crf <float>

Single Pass Constant Rate Factor

Rather than relying on blindly assigning a low quantizer to blocks to ensure a minimum quality, CRF encoding uses built in metrics intended to simulate human perception so it can encode blocks with even quality. This will almost always result in smaller output files than Constant Quantizer encoding at similar quality. Lower values will provide higher quality and a higher bitrate. Commonly used values range from 18 - 24, depending on the content of your video.
9
float




--bitrate <int>

Average Bitrate

When encoding with a specific output size in mind it's generally preferable to use ABR (Average Bitrate) encoding. Quality will be optimized if you encode with either 2 or 3 passes, but ABR encoding can be done in a single pass as well. Use this option to set the average bitrate for encoding.

Single pass and multipass ABR encoding use different EncodingMode numbers in x264 profiles. For multiple passes set EncodingMode to 4 and for single pass ABR encoding use a value of 0.
0
or
4

plus
int




--pass <int>

Pass Number

Multiple passes for the same encode must be run as separate encoding jobs. For only single pass (ABR) encoding, don't specify a value for --pass. For 2 pass encoding start with --pass 1 for the first and --pass 2 for the second. For more than 2 passes use --pass 1 for the first pass, --pass 3 for every other pass except the last, and --pass 2 for the final pass5.

Since the same profile must work for multiple passes, the pass number isn't specified. However, the maximum number of passes for a GUI to allow can be set.

3



--stats ".stats"

Stats File Extension

For multipass encoding the Stats file is used to hold the results of each successive pass. This is how the encoder improves quality during each encode (or at least the first 2-3). This sets the file extension that will be used, which is generally set to .stats, although you can use any extension you wish.

.stats



Interlaced Video


By default x264 assumes progressive video. If you're encoding interlaced content you'll need to either deinterlace before encoding or tell x264 to encode interlaced. Although interlaced encoding requires a higher bitrate for comparable quality, deinterlacing itself tends to reduce quality.

--interlaced

Interlaced Encoding

This option requires no additional information. When it's present interlaced encoding will be performed. Keep in mind that encoding progressive video interlaced may reduce quality slightly, but encoding interlaced video as progressive will always produce worse results.
All x264 profiles should have a specific (true or false) setting for interlaced encoding.
bool




B Frames


One of the most important features of MPEG encoding is the B frame. B frames are significantly smaller than I frames, and usually also much smaller than P frames. This is because, unlike P frames, B frames are produced by comparing both earlier and later frames. While more B frames will tend to increase compression, using too many will reduce quality. Since B frames also require more CPU power to play than I or P frames, they're not supported in lowest AVC profile (baseline), which is intended for portable and handheld players with little decoding power.


--bframes <int>

0

Use B Frames

The number you use here will become the maximum number of consecutive B frames (with no I or P frames in between) which will be encoded. If you leave Adaptive B frames on x264 can choose to use fewer B frames than the number you specify, but will never use more. If Adaptive B frames is turned off you shouldn't set this any higher than 1 or 2.
int




--no-b-adapt

Adaptive B Frames

By default x264 decides when to add B frames and how many to add. Although you'll never have more consecutive B frames than you have set with the --bframes option, you may have fewer, particularly if you've configured it at a higher value than 3. If you turn of adaptive B frames you need to be careful not to use more than 1 or 2.
Adaptive B frames should be explicitly set to either true or false in every profile. Notice that it's not quite like the command line option where true means 'don't use adaptive B frames. Instead, for a profile, true means use Adaptive B frames and false means don'e use them.
bool




--b-pyramid

B Frame Pyramid

If you're using more than 1 B frame you'll also want to use B Frame Pyramid. Since B frames are predicted using frames both before and after, this may include one or more additional B frames. Unlike MPEG-2, AVC allows these B frames to be used as reference frames to predict other B frames. Set it to true to enable this feature.
bool




Increasing Quality With More Reference Frames


P and B frames are basically a record of changes between frames, rather than complete images. The differences are predicted by analyzing one or more preceding frames, and in the case of B frames also following frames. AVC allows you to use more than one frame in each direction as a reference. This is particularly helpful for encoding animation, where having two or more identical frames is common, making prediction difficult.

--ref <int>

1

Number of Reference Frames

Using between 3 and 5 reference frames is recommended for most encoding. Using more reference frames will slow down encoding (and may require better hardware for playback), and shouldn't be done unless absolutely necessary. If you find that your source video has passages with large sequences of duplicate frames you may need to increase this.
int




Deblocking and Lossless Compression


Among AVC's advanced features are the ability to cover up encoding artifacts inherent in macroblock based encoding, as well as additional (lossless) compression to help reduce file size.

--nf

Turn Off Deblocking

Deblocking is used to hide the blocking artifacts that can occur at the boundaries of the 16x16 macroblocks each frame is divided into for quantization. The video isn't actually altered, but rather analyzed by the encoder to create a sort of roadmap for the encoder to use for its built-in deblocking filter. Although deblocking requires additional CPU power, and may not be useful for playback on older computers, deblocking can be calculated by the encoder and ignored by the decoder, making it available without re-encoding when you upgrade your computer. Deblocking should always be turned on, but if you want to turn it off use --nf or false in the profile.
true




--no-cabac

Use CAVLC Instead of CABAC

After lossy compression is finished, AVC video has an additional lossless compression stage which can be performed with two different algorithms6. If you're encoding for some kind of portable or mobile device or many older computers you may wan to turn off the default CABAC compression to use the less complex CAVLC.
bool




Macroblock Partitioning


While Deblocking helps to hide boundaries between macroblocks, partitioning can actually reduce the amount of blocking done by the encoder. Partitions are 4x4 or 8x8 divisions within a single 16x16 macroblock. Depending on the size of partitions used in a particular encode, a single could be encoded as a single 16x16 block, four 8x8 partitions, sixteen 4x4 partitions, or any combination of 4x4 and 8x8 partitions.

The use of partitions allows a lower quantizer to be used for a very small part of a frame, resulting in smaller files without sacrificing quality. It also helps reduce blocking, as partition boundaries aren't as regular as macroblock edges. Using partitions also slows down both encoding and decoding. If you're using an older computer for either you may want to avoid 4x4 partitions, or at least limit them to I frames.

--partitions [i8x8,i4x4,p8x8,p4x4,b8x8,all]

Macroblock Partitioning

You can either set --partitions to all in order to allow 4x4 and 8x8 macroblock partitions for I and P frames, as well as 8x8 partitions for B frames. Use the notation in the command line example to add one or more partition sizes. Notice that the notation uses the frame type (i, p, or b) folowed by the desired blocksize. Multiple selections are separated by commas (but not spaces).
bool

bool
bool
bool
bool



GOP Size


Besides deciding how to use B frames, you may want to specify the total GOP size. Although the default of a minimum of 10 frames and maximum of 250 may play in Blu-ray players, if you want to author strictly to Blu-ray specs its best to set the maximum to the number of frames in a second of video, rounding partial frames up. For example, for 25fps video you'd use a maximum GOP size of 25 frames. For 23.97fps film you'd use 24, and for 29.97fps video you'd use 30. Depending on your Blu-ray player you may be able to use larger GOPs than the standard allows, but this isn't guaranteed.

--keyint <int>

250

Maximum GOP Size

For most encoding the default value of 250 frames per GOP should work fine. For Blu-ray set it to the framerate of the video, rounded up. That means 23.976 for most film content, 25fps for video at the PAL framerate of 25fps, and 30 for video at the NTSC framerate of 29.97fps.
int




--min-keyint <int>

25

Minimum GOP Size

x264's default GOP size of 25 is suitable for most encoding, but you can change is if you'd like. Obviously if you set a maximum GOP size of 24 you'll probably want to lower it.
int




Return To Your Guide



If you came here from another guide, select the appropriate link below to return.

Return to Convert MPEG-2 Captures to AVC with AutoMKV



x264 Encoder SettingsYou are viewing Page 2 of 2 -- Go to page 1 , 2



1More About AVC Profiles

You can learn more about the specifics of various AVC profiles and levels in our Glossary entry for MPEG-4 Part 10. MPEG-4 Part 10 is one of two designations for Advanced Video Coding. The other is the ITU name of H.264.
(Return)

2MKV vs. MP4

Although MP4 is a suitable container for AVC video, the use of private streams such as AC-3 (Dolby Digital) audio isn't widely implemented. By using the MKV (Matroska) container you have more options for audio compression beyond AAC (the official MPEG-4 audio standard).
(Return)

3Other Size-Based Strategies

Predicting File Size Using Constant QualityMany encoding tools have hybrid encoding strategies which try to predict file size at a given quality setting. This can be used to ensure that the quality across your encoded video is similar, while still meeting your size requirements.
(Return)

4Quantization in Layman's Terms

Think of quantization as rounding. It's actually much more complex than that, but the basic purpose of quantization is to round large values to smaller ones to save space. Imagine you have the numbers 1,000,000 and 1,329,613. If you round both numbers to the nearest hundred thousand you'll get values of 1,000,000 and 1,300,000. This is like using a high quantizer. Notice that the first number hasn't been changed. This is similar to how a high quantizer effects blocks with very little detail. The second number, which contained more detail (non-zero digits) looks significantly different than the original number of 1,329,613. In order to keep the extra detail in this number you'd need to round to a lower value, such as the nearest hundred for 1,329,600. Similarly, blocks with a lot of detail benefit from a lower quantizer, while blocks with less detail might not.
(Return)

5Why Pass 2 After Pass 3?

If you're encoding to an average bitrate in more than one pass, x264 needs to know what pass its working on. Valid values are 1, 2, or 3. On pass 1 no video will be generated, only a .stats file to be used by the second pass. A value of 2 indicates the last pass of a multipass encode and therefore doesn't update the Stats file. Pass 3 encodes video using the existing Stats file like the second pass, but also updates it for use in the next pass.
(Return)

6The Difference Between CABAC and CAVLC

The standard algorithm, CABAC (Context Adaptive Binary Arithmetic Coding) is more efficient than the alternative CAVLC (Context Adaptive Variable Length Coding), meaning file size for the same video quality will be lower. However, CABAC also requires more processing power for the decoder, and may not be suitable for portable devices or older computers. In addition to requiring a faster computer for decoding, CABAC is also more CPU intensive for encoding. Using CAVLC doesn't reduce video quality, but will result in larger files to achieve that quality.
(Return)

Version History

http://www.afterdawn.com/guides/archive/x264_options_explained_page_2.cfm