Encoding x264 + Vorbis

Hi Habr.
Actually, I decided to write the first post after I read this one . In it, the author tried to state his vision, but, in my opinion, did not succeed.

So, given: BDRemux, 1080p, 25 series.
Task: Make the highest quality rip 480p (or 720p), in 10 bits.

Immediately make a reservation that we will code anime.

For work, we need:
1. x264 , for encoding the video stream.
2. ffmpeg , we will use it only for encoding sound, so in my opinion it’s easier.
3. mkvtoolnix , to build this miracle in the mkv container.

Since we have a lot of episodes, we’ll write a simple batch file that will be useful for subsequent rips.

Video


First, I will give the code, then I will explain the parameters:

"...\x264-10b.exe" "...\имя файла источника.m2ts" --input-res 1920x1080 --fps 23.976 --profile high10 --preset medium --tune animation --crf=15.5 --me=umh --ref=9 --deblock=-1,-1 --merange=24 --bframes=12 --trellis=1 --video-filter resize:width=854,height=480,method=lancoz --output "...\имя выходного файла.264"


Parameters

--input-res 1920x1080 --fps 23.976

Prompt x264 resolution and frame rate of the source. (you can peek at MediaInfo)

--profile high10

Options: baseline, main, high, high10, high422, high444.
We specify a profile. In this case, we encode in 10 bits, so high10, if in 8 - high.

--preset medium

Options: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo.
Balance of coding speed and quality. Those. higher speed - worse quality.
I mainly use medium, slow and sometimes veryslow.

--tune animation

Options: film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency.
Presets depending on the input video. For movies, one thing for anime is different.

--crf=15.5

Range: 1-50.
Regular quality mode and its level. The smaller, the better the quality. Allows each frame to use its own QP based on frame complexity.
Read more in English here .
This is the best method for single pass encoding.
Possible replacement options: --bitrate or --qp, we will not consider them.

Next, we redefine some parameters, because not all of them from the medium preset are suitable for us:

--me=umh

Options: dia, hex, umh, esa, tesa.
A method for estimating the movement of a full pixel. I recommend umh.

--ref=9

Range: 1-16.
The number of reference frames. The more, the slower it will encode. If you follow the specifications for supporting household appliances, 4 - maximum for 1080p, and 9 - maximum for 720p.
At 6 and above, you will not see much difference in quality, and the encoding speed will drop significantly.

--deblock=-1,-1.

Deblocking in format сила:порог. In short, the higher the power of deblocking, the stronger it is used, the higher the threshold, the more blocks it comes across.
Well painted here .
Since we encode BDRemux anime, it is advisable to lower the strength and threshold to reduce line blur. I use -1: -1, but there were rips with -2: -2.

--merange=24

Range: 4-64.
Determines the maximum number of attempts to find the best option when searching for a macroblock motion vector. The more, the better the quality.
It does not make much sense to bet more than 24.

--bframes=12

Range: 1-16.
Sets the maximum number of parallel B-frames. Great value can lead to a significant improvement in the efficiency of the compression ratio.

--trellis=1

Range: 0-2.
Trellis quantization to increase compression efficiency. 0 - disabled. 1 - Option "on macroblocks." 2 - "everywhere."
1 is a good compromise between loss of speed and compression efficiency. Best of 2, but in conjunction with --psy-rd, otherwise it will blur the small details.

Resize

--video-filter resize:width=854,height=480,method=lancoz

Specify the width and height, as well as the method. I am using lancoz.
Options: fastbilinear, bilinear, bicubic, experimental, point, area, bicublin, gauss, sinc, lanczos, spline.

Audio


For coding we will use ffmpeg.

"...\ffmpeg\bin\ffmpeg.exe" -i "...\имя файла источника.m2ts" -vn -c:a libvorbis -qscale:a 6 "...\имя выходного файла.ogg"


Everything is simple here:

-vn

Turn off video encoding.

-c:a libvorbis -qscale:a 6

We indicate the encoder and quality.
Range: 0-10.
The more, the better the quality.
6 is ~ 192 Kbps.

Assembly:


We encode the first series.

Open the mkvtoolnix GUI, select the resulting video and audio, set parameters (for example, it’s useful to set the aspect and frame rate for video, and track language for audio) and click copy to clipboard, we get something like:

"...\mkvtoolnix\mkvmerge.exe" -o "...\имя выходного файла.mkv"  "--default-track" "0:yes" "--forced-track" "0:no" "--aspect-ratio" "0:16/9" "--default-duration" "0:23.976fps" "-d" "0" "-A" "-S" "-T" "--no-global-tags" "--no-chapters" "(" "...\имя перекодированного видео.264" ")" "--language" "0:jpn" "--default-track" "0:yes" "--forced-track" "0:no" "-a" "0" "-D" "-S" "-T" "--no-global-tags" "--no-chapters" "(" "...\имя перекодированного аудио.ogg" ")" "--track-order" "0:0,1:0"

Full code for 25 episodes:

@echo off
for /L %%i in (1,1,9) do (
"...\ffmpeg\bin\ffmpeg.exe" -y -i "...\имя 0%%i.m2ts" -vn -map 0:2 -c:a libvorbis -qscale:a 6 "...\имя 0%%i.ogg"
"...\x264\x264-10b.exe" "...\имя 0%%i.m2ts" --input-res 1920x1080 --fps 23.976 --profile high10 --preset medium --tune animation --crf=15.5 --me=umh --ref=9 --deblock=-1,-1 --merange=24 --bframes=12 --trellis=1 --video-filter resize:width=854,height=480,method=lancoz --output "...\имя 0%%i.264"
"...\mkvtoolnix\mkvmerge.exe" -o "...\имя 0%%i.mkv"  "--default-track" "0:yes" "--forced-track" "0:no" "--aspect-ratio" "0:16/9" "--default-duration" "0:23.976fps" "-d" "0" "-A" "-S" "-T" "--no-global-tags" "--no-chapters" "(" "...\имя 0%%i.264" ")" "--language" "0:jpn" "--default-track" "0:yes" "--forced-track" "0:no" "-a" "0" "-D" "-S" "-T" "--no-global-tags" "--no-chapters" "(" "...\имя 0%%i.ogg" ")" "--track-order" "0:0,1:0"
)
for /L %%i in (10,1,25) do (
"...\ffmpeg\bin\ffmpeg.exe" -y -i "...\имя %%i.m2ts" -vn -map 0:2 -c:a libvorbis -qscale:a 6 "...\имя %%i.ogg"
"...\x264\x264-10b.exe" "...\имя %%i.m2ts" --input-res 1920x1080 --fps 23.976 --profile high10 --preset medium --tune animation --crf=15.5 --me=umh --ref=9 --deblock=-1,-1 --merange=24 --bframes=12 --trellis=1 --video-filter resize:width=854,height=480,method=lancoz --output "...\имя %%i.264"
"...\mkvtoolnix\mkvmerge.exe" -o "...\имя %%i.mkv"  "--default-track" "0:yes" "--forced-track" "0:no" "--aspect-ratio" "0:16/9" "--default-duration" "0:23.976fps" "-d" "0" "-A" "-S" "-T" "--no-global-tags" "--no-chapters" "(" "...\имя %%i.264" ")" "--language" "0:jpn" "--default-track" "0:yes" "--forced-track" "0:no" "-a" "0" "-D" "-S" "-T" "--no-global-tags" "--no-chapters" "(" "...\имя %%i.ogg" ")" "--track-order" "0:0,1:0"
)
pause

Since I have two crooked hands, the same code is written twice.

Thank you all for your attention.

UPD:

--input-res 1920x1080 --fps 23.976

You can not specify. This is only necessary if you are feeding x264 a clean raw stream.

--profile high10

Also, you can not specify, because
Bit rate is determined at the compilation stage and it will not work to encode in 8 and 10 with one binary. So it makes no sense to set up a profile in principle (unless you need 4: 2: 2 or 4: 4: 4).

Thanks tp7 .

Also, in my opinion, it makes sense to raise --subme to 9.
You can read more about all the options here in English.

Also popular now: