<blockquote>
  <p>What's the correct way to generate the highest quality possible file
  inside a bitrate constraint using ffmpeg with x264?</p>
</blockquote>

<p>There is no "correct" way. This is still an evolving area of research, and about half the articles you have read on the subject are more marketing that they are science. The "correct" way may also be different for every video. Netflix for example just encodes the video a couple dozen ways, and keeps the one it thinks is best and throws the rest away.</p>

<p>The closest thing to a "best" single answer, would be 2 pass placebo capped CRF.</p>

CC BY-SA 4.0
2019-07-23T21:39:23.777Z
Ok but still, which one is better, single pass CRF capped or the other ?
Freedo
CC BY-SA 4.0
2019-07-23T21:41:01.477Z
“Best” is not caped CRF next I guess would be 2 pass caped CRF
SlimSCSI
CC BY-SA 4.0
2019-07-23T21:52:07.043Z
Best quality inside a X bitrate
Freedo
CC BY-SA 4.0
2019-07-23T18:53:48.943Z
2019-07-23T18:53:48.943Z
SlimSCSI
<p>I'm not sure you gathered the spirit of that article as intended. The author's point was to describe &quot;capping&quot; or constraining the CRF setting by creating a bandwidth ceiling per frame. The purpose of which is to constrain/limit the overall file size to a pre-defined maximum. This forcibly limits the freedom of the CRF algorithm with respect to its upper limit of bits-per-frame.</p>
<p>Now, take that concept and think about applying ffmpeg's 2-pass model to it. Running 2-pass on capped CRF (aka Constrained CRF) is redundant and a waste of time. First, how would you constitute the 2nd pass? If you repeat the first pass logic, you'll likely attain a smaller target file, but you'll also likely dilute the overall image quality to some extent. The target file size will still have the same maximum possible size it had before. The 2nd pass will almost undoubtedly take longer to process than the first pass, because of how the 2-pass process works in ffmpeg, and the fact you'll be compressing already compressed images again.</p>
<p>Overall, it's just plain a bad idea. It's a time waster and you'd likely end up with worse quality versus the first pass result. Best case scenario, they will look the same. If file size is your top priority, you need to move away from H.264 and choose a more recent codec library optimized for compression (such as h.265/HEVC or VP9, possibly AV1). Constrained CRF is not a poor idea at all, but attempting to run 2-pass encoding with it is.</p>

CC BY-SA 4.0
2021-08-15T21:20:54.280Z
2021-08-15T21:20:54.280Z
MrPotatoHead
<p>Based on <a href="https://slhck.info/video/2017/02/24/crf-guide.html" rel="nofollow noreferrer">CRF Guide (Constant Rate Factor in x264, x265 and libvpx)</a>, quoting:</p>

<blockquote>
  <p>Streaming nowadays is done a little more cleverly. YouTube or Netflix
  are using 2-pass or even 3-pass algorithms, where in the latter, a CRF
  encode for a given source determines the best bitrate at which to
  2-pass encode your stream. They can make sure that enough bitrate is
  reserved for complex scenes while not exceeding your bandwidth.</p>
</blockquote>

<p>also the article <a href="https://streaminglearningcenter.com/blogs/saving-encoding-streaming-deploy-capped-crf.html" rel="nofollow noreferrer">Saving on Encoding and Streaming: Deploy Capped CRF</a> also claims that "Capped CRF" is the highest quality while also making sure bitrate does not go above X but they propose a single pass with:</p>

<pre><code>ffmpeg -i input_file -crf 23 -maxrate 6750k -bufsize 6750k output_file
</code></pre>

<p>What's the correct way to generate the highest quality possible file inside a bitrate constraint using ffmpeg with x264?</p>

CC BY-SA 4.0
2019-07-23T12:48:36.910Z
I'd hazard a guess that unless you were dealing with the volume of content that YT and NF are, the small decreases in file size probably aren't going to give you a good return on the time spent doing the extra passes. But I don't know your use case.
stib
CC BY-SA 4.0
2019-07-23T21:27:58.603Z
@stib Time is not relevant, as long it stays under 24 hours for a 3 hour input. Saving on the bitrate while delivering very high video quality is the goal
Freedo
<ffmpeg><h.264>
How to 2-pass h264 with CRF and maximum bitrate with ffmpeg?
Freedo
# How to 2-pass h264 with CRF and maximum bitrate with ffmpeg?

(Asked by: username_0 on 2019-07-23)

Based on CRF Guide (Constant Rate Factor in x264, x265 and libvpx), quoting:

Streaming nowadays is done a little more cleverly. YouTube or Netflix
  are using 2-pass or even 3-pass algorithms, where in the latter, a CRF
  encode for a given source determines the best bitrate at which to
  2-pass encode your stream. They can make sure that enough bitrate is
  reserved for complex scenes while not exceeding your bandwidth.

also the article Saving on Encoding and Streaming: Deploy Capped CRF also claims that "Capped CRF" is the highest quality while also making sure bitrate does not go above X but they propose a single pass with:
ffmpeg -i input_file -crf 23 -maxrate 6750k -bufsize 6750k output_file

What's the correct way to generate the highest quality possible file inside a bitrate constraint using ffmpeg with x264?

**Tags:** ffmpeg, h.264

> **Comments:**
> **username_1**: I'd hazard a guess that unless you were dealing with the volume of content that YT and NF are, the small decreases in file size probably aren't going to give you a good return on the time spent doing the extra passes. But I don't know your use case.  
> **username_0**: @username_1 Time is not relevant, as long it stays under 24 hours for a 3 hour input. Saving on the bitrate while delivering very high video quality is the goal  

---

## 2 answers

### Answer by username_2 on 2019-07-23. Score: 3

> What's the correct way to generate the highest quality possible file
> inside a bitrate constraint using ffmpeg with x264?

There is no "correct" way. This is still an evolving area of research, and about half the articles you have read on the subject are more marketing that they are science. The "correct" way may also be different for every video. Netflix for example just encodes the video a couple dozen ways, and keeps the one it thinks is best and throws the rest away.

The closest thing to a "best" single answer, would be 2 pass placebo capped CRF.

> **Comments:**
> **username_0**: Ok but still, which one is better, single pass CRF capped or the other ?  
> **username_2**: “Best” is not caped CRF next I guess would be 2 pass caped CRF  
> **username_0**: Best quality inside a X bitrate  

---

### Answer by username_3 on 2021-08-15. Score: 1

I'm not sure you gathered the spirit of that article as intended. The author's point was to describe "capping" or constraining the CRF setting by creating a bandwidth ceiling per frame. The purpose of which is to constrain/limit the overall file size to a pre-defined maximum. This forcibly limits the freedom of the CRF algorithm with respect to its upper limit of bits-per-frame.

Now, take that concept and think about applying ffmpeg's 2-pass model to it. Running 2-pass on capped CRF (aka Constrained CRF) is redundant and a waste of time. First, how would you constitute the 2nd pass? If you repeat the first pass logic, you'll likely attain a smaller target file, but you'll also likely dilute the overall image quality to some extent. The target file size will still have the same maximum possible size it had before. The 2nd pass will almost undoubtedly take longer to process than the first pass, because of how the 2-pass process works in ffmpeg, and the fact you'll be compressing already compressed images again.

Overall, it's just plain a bad idea. It's a time waster and you'd likely end up with worse quality versus the first pass result. Best case scenario, they will look the same. If file size is your top priority, you need to move away from H.264 and choose a more recent codec library optimized for compression (such as h.265/HEVC or VP9, possibly AV1). Constrained CRF is not a poor idea at all, but attempting to run 2-pass encoding with it is.


---

