6

I know that it's very hard to find out best encoding parameters for a whole video file. Wouldn't it be easier to just go through the video and find out the best parameters for different parts (or scenes).

Does it make sense or probably there is software that already allows that?

1
  • 1
    No need to use two-passes. See the FFmpeg and x264 Encoding Guide; specifically the section on using a constant rate factor (CRF) for examples and more information. All you need to do is pick your crf value which controls your quality, and your encoding preset which controls your encoding speed/compression efficiency.
    – llogan
    Mar 15, 2013 at 21:34

3 Answers 3

5

What you are describing is effectively what 2-pass VBR does for you. It makes a first path that calculates the level of change for each particular time in the video and then uses this information to make the best possible use of the available storage space.

It is, however, entirely possible to do the process manually by doing multiple encodings with different parameters. In fact, many formats even support having multiple encodings with different parameters within the same file. This is how you are able to change the resolution on the fly when streaming videos on YouTube and how Netflix automatically adjusts the quality on the fly as you watch to try to avoid buffering issues.

Any professional level encoder should allow you to specify multiple formats for the file in queue and many will even let you specify multiple outputs for one instance of the file. (For example, the render queue in After Effects allows for this.)

In general, I would recommend running a 2-pass VBR and then adjusting the keyframe and total bandwidth accordingly. If you are using a format that works based on the motion in the scene, then generally keyframes need to be more often if the video gets lots of motion artifacts (where it diverges from where it actually should be too much in high motion). If the video is simply too low quality and "pixelly", then the bandwidth generally needs to be raised or the keyframes lowered.

2
  • Do you know of software that allows scene/regional parameters for MPEG-2 or AVC encoding? I have read that Hollywood DVDs are mastered by a 'compressionist' who uses tools which allow such fine-grained tweaking.
    – Gyan
    Mar 22, 2013 at 9:45
  • @Mulvya - I have used such a package once when I was using a hardware encoder and Sonic DVD Producer for MPEG-2. The software was however very complex, unstable and unwieldy and was, in short, the largest waste of $5 grand I have ever spent. There may be cheaper options that allow that level of control now, but unless you are thoroughly knowledgeable about the algorithms and looking through for particular artifact problems, you will probably have better luck with 2 pass VBR.
    – AJ Henderson
    Mar 22, 2013 at 13:12
2

Yes, some encoders allow changing parameters (like target quality or bitrate, or psychovisual tuning options (x264's aq and psy-rd options).

x264, the stand-alone command-line frontend for the library, has a --zones parameter to give more bitrate to some parts of the video. So you could for example reduce the quality for the credits.

The actual x264 library supports changing many parameters on the fly, but I don't know if ffmpeg has a way to specify option changes for ranges of the video. You could get nearly the same result by segmenting your video, and encoding the different segments with different options, then combining them.

Generally nobody actually does this, because it's a HUGE amount of work, and it's really hard to know which settings are going to help just by looking at a scene. Usually if you want more quality, you just spend more CPU time on the encode, and that improves everything across the board. (--preset veryslow). I could imagine getting some benefit out of tweaking AQ settings, though, maybe to address banding in one scene, but changing it for other scenes where that wasn't an issue.

Still, bits are essentially never precious enough for it to be worth paying a human to save them, instead of just electricity and CPU time. This is more of a hobby thing / thought experiment to see what different codec settings do, IMO. Maybe when we need to stream video to / from a mars colony, bits will be that precious...

11
  • 1
    👍 Thanks! My use case is a screen recording of a click dummy. Quite often while I explain on the audio track, the image is still for 1-5 secs, great reduction of frames/filesize with -vf mpdecimate -vsync vfr. Some more dynamic here/then: mouse cursor, UI widgets open/close. All looks great with -crf 28. Just in the intro I shortly slide in/out a portrait and say hello. This lookus ugly at CRF 28. So I looked for a way to compress only that intro scene with a higher quality. No I know: Either separate intro + main video concatenated or find a way for --zones within ffmpeg.
    – porg
    Jul 3, 2023 at 10:26
  • @porg: After I wrote this answer, I tried actually using --zones. It didn't work well; the bitrate spiked right after a low-bitrate zone IIRC, wasting a lot of the savings, so I think rate-control doesn't interact well with it, at least the way I was doing it where I tried to set a bitrate limit for a short part of a video. So concatenation with compatible settings is probably good. Jul 3, 2023 at 12:01
  • Generally you deemed "human encoding optimization not worth it". I overally agree. But an optimization for at most 1 or2 scenes per video (intro or outro/credits) to receive a different quality parameter to me seems a legit optimization effort. So my 30min video "looks really well enough" at CRF 28. But that first 10secs of the intro really need the CRF 18 to look pleasant and not turn off my viewers right at the beginning, but still I want my viewers (also not blessed with broadband!) to benefit from a video otherwise really well compressed.
    – porg
    Jul 3, 2023 at 18:59
  • Also regarding buffering the UX should be ok if you have the high quality part at the beginning, then followed by average compression. It will buffer what's necessary for the high quality start. Sort of "overcompensates", may take longer than necessary for the later median bitrate. When reaching the average quality part it will most likely continue without any hickups. Whereas starting low, the buffer likely was calculated over-optimistically small, and when reaching the main higher quality part will get overwhelmed and may need to rebuffer then.
    – porg
    Jul 3, 2023 at 19:09
  • 1
    Was successful eventually! See my full writeup with detailed instructions at: video.stackexchange.com/questions/36646/…
    – porg
    Jul 5, 2023 at 14:01
0

Variable bit rate and adaptive codecs are common in the audio world, and work very well. The most obvious example is VBR MP3s. Encoding video is a fundamentally similar problem, so I would be surprised if adaptive codecs don't exist there too.

1
  • That would be good to know they are exist. However, I wonder if there are semi-automatic applications for professionals who wish to adjust quality of the output to the best.
    – Kentzo
    Mar 14, 2013 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.