日本語

How CRF Affects Quality and File Size in FFmpeg libx264

​​FFmpeg​command line,H.264,video

When you convert video to H.264 (libx264) with FFmpeg, the -crf value decides the balance between quality and file size. A higher value makes the file smaller and lowers the quality. In my test environment, I encoded three kinds of video at CRF 13 to 43 and measured file size and quality values.

This is useful if you want to shrink a video file or compress a video for the web and want a rough idea of how much a given CRF will reduce the size. To summarize the results first: even at the same CRF 23, file size was about 3.78 MB for a simple graphic pattern and 19.79 MB for a video with grainy noise, roughly five times the difference depending on the content.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (the Ubuntu package), x264 core 164, and a virtual machine with 4 CPU cores. I used only the libx264 encoder and the default medium preset. The videos were test videos generated by FFmpeg; I did not use footage from a camera. I did not test the x265 (H.265), VP9, or AV1 encoders, evaluation with VMAF, or how the results look on actual devices.

The Videos Compared and How I Measured

I created three source videos with FFmpeg filters. Each is 10 seconds, 1280×720, 30 fps (300 frames). For use as a reference, I saved them in a lossless format (FFV1). When I compared the simple graphic video with a version generated from the filter again, PSNR was inf (identical).

bash
# Simple graphic (testsrc2 test pattern)
ffmpeg -f lavfi -i "testsrc2=size=1280x720:rate=30,format=yuv420p" -t 10 -c:v ffv1 src-simple.mkv
# Fine pattern (zooming Mandelbrot set)
ffmpeg -f lavfi -i "mandelbrot=size=1280x720:rate=30,format=yuv420p" -t 10 -c:v ffv1 src-detail.mkv
# Fine pattern with per-frame changing grain noise added
ffmpeg -f lavfi -i "mandelbrot=size=1280x720:rate=30,noise=alls=10:allf=t,format=yuv420p" -t 10 -c:v ffv1 src-grain.mkv

In the lossless state, the file sizes were 15.9 MB for the simple graphic, 92.3 MB for the fine pattern, and 261.2 MB for the video with grain noise.

I encoded each one at seven CRF values: 13, 18, 23, 28, 33, 38, and 43. I did not include audio.

bash
ffmpeg -i src-detail.mkv -c:v libx264 -preset medium -crf 23 -an out-detail-crf23.mp4

I measured quality by comparing each frame of the encoded video with the source video, using FFmpeg's psnr and ssim filters. A higher PSNR and an SSIM closer to 1 both mean the video is closer to the source. When I compared the source video with itself in my test environment, PSNR was inf (infinity) and SSIM was 1.000000.

bash
ffmpeg -i out-detail-crf23.mp4 -i src-detail.mkv \
  -lavfi "[0:v]setpts=N/(30*TB),split[a1][a2];[1:v]setpts=N/(30*TB),split[b1][b2];[a1][b1]psnr;[a2][b2]ssim" \
  -f null -

setpts=N/(30*TB) resets the timestamps of both videos based on the frame number. The reason this was necessary, and what went wrong without it, is explained later in the article.

File Size and Quality by CRF

File sizes are given in MB, where 1 MB = 1,000,000 bytes. The bitrate is the video bitrate. PSNR is the average of Y, U, and V combined, and SSIM is the overall (All) value.

Results for the simple graphic (testsrc2):

CRF Size Bitrate PSNR SSIM
13 8.00MB 6,394kbps 54.70dB 0.9995
18 5.85MB 4,678kbps 49.72dB 0.9984
23 3.78MB 3,018kbps 44.54dB 0.9956
28 1.82MB 1,451kbps 39.76dB 0.9874
33 0.98MB 779kbps 36.93dB 0.9795
38 0.72MB 569kbps 35.18dB 0.9711
43 0.50MB 394kbps 33.18dB 0.9597

Results for the fine pattern (Mandelbrot set):

CRF Size Bitrate PSNR SSIM
13 18.01MB 14,406kbps 48.16dB 0.9961
18 11.87MB 9,490kbps 44.18dB 0.9940
23 7.08MB 5,663kbps 39.87dB 0.9893
28 3.37MB 2,689kbps 35.56dB 0.9791
33 1.19MB 949kbps 32.27dB 0.9611
38 0.52MB 415kbps 30.53dB 0.9372
43 0.40MB 317kbps 29.34dB 0.9142

Results for the fine pattern with grain noise added:

CRF Size Bitrate PSNR SSIM
13 128.97MB 103,173kbps 43.92dB 0.9839
18 81.07MB 64,850kbps 38.51dB 0.9449
23 19.79MB 15,826kbps 33.18dB 0.7660
28 4.48MB 3,577kbps 31.59dB 0.7158
33 1.56MB 1,248kbps 29.73dB 0.6938
38 0.66MB 525kbps 28.37dB 0.6651
43 0.51MB 403kbps 27.24dB 0.6382

The file sizes are graphed below. The vertical axis is logarithmic.

Line graph with CRF values 13 to 43 on the horizontal axis and file size in MB (logarithmic scale) on the vertical axis. The video with grain noise starts at about 129 MB at CRF 13, the fine pattern at about 18 MB, and the simple graphic at about 8 MB. At CRF 43, all three converge to about 0.4 to 0.5 MB. A vertical dotted line marks the default CRF 23.
File size by CRF. The three videos were encoded with the same settings

When CRF goes up by 5, the size reduction varied widely, from about 0.2 to 0.8 times, depending on the video and the CRF range. From CRF 23 to 28, the simple graphic went from 3.78 MB to 1.82 MB, and the fine pattern from 7.08 MB to 3.37 MB, both about 0.48 times. For the video with grain noise, the size dropped to about a quarter from CRF 18 to 23 and again from CRF 23 to 28. At higher CRF values the reduction gets smaller, and at CRF 43 all three videos converged to about 0.4 to 0.5 MB.

Encoding time also generally decreased as CRF increased. For the video with grain noise, it took 23.3 seconds at CRF 13 and 5.5 seconds at CRF 43 (single runs on this virtual machine).

Even at the Same CRF, Size Depends on the Content

CRF is not a way to specify a target file size. The x264 help describes --crf as "Quality-based VBR" (variable bitrate based on quality), and the FFmpeg encoder documentation also describes the libx264 crf option as the value that sets the quality for the constant-quality mode.

The results show the same thing. Comparing at the default CRF 23, file sizes were:

  • Simple graphic: 3.78 MB
  • Fine pattern: 7.08 MB
  • Video with grain noise: 19.79 MB

So if you need to fit a fixed size, such as an upload limit, you cannot determine the size from the CRF value alone.

How the Fine Pattern Breaks Down at Higher CRF

I cropped the same area from a frame at 5 seconds in the fine-pattern video, enlarged it 2 times, and placed the crops in a column. From top to bottom: CRF 18, 28, and 38.

Three vertically stacked crops of the same area of the fine-pattern video at CRF 18, 28, and 38. At CRF 18 and 28, the fine dots along the edges of the pattern remain clearly visible, while at CRF 38 the edge dots are smeared and blurred, and the flat areas blend into each other with soft boundaries
Part of the fine-pattern video enlarged 2 times. From top to bottom: CRF 18, 28, and 38

At CRF 18 and 28, the fine dots on the edges of the pattern are both preserved, and the difference is slight even side by side. At CRF 38 the edge dots are smeared and the whole image looks blurred. The numbers show the same: SSIM dropped from 0.9791 at CRF 28 to 0.9372 at CRF 38. The size went from 3.37 MB to 0.52 MB, about one-sixth.

Grain Noise Disappears First as CRF Goes Up

The video with grain noise behaved differently from the other two. From CRF 18 to 23, the size dropped from 81.07 MB to 19.79 MB, a quarter, and SSIM fell sharply from 0.9449 to 0.7660.

Cropping a dark area and enlarging it 4 times shows what is happening. From top to bottom: the source, CRF 18, and CRF 28.

Dark red and green area of the grain-noise video enlarged 4 times, shown in a vertical stack: the source video, CRF 18, and CRF 28. The source and CRF 18 show fine grain across the whole area, while at CRF 28 the grain is gone and the colors look like smooth, flat fills
Dark area of the grain-noise video enlarged 4 times. From top to bottom: the source, CRF 18, and CRF 28

At CRF 18, the grain remains across the area, as in the source. At CRF 28, the grain is almost gone and the areas look smooth. The boundary between the red and green areas, however, is still there.

PSNR and SSIM measure how closely the video matches the source, noise included. So when the noise disappears, the values drop sharply even though the shape of the pattern remains. The same pattern without added noise was 11.87 MB at CRF 18, while the version with noise was 81.07 MB. At CRF 18, most of the file size was spent on keeping the noise. Whether you want to keep the noise changes which CRF you should choose.

The Default CRF and the Upper Limit

When I encoded with libx264 without -crf and checked the settings string that x264 writes into the video, it contained rc=crf and crf=23.0. The default is CRF 23.

text
x264 - core 164
rc=crf mbtree=1 crf=23.0

According to the x264 help, the upper limit for --crf is 51. In my test environment, -crf 52 did not produce an error, and the written setting was crf=51.0. -crf 60 also resulted in crf=51.0, so values above 51 were treated as 51.

When Measuring Quality, Align the Timestamps of the Two Videos

The psnr filter documentation states that the two inputs must have the same number of frames and are compared frame by frame. Even with the same frame count, the results were not valid when the timestamp intervals differed.

In my test environment, the time base of the source MKV was 1/1000 second, and the encoded MP4 had a time base of 1/15360 second. The MKV's timestamps were rounded to 1/1000 second, so the first frames were at 0.033 s, 0.067 s, and so on (line 1 is the MKV time base, line 2 is the MP4 time base, and line 3 shows the timestamps of the first five MKV frames).

text
time_base=1/1000
time_base=1/15360
src-simple.mkv: 0.000000 0.033000 0.067000 0.100000 0.133000

When I compared the simple graphic at CRF 13 without setpts, the average PSNR was 28.36 dB.

text
PSNR y:30.013236 u:26.305001 v:26.143030 average:28.356538 min:22.129657 max:58.679206

When I reset the timestamps from the frame number with setpts=N/(30*TB) and compared again, it was 54.70 dB.

text
PSNR y:54.690004 u:54.651996 v:54.797966 average:54.701428 min:53.636060 max:58.679206

CRF 13 is the highest-quality setting in the table, so a value as low as 28 dB was not plausible. When I wrote the per-frame values without setpts to stats_file, the Y PSNR for frames 1 and 2 was in the 55 dB range, but frame 3 was 26.65 dB. Of the 300 frames, 100 were below 40 dB, which showed that I had been comparing mismatched frames.

Before comparing the values for each CRF, check whether the result for the highest-quality setting is unnaturally low. That makes it possible to catch a mistake in the measurement method.

​Posted by 86