<p>You problem is easy: <code>-b:v</code> does not set the maximum bitrate, but the target bitrate. Set target bitrate first <code>-b:v 1M</code>, then maximum bitrate <code>-maxrate 2M</code>.</p>

CC BY-SA 4.0
2022-04-02T11:26:25.227Z
2022-04-02T15:45:01.770Z
2022-04-02T15:45:01.770Z
muhe138
<p>Short version: Why did my total file size increase when I switched from constant quality to constrained quality at the same crf? Shouldn't this just set a maximum bitrate and thus only either do nothing or <em>lower</em> file size, depending on whether the maximum is hit?</p>
<p>Long version: I'm trying to maximise (subjective) overall quality in a library of video files given a fixed total file size limit for the entire library.</p>
<p>I started off with constant quality, and adjusting the crf until I hit my limit.
But I found a subset of scenes (usually simple but with a single moving object) looked particularly low quality/smeary. After watching the bitrate meter during encoding I concluded too much of the budget was spent in complex high-action scenes (where subjectively I think the extra quality is somewhat wasted), leaving not enough for &quot;simpler&quot; scenes, so, short of a more direct method to apply an adjustable bias towards simpler scenes (I don't want to just switch straight to constant quantization or constant bitrate or anything, I'm trying to find an optimal middle ground), I decided to try constrained quality, as described here: <a href="https://trac.ffmpeg.org/wiki/Encode/VP9" rel="nofollow noreferrer">https://trac.ffmpeg.org/wiki/Encode/VP9</a>, where I intended to slowly lower the maximum until I couldn't justify lower quality complex scenes, then use the freed space to boost the crf to the benefit of the simple scenes.</p>
<p>This seems to have <em>sort of</em> worked, but the behaviour I'm seeing is surprising, and I don't understand it.</p>
<p>I <em>expected</em> that setting a maximum bitrate with <code>-b:v 1M</code> (I'd seen quite a few complex scenes were exceeding that bitrate, pushing to around 1.5M) would reduce the total size of the entire library of video files (at the cost of reducing quality in complex scenes).
Instead, I found the total file size had <em>increased</em> after I applied this, meaning constrained quality mode does not behave the way I expected.</p>
<p>Theories:</p>
<ol>
<li>My original expectation was that constrained quality meant &quot;match constant quality but any scenes that exceed the maximum bitrate will use VBR/target bitrate rules to control their bitrate instead&quot;.</li>
<li>Now I wonder if perhaps another possibility is that the constraint only applies globally for the entire video, and once the constant quality goal tries to cross the bitrate constraint (momentarily?, or on average over a video?), the entire video stops trying to match constant quality, and instead follows the VBR rules, which forces the &quot;simple&quot; scenes to suddenly use vastly more bitrate than they needed in constant quality mode. That seems insane and pointless to me, constrained quality makes no claim about boosting the quality beyond that requested, so why waste bits giving higher quality that wasn't asked for?</li>
</ol>
<p>Some videos use significantly less in constrained quality mode, as expected, and some videos use significantly more, which I didn't expect at all. On the other hand, without me having to change the crf at all, the &quot;simple&quot; scenes I originally wanted to boost the quality for <em>have</em> actually increased in quality, exactly as I originally wanted, I just don't know why it behaves this way!</p>
<p>I'm seeing a subset of longer videos reduce in size by 7% or so (276MB -&gt; 257MB total for 17 files), but I'm seeing a subset of shorter videos <em>increase</em> in size by 40% or so (95MB -&gt; 133MB total for 220 files).</p>
<p>Commands used look something like</p>
<p>Constant Quality:</p>
<p><code>ffmpeg -y -i in_file.mov -c:v libvpx-vp9 -crf 32 -b:v 0  -pass 1 -passlogfile in_file.webm.log -an -f null /dev/null &amp;&amp; ffmpeg -i in_file.webm -c:v libvpx-vp9 -crf 32 -b:v 0  -pass 2 -passlogfile in_file.webm.log -an out_file.webm</code></p>
<p>Constrained Quality:</p>
<p><code>ffmpeg -y -i in_file.mov -c:v libvpx-vp9 -crf 32 -b:v 1M -pass 1 -passlogfile in_file.webm.log -an -f null /dev/null &amp;&amp; ffmpeg -i in_file.webm -c:v libvpx-vp9 -crf 32 -b:v 1M -pass 2 -passlogfile in_file.webm.log -an out_file.webm</code></p>
<p>(so identical beyond the -b:v value)</p>

<ffmpeg><video-quality><bitrate><vp9>
Why does setting a maximum bitrate in Constrained Quality mode sometimes increase file size?
Finn
# Why does setting a maximum bitrate in Constrained Quality mode sometimes increase file size?

(Asked by: username_0 on 2022-03-09)

Short version: Why did my total file size increase when I switched from constant quality to constrained quality at the same crf? Shouldn't this just set a maximum bitrate and thus only either do nothing or *lower* file size, depending on whether the maximum is hit?

Long version: I'm trying to maximise (subjective) overall quality in a library of video files given a fixed total file size limit for the entire library.

I started off with constant quality, and adjusting the crf until I hit my limit.
But I found a subset of scenes (usually simple but with a single moving object) looked particularly low quality/smeary. After watching the bitrate meter during encoding I concluded too much of the budget was spent in complex high-action scenes (where subjectively I think the extra quality is somewhat wasted), leaving not enough for "simpler" scenes, so, short of a more direct method to apply an adjustable bias towards simpler scenes (I don't want to just switch straight to constant quantization or constant bitrate or anything, I'm trying to find an optimal middle ground), I decided to try constrained quality, as described here: <https://trac.ffmpeg.org/wiki/Encode/VP9>, where I intended to slowly lower the maximum until I couldn't justify lower quality complex scenes, then use the freed space to boost the crf to the benefit of the simple scenes.

This seems to have *sort of* worked, but the behaviour I'm seeing is surprising, and I don't understand it.

I *expected* that setting a maximum bitrate with `-b:v 1M` (I'd seen quite a few complex scenes were exceeding that bitrate, pushing to around 1.5M) would reduce the total size of the entire library of video files (at the cost of reducing quality in complex scenes).
Instead, I found the total file size had *increased* after I applied this, meaning constrained quality mode does not behave the way I expected.

Theories:

1. My original expectation was that constrained quality meant "match constant quality but any scenes that exceed the maximum bitrate will use VBR/target bitrate rules to control their bitrate instead".
2. Now I wonder if perhaps another possibility is that the constraint only applies globally for the entire video, and once the constant quality goal tries to cross the bitrate constraint (momentarily?, or on average over a video?), the entire video stops trying to match constant quality, and instead follows the VBR rules, which forces the "simple" scenes to suddenly use vastly more bitrate than they needed in constant quality mode. That seems insane and pointless to me, constrained quality makes no claim about boosting the quality beyond that requested, so why waste bits giving higher quality that wasn't asked for?

Some videos use significantly less in constrained quality mode, as expected, and some videos use significantly more, which I didn't expect at all. On the other hand, without me having to change the crf at all, the "simple" scenes I originally wanted to boost the quality for *have* actually increased in quality, exactly as I originally wanted, I just don't know why it behaves this way!

I'm seeing a subset of longer videos reduce in size by 7% or so (276MB -> 257MB total for 17 files), but I'm seeing a subset of shorter videos *increase* in size by 40% or so (95MB -> 133MB total for 220 files).

Commands used look something like

Constant Quality:

`ffmpeg -y -i in_file.mov -c:v libvpx-vp9 -crf 32 -b:v 0 -pass 1 -passlogfile in_file.webm.log -an -f null /dev/null && ffmpeg -i in_file.webm -c:v libvpx-vp9 -crf 32 -b:v 0 -pass 2 -passlogfile in_file.webm.log -an out_file.webm`

Constrained Quality:

`ffmpeg -y -i in_file.mov -c:v libvpx-vp9 -crf 32 -b:v 1M -pass 1 -passlogfile in_file.webm.log -an -f null /dev/null && ffmpeg -i in_file.webm -c:v libvpx-vp9 -crf 32 -b:v 1M -pass 2 -passlogfile in_file.webm.log -an out_file.webm`

(so identical beyond the -b:v value)

**Tags:** ffmpeg, video-quality, bitrate, vp9


---

## 1 answers

### Answer by username_1 on 2022-04-02. Score: 1

You problem is easy: `-b:v` does not set the maximum bitrate, but the target bitrate. Set target bitrate first `-b:v 1M`, then maximum bitrate `-maxrate 2M`.


---

