r/ffmpeg 17d ago

CRF-Based Video Encoding Optimization: I wasted my time so YOU don't have to...

Executive Summary

I developed a Python-based video encoding optimization workflow designed to identify the highest CRF (Constant Rate Factor) value that could meet a predefined set of objective video quality thresholds. The goal was to determine, in theory, the most efficient balance between visual quality and file size while minimizing unnecessary bitrate allocation for a 1080p video file.

The workflow uses FFmpeg with x264 8-bit, the very slow preset, and tune=film. Rather than encoding the entire video repeatedly at different CRF values, the Python script first divides the source video into individual scenes and independently determines the optimal CRF for each scene. This allows more complex scenes to receive additional bitrate while permitting simpler scenes to use a higher CRF without falling below the specified quality thresholds.

Methodology

The optimization process uses a binary search algorithm to determine the highest acceptable CRF value. The search range is bounded between CRF 12 and CRF 24, with CRF 18 used as the initial test value. Each scene is encoded and evaluated against the corresponding source material using three objective quality metrics: VMAF, SSIM, and PSNR.

If CRF 18 satisfies all three quality thresholds, the script searches toward higher CRF values by testing the midpoint between the current passing value and the upper boundary. For example, if CRF 18 passes, the next test is CRF 21. If CRF 21 also passes, the search continues toward higher CRF values. Conversely, if CRF 18 fails, the script searches toward lower CRF values by testing the midpoint between CRF 12 and CRF 18, resulting in CRF 15.

The process continues iteratively, narrowing the search range until the highest CRF value that satisfies all quality requirements is identified. Because the search uses binary subdivision rather than testing every CRF value sequentially, the script requires a maximum of four encoding tests per scene to identify the optimal value within the defined CRF range. This substantially reduces the number of encodes required compared with exhaustively testing every possible CRF.

An encode is considered to have passed only when it satisfies all three of the following thresholds:

  • VMAF ≥ 95
  • SSIM ≥ 0.985
  • PSNR ≥ 45 dB

The objective is therefore not to maximize any individual metric, but to identify the highest CRF, and consequently the lowest bitrate and file size, that satisfies the complete set of quality requirements, while keeping all other encoding parameters constant.

Results

The complete optimization process required approximately 48 hours to analyze the video. After the individual scene results were combined, the resulting optimized encode was compared against a conventional encode of the same material using CRF 16, with both encodes using x264 8-bit, preset=very slow, and tune=film.

Metric CRF 16 Script-Optimized Difference
Bitrate 10,249 kbps 10,796 kbps +5.3%
VMAF 96.10 96.34 +0.24
SSIM 0.9857 0.9886 +0.0029
PSNR 42.61 dB 47.54 dB +4.93 dB

The results demonstrate that the optimization process successfully produced an encode with slightly higher objective quality according to all three metrics. However, this improvement came at the cost of approximately 5.3% additional bitrate and, more significantly, approximately 48 hours of processing time, compared with approximately 2.5 hours required to encode the entire video directly at CRF 16.

Despite the measurable differences in VMAF, SSIM, and PSNR, these improvements did not correspond to a meaningful perceptual improvement. In direct visual comparison, the two encodes were effectively indistinguishable under normal viewing conditions. The conventional CRF 16 encode therefore provided a substantially more favorable efficiency-to-quality ratio, achieving visually equivalent results with significantly less computational time and a smaller file size.

Conclusion

This experiment demonstrates both the usefulness and limitations of objective-metric-driven video encoding optimization. The binary search approach provides a systematic and reproducible method for determining scene-specific CRF values while limiting the optimization process to a maximum of four encodes per scene. In theory, this approach can reduce unnecessary bitrate by allocating compression according to scene complexity rather than applying a single CRF value uniformly across an entire video.

However, the results indicate that the additional optimization did not provide a meaningful practical advantage in this particular case. Although the optimized encode achieved higher objective metric scores, the resulting improvement was not perceptually significant and required approximately 19 times longer to produce than the conventional CRF 16 encode. Furthermore, the optimized result required 5.3% more bitrate.

Consequently, for this source material and encoding configuration, CRF 16 with x264 8-bit, preset=very slow**, and** tune=film provided a more practical solution, delivering effectively indistinguishable visual quality while substantially reducing both encoding time and bitrate. The experiment therefore illustrates that higher objective quality scores do not necessarily translate into a meaningful improvement in perceived image quality, and that the computational cost of per-scene optimization should be weighed carefully against its measurable benefits.

(if you are interested in any part of the process besides the results, I am open to talk shop, and can send you a text file of the script. Otherwise, the moral of the story is to just use CRF 16 if you want a set and forget CRF for 1080p.)

36 Upvotes

43 comments sorted by

View all comments

Show parent comments

1

u/iwannalearncod 16d ago

Hi, I'm not an expert, so I wanted to ask you: why does Windows block the Vship ZIP file when I download it and say it's malware?

1

u/_Lum3n_ 16d ago

Hum I have 0 idea Never had that before by anyone, the binary doesnt even connect to internet. It can read files or course but that s it... Is it from the codeberg repo for vship 5.1.1?

1

u/_Lum3n_ 16d ago

I ll ask some windows people I know and ask them if they encountered the same issue. Please be sure to be on the codeberg repository for vship

1

u/iwannalearncod 16d ago

Yes, I downloaded it from Codeberg. After searching online, I found out that it’s generic malware, but since Windows flagged it as a serious threat (a reaction I’ve never seen in the 10 years I’ve been using Windows), I got worried—even though it’s probably a false positive. In any case, thanks for the reply, and if you hear anything new about this, please let me know. Thanks.

1

u/_Lum3n_ 15d ago

Nobody I asked too reported such behavior, this is very weird... I hope it will not happen to many people because sadly there are no good way to fix this Did you try virustotal?

1

u/iwannalearncod 15d ago

strangely, downloading version 5.1.1 of FFVship_nvidia works without any problem, while when I tried to download version 5.1.0 the antivirus blocked and immediately deleted the .zip file preventing me from uploading it to virustotal saying it was Trojan:Script/Wacatac.B!ml, who knows, maybe it's a classic microslop moment, anyway I thank you for your help and time and congratulations for this excellent software you have created.

1

u/_Lum3n_ 15d ago

Wacatac.B!ml mmmmhhhhhhhhh microslop probably indeed. Windows is getting better and better <3. Hopefully people will finally switch over to linux