How to encode (x264) several videos to a target capacity with the same quality

For questions and discussion that is NOT (I repeat NOT) specific to a certain Operating System.
mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

How to encode (x264) several videos to a target capacity with the same quality

Postby mederi » 03 Jul 2020 17:40

Perhaps there is somebody who understands 2-pass encoding. It is possible to encode one video to a target size. Is it possible to achieve a target capacity (DVD) with several video files (series episodes) encoded individually but with the same quality? If all videos have the same duration (or not), but they contain different contents (scenes), the sizes must be different.
{1.mkv, 2.mkv, ...} 10 GB => 4.5 GB?
I could run 1st-pass only to generate stats+mbtree files for all sources. Could I use one the same value of target bitrate parameter for all videos just for this 1st-pass purpose?
Then is it possbile to calculate individual target bitrates from all merged stats files? Or is it possible to calculate one average CRF factor I could use for all individual sources?
One stats contents example:

Code: Select all

#options: 720x300 fps=24000/1001 timebase=1001/24000 bitdepth=8 cabac=1 ref=1 deblock=1:0:0 analyse=0x1:0 me=dia subme=2 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=23 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=399 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00 in:0 out:0 type:I dur:2 cpbdur:2 q:34.99 aq:16.00 tex:193 mv:391 misc:96 imb:855 pmb:0 smb:0 d:- ref:; in:4 out:1 type:P dur:2 cpbdur:2 q:34.89 aq:15.00 tex:0 mv:0 misc:144 imb:0 pmb:0 smb:855 d:- ref:0 ; in:2 out:2 type:B dur:2 cpbdur:2 q:34.89 aq:19.00 tex:0 mv:0 misc:128 imb:0 pmb:0 smb:855 d:- ref:0 ; in:1 out:3 type:b dur:2 cpbdur:2 q:34.89 aq:20.00 tex:0 mv:0 misc:120 imb:0 pmb:0 smb:855 d:- ref:0 ; in:3 out:4 type:b dur:2 cpbdur:2 q:34.89 aq:20.00 tex:0 mv:0 misc:120 imb:0 pmb:0 smb:855 d:- ref:0 ; in:8 out:5 type:P dur:2 cpbdur:2 q:26.89 aq:7.00 tex:0 mv:0 misc:160 imb:0 pmb:0 smb:855 d:- ref:0 ; in:6 out:6 type:B dur:2 cpbdur:2 q:30.89 aq:15.00 tex:0 mv:0 misc:144 imb:0 pmb:0 smb:855 d:- ref:0 ; in:5 out:7 type:b dur:2 cpbdur:2 q:32.89 aq:18.00 tex:0 mv:0 misc:128 imb:0 pmb:0 smb:855 d:- ref:0 ; in:7 out:8 type:b dur:2 cpbdur:2 q:28.89 aq:14.00 tex:0 mv:0 misc:128 imb:0 pmb:0 smb:855 d:- ref:0 ; in:12 out:9 type:P dur:2 cpbdur:2 q:18.89 aq:0.00 tex:0 mv:0 misc:168 imb:0 pmb:0 smb:855 d:- ref:0 ; in:10 out:10 type:B dur:2 cpbdur:2 q:22.89 aq:7.00 tex:0 mv:0 misc:152 imb:0 pmb:0 smb:855 d:- ref:0 ; in:9 out:11 type:b dur:2 cpbdur:2 q:24.89 aq:10.00 tex:0 mv:0 misc:128 imb:0 pmb:0 smb:855 d:- ref:0 ; in:11 out:12 type:b dur:2 cpbdur:2 q:20.89 aq:6.00 tex:0 mv:0 misc:136 imb:0 pmb:0 smb:855 d:- ref:0 ; in:16 out:13 type:P dur:2 cpbdur:2 q:11.66 aq:0.00 tex:0 mv:0 misc:168 imb:0 pmb:0 smb:855 d:- ref:0 ; in:14 out:14 type:B dur:2 cpbdur:2 q:15.27 aq:0.00 tex:0 mv:0 misc:152 imb:0 pmb:0 smb:855 d:- ref:0 ; in:13 out:15 type:b dur:2 cpbdur:2 q:17.08 aq:2.00 tex:0 mv:0 misc:136 imb:0 pmb:0 smb:855 d:- ref:0 ; in:15 out:16 type:b dur:2 cpbdur:2 q:13.47 aq:0.00 tex:0 mv:0 misc:136 imb:0 pmb:0 smb:855 d:- ref:0 ;

User avatar
rom1v
Developer
Developer
Posts: 36
Joined: 02 Jul 2012 13:37

Re: How to encode (x264) several videos to a target capacity with the same quality

Postby rom1v » 06 Jul 2020 15:59

Perhaps there is somebody who understands 2-pass encoding. It is possible to encode one video to a target size. Is it possible to achieve a target capacity (DVD) with several video files (series episodes) encoded individually but with the same quality? If all videos have the same duration (or not), but they contain different contents (scenes), the sizes must be different.
I suggest you encode the episodes you want on the same media as one (big) video, then split.

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

Re: How to encode (x264) several videos to a target capacity with the same quality

Postby mederi » 07 Jul 2020 18:37

Thanks for the response. I really hoped that it would be possible to do it by parts rather than in one go. The encoding would take very long time (days) and could fail at some point meaning that it would be necessary to start all over again on my low-end computer.
Is it true that 2-pass encoding results in equal quality throughout all the video, although the ABR method with its fluctuations of quality is used in the 1st-pass analysis? https://slhck.info/video/2017/03/01/rate-control.html :
Note that a two-pass and CRF encode with the same resulting bitrates should be identical in quality.
And what about the container/encapsulation (mkv, mp4) structure size? Several megabytes is not a negligible size, too. Is there a formula I could use to calculate the amount of data used for wrapping of video and audio streams?
--- EDIT ---
It is called overhead: https://en.wikipedia.org/wiki/Compariso ... s#Overhead

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

Re: How to encode (x264) several videos to a target capacity with the same quality

Postby mederi » 07 Jul 2020 19:04

Is it possible to calculate a CRF factor of the 2-pass encoded video? I could merge all videos, then start the 2-pass process and then abort it right after the end of the 2nd-pass of the first video/episode and cut/trim it properly. Then I could use the CRF method with the rest of videos/episodes.

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

Re: How to encode (x264) several videos to a target capacity with the same quality

Postby mederi » 08 Jul 2020 15:09

I could use trial and error method to find out a corresponding CRF value: a short sample from the original first video converted using CRF method to compare with a corresponding sample from the 2-pass encoded first video (merged all videos > 2-pass > abort). Does it make a sense?

Lotesdelere
Cone Master
Cone Master
Posts: 9896
Joined: 08 Sep 2006 04:39
Location: Europe

Re: How to encode (x264) several videos to a target capacity with the same quality

Postby Lotesdelere » 09 Jul 2020 10:33

As I understand it, CRF highly depends on the content of the video: more details results in higher bitrate. So I'm not sure a short sample will be enough to predict the bitrate result all along the video.

+ https://forum.doom9.org/showthread.php? ... ost1308412
CRF is not a measure of quality, it is a command to ratecontrol. Different settings and sources at the same CRF are not guaranteed to give the same visual quality.

So a given CRF value for a video might not give similar results for another video.
It's all about your goal: do you aim at quality or is file size your priority ? Considering some "low" parameters used (analyse, me, subme, trellis) you don't care much about quality, so you may want to use CRF+VBV for a better bitrate control.
Because as long as you're not using a constrained bitrate for all videos, I don't see how you can predict the final sizes from different sources.

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

Re: How to encode (x264) several videos to a target capacity with the same quality

Postby mederi » 15 Jul 2020 15:58

A guy on the mentioned forum tries to understand CRF method and compares higher resolution video with a lower resolution one. It is not my case.
> https://forum.doom9.org/showthread.php? ... ost1308419
But once you found that CRF value, you can use it for all your future encodes and get pretty similar (but not identical!) results.
https://code.videolan.org/videolan/x264 ... ontrol.txt

All I want is approximately the same constant quality in all videos that would approximately fit in a certain capacity (DVD) and I would like to encode them by parts rather than to encode them as one big video followed by a final split into separate videos (I could cut on key (I) frames only). The same settings (preset), the same resolution (SD, 720x404).
From the gathered information I can say that the CRF method means the desired constant quality and it is more effective than QP method. The constant quality requires higher bitrates for more details in various scenes and this is expected. A selected profile would be the limit. I am not an expert so I use only profiles and presets that clever developers of x264 encoder have prepared for most users (profile Main@L3 should cover most Android devices, medium preset is just right).
I just hoped some VideoLAN guys around here with more knowledge about x264 compression methods could help me to find a way how to do it or deny it as a "mission impossible". It seems I need to perform some trial and error tests on sample videos whether it is possible or not.
Is the doom9 forum a better place to discuss this my idea? But this is the only official VideoLAN forum and x264 is the VideoLAN project.

Lotesdelere
Cone Master
Cone Master
Posts: 9896
Joined: 08 Sep 2006 04:39
Location: Europe

Re: How to encode (x264) several videos to a target capacity with the same quality

Postby Lotesdelere » 15 Jul 2020 18:41

Well, either use the same CRF based settings with all of your encodings or use rom1v's suggestion. Because AFAIK the only way to predict the exact file(s) size(s) is to use constant bitrate, so yes, you will have to do some trial and error tests.

Tips: you should use 720*400 because both are divisible by 16 so x264 will perform much better. You will save room/file size and the aspect ratio distortion is acceptable IMO. Depending on your source file, first deinterlace if needed, then crop to the image only, no need to encode the black bars (if any) :wink: , then resize using spline36 or lanzcos.

And yes, the Doom9 forum is a good place for talking about such things.


Return to “General VLC media player Troubleshooting”

Who is online

Users browsing this forum: No registered users and 49 guests