
x264 or how to encode video
- From the sandbox
- Tutorial

I must make a reservation right away that initially the article was not mine. I stumbled upon it, about five years ago, when the task was to do something with the recorded moments from the then beloved by many Battlefield 2 games, on the popular domestic movie maker resource. Gradually, the article was drafted and published, here and there. I do not exclude that the article originally came from behind the “hillock” and everything was translated into everything in our mighty language.
So, the x264 codec replaced such monsters of its time as DivX and XviD and successfully put both on the blades. In order to achieve a truly impressive result, we will need the following things:
1. MeGUI - with this we compress the video itself. Rather, the codec itself compresses, and this is just a GUI that combines dozens of different specialized utilities.
2.Avisynth - frame server. If suddenly someone does not know what it is, then he is the intermediary between our uncompressed video and the codec.
3. VLC media player - Everything is quite simple here. An omnivorous player that can work with streaming video. Quite popular .
4. K-Lite Codec Pack - a package of all possible codecs for all occasions. We need a Mega build.
I strongly recommend updating the K-Lite Codec Pack, at least always before video compression. This is certainly not necessary, but experience suggests that if you encounter incomprehensible errors / jambs / glitches / etc, then in 50%, or even more, updating the codecs will save you from unnecessary hemorrhoids.
By the way, MeGUI is quite fast and often updated and supplemented. The screenshots below may no longer correspond to the current version, but it's not scary. As a rule, the arrangement of elements changes, something is pushed to the right, something is moved to another bookmark. The loss is very fast, so do not be alarmed.
Go. Install Avisynth and then MeGUI . After MeGUI is updated, we go to the folder where our prototype lies, and for convenience we create a file there with the * .avs extension. Open with a notebook and write the coveted lines:
AVISource ("video.avi")
ConvertToYV12 ()
The first line will tell MeGUI which file you want to work with. The second line indicates the color system used.
There are several different ways to represent color. For example: YUV and RGB color space. In the YUV color space, there is one component that represents luminance (luminance signal) and two other components that represent color (luminance signal). While luminance is transmitted with all the details, some details in the components of the color signal can be removed by lowering the resolution of the samples (filtering or averaging), which can be done in several ways (i.e. there are many formats for saving the image in the YUV color space ) YV12 is one of such formats (here the color signal is common to each block of a 2x2 pixel), which is supported by AviSynth.
We got a script. Move on. Open MeGUI and indicate the location of the script. If the AviSynth script is in the same folder as your video, the second line will be filled automatically.

Open the codec settings by clicking on the Config button , to the right of Encoder settings . We tick off, confirming that we really need advanced settings. Then we just need to check the boxes in accordance with the screenshots.





Click on the queue button and go to sleep, drink coffee, etc. depending on preferences and PC power.
I want to make a reservation that this config is suitable for the source video 720p. For 1080p, you need to edit the config a bit:
Frame-Type Tab -> Change the Number of Reference Frames value from 9 to 4.
You can also specify how many cores can be used by the encoder:
Misc tab -> Other -> Threads section and specify how many threads to compress the video. 1 thread per 1 virtual or physical core.
What do we get as a result. I had the following video available:
Format: RGB
Codec ID: 0x00000000
Codec ID / Info: Basic Windows bitmap format. 1, 4 and 8 bpp versions are palettised. 16, 24 and 32bpp contain raw RGB samples
Duration: 3mn 42s
Bit rate: 663 Mbps
Width: 1,280 pixels
Height: 720 pixels
Display aspect ratio: 16: 9
Frame rate: 29.970 fps
Bit depth: 8 bits
Bits / (Pixel * Frame ): 24.000
Stream size: 17.2 GiB (100%)
After waiting about 15-16 minutes, I received 184 Mb of output.
If Habru is interested in similar articles on video compression, then I will continue and share my experience. If you want to correct me and point out a mistake, then I will gladly answer all criticism and comments.