日本語

Comparing Video Quality Before and After Conversion with FFmpeg PSNR and SSIM

​​FFmpeg​command line,image quality,video

The FFmpeg psnr and ssim filters give you PSNR and SSIM scores that show how close a converted video is to the original. They are useful when you change the compression ratio or encoding settings and want to compare how much quality was lost using numbers rather than impressions.

The command passes the converted and original videos as two -i inputs, and uses -f null - to measure without writing any output file. I checked how to read the numbers and how they change when the resolution or frame count differs, using actual output.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), on a virtual machine with 4 CPU cores. The original video was FFmpeg's test pattern testsrc2 (1280×720, 30 fps, 10 seconds, 300 frames), with grain noise added only to the last 5 seconds, saved with libx264 using -qp 0 (lossless). I did not test camera footage, HDR video, or VMAF (the FFmpeg build in this environment did not include libvmaf).

Basic command for comparing the converted and original videos

I compare crf23.mp4, converted from the original original.mp4 with libx264 at CRF 23, against the original.

bash
ffmpeg -i original.mp4 -c:v libx264 -crf 23 -pix_fmt yuv420p crf23.mp4

This command measures PSNR. The converted video is the first -i, and the original is the second.

bash
ffmpeg -i crf23.mp4 -i original.mp4 -lavfi "[0:v][1:v]psnr" -f null -

-lavfi specifies a filter graph (the same as -filter_complex). [0:v] is the video from the first input, and [1:v] is the video from the second. -f null - tells FFmpeg not to write the result anywhere.

After the progress output, this line is printed at the end.

text
[Parsed_psnr_0 @ 0x555bfdac6580] PSNR y:36.164485 u:37.072123 v:37.117806 average:36.453030 min:32.932440 max:46.194087

For SSIM, just change psnr to ssim.

bash
ffmpeg -i crf23.mp4 -i original.mp4 -lavfi "[0:v][1:v]ssim" -f null -
text
[Parsed_ssim_0 @ 0x5601da7db600] SSIM Y:0.872866 (8.957378) U:0.915423 (10.727466) V:0.917304 (10.825171) All:0.887365 (9.483269)

To measure both at once, join them with ; and write [0:v][1:v] for each. In my test environment this gave the same values as running them separately.

bash
ffmpeg -i crf23.mp4 -i original.mp4 -lavfi "[0:v][1:v]psnr;[0:v][1:v]ssim" -f null -

Reading the numbers

In the PSNR line, y, u, and v are the values for brightness (Y) and the two color components (U and V). The unit is dB, and a higher value means the video is closer to the original. According to the FFmpeg documentation for psnr, average is the mean squared error (MSE) across all frames, converted to PSNR. min and max are the lowest and highest per-frame PSNR values (written out per frame as psnr_avg in the next section). For the CRF 35 video, the min:30.864736 and max:36.687082 on the overall line matched the lowest and highest per-frame values, 30.86 and 36.69.

In the SSIM line, Y, U, and V are the component values, and All is the overall value. A value closer to 1 means the video is closer to the original. The number in parentheses is the same value expressed in dB. Calculating −10×log10(1−0.887365) from CRF 23's All:0.887365 gives 9.483266, which closely matches the output value of 9.483269.

When you compare identical files, PSNR is inf (infinity) and SSIM is 1.000000.

text
PSNR y:inf u:inf v:inf average:inf min:inf max:inf
SSIM Y:1.000000 (inf) U:1.000000 (inf) V:1.000000 (inf) All:1.000000 (inf)

Here is the comparison between the CRF 23 and CRF 35 videos. File sizes use 1 MB = 1,000,000 bytes. The original video is 139.51 MB.

Converted Size PSNR (average) SSIM (All)
CRF 23 15.67 MB 36.45 dB 0.8874
CRF 35 0.84 MB 32.94 dB 0.8152

The FFmpeg documentation calls the first input main and the second input reference (the baseline for comparison). However, when I swapped the first and second inputs in my test environment, both PSNR and SSIM stayed the same to six decimal places.

Writing out per-frame values to find where quality dropped

The final line shows the average for the whole video, so a short section with a large quality drop can go unnoticed. Adding stats_file writes the per-frame values to a file.

bash
ffmpeg -i crf35.mp4 -i original.mp4 \
  -lavfi "[0:v][1:v]psnr=stats_file=psnr.log;[0:v][1:v]ssim=stats_file=ssim.log" \
  -f null -

For this 300-frame video, both files had 300 lines. Each line is one frame, and n is the frame number (counting from 1).

text
n:1 mse_avg:13.98 mse_y:16.37 mse_u:9.44 mse_v:8.95 psnr_avg:36.68 psnr_y:35.99 psnr_u:38.38 psnr_v:38.61
n:1 Y:0.971304 U:0.986116 V:0.993725 All:0.977510 (16.480087)

The original video has noise starting at 5 seconds (frame 151). Frames 150 and 151, on either side of that boundary, show a clear change in the values. The top two lines are from psnr.log, and the bottom two are from ssim.log.

text
n:150 mse_avg:14.20 mse_y:15.42 mse_u:12.56 mse_v:10.94 psnr_avg:36.61 psnr_y:36.25 psnr_u:37.14 psnr_v:37.74
n:151 mse_avg:47.25 mse_y:48.28 mse_u:45.91 mse_v:44.47 psnr_avg:31.39 psnr_y:31.29 psnr_u:31.51 psnr_v:31.65
n:150 Y:0.973524 U:0.987775 V:0.993339 All:0.979201 (16.819664)
n:151 Y:0.648825 U:0.679378 V:0.682660 All:0.659556 (4.679542)

Each file consists of key:value pairs separated by spaces, so you can extract them with awk. This command shows the 5 frames with the lowest psnr_y.

bash
awk '{for(i=1;i<=NF;i++){split($i,a,":"); if(a[1]=="psnr_y") print a[2], $1}}' psnr.log | sort -n | head -5
text
30.71 n:251
30.74 n:253
30.74 n:255
30.75 n:252
30.77 n:256

At 30 fps, frame 251 falls at about 8.33 seconds ((251−1)÷30).

I split the frames into the first half (frames 1 to 150) and the second half (frames 151 to 300) and averaged the Y values.

Converted PSNR Y first half PSNR Y second half SSIM Y first half SSIM Y second half SSIM Y overall
CRF 23 44.41 dB 33.51 dB 0.9945 0.7512 0.8729
CRF 35 35.88 dB 30.97 dB 0.9705 0.6427 0.8066
Line graph of per-frame PSNR (Y) and SSIM (Y) for CRF 23 and CRF 35. PSNR is on top and SSIM is below. Both values drop sharply at the 5-second mark
Per-frame PSNR and SSIM. Noise appears in the original video from 5 seconds

The thin peak around 8.3 seconds in the graph is frame 250. Using ffprobe to check frame types, both videos had I-frames (frames that do not reference other frames) only at frames 1 and 250. For CRF 23, PSNR (Y) rises from 33.62 dB at frame 249 to 36.83 dB at frame 250, then falls to 32.75 dB at frame 251.

For CRF 35, the overall SSIM is 0.8066, but the first half alone is 0.9705 and the second half is 0.6427. The overall 0.8066 alone does not show that the first half barely changed while the second half dropped sharply. For a comparison of what happens when CRF is raised on footage with grain noise, see my earlier article, "How changing CRF in FFmpeg affects quality and file size", which compares enlarged images side by side.

Videos with different resolutions cannot be compared directly

When I compared small.mp4, the original scaled down to 640×360 and then converted, the command stopped with an error.

text
[Parsed_psnr_0 @ 0x55d2dc4ae680] Width and height of input videos must be same.
[Parsed_psnr_0 @ 0x55d2dc4ae680] Failed to configure input pad on Parsed_psnr_0

The documentation also says both inputs must have the same resolution and pixel format. Use scale to bring the converted video back to the original size before comparing.

bash
ffmpeg -i small.mp4 -i original.mp4 \
  -lavfi "[0:v]scale=1280:720[a];[a][1:v]psnr;[0:v]scale=1280:720[b];[b][1:v]ssim" \
  -f null -
text
PSNR y:32.289830 u:30.095621 v:27.913140 average:30.830173 min:29.432048 max:33.123405
SSIM Y:0.801491 (7.022196) U:0.806095 (7.124115) V:0.810606 (7.226330) All:0.803777 (7.072510)

This also printed the following warning for each frame, producing 600 lines (300 each for psnr and ssim).

text
master and reference frames use different color ranges (tv != unknown)

This warning means the color range information is tv in the video after scale, but unknown (not recorded) in the original. Adding setparams=range=unknown after scale removed all the warnings, and PSNR and SSIM matched to six decimal places.

bash
ffmpeg -i small.mp4 -i original.mp4 \
  -lavfi "[0:v]scale=1280:720,setparams=range=unknown[a];[a][1:v]psnr;[0:v]scale=1280:720,setparams=range=unknown[b];[b][1:v]ssim" \
  -f null -

The numbers also changed depending on how I matched the sizes.

How sizes were matched PSNR (average)
Converted video upscaled to 1280×720 (no flags specified) 30.83 dB
Converted video upscaled to 1280×720 (flags=lanczos) 30.87 dB
Original downscaled to 640×360 40.27 dB

Even with the same small.mp4, comparing by downscaling the original gave about 9.4 dB higher PSNR than upscaling the converted video. When comparing settings, you must use the same size-matching method for each.

With different frame counts, the shorter video's last frame is compared against the rest

The documentation assumes both inputs have the same number of frames and are compared one frame at a time. I compared first5.mp4, which contains only the first 5 seconds (150 frames), against the 10-second original (300 frames).

text
frame=  300 fps= 85 q=-0.0 Lsize=N/A time=00:00:09.96 bitrate=N/A speed=2.83x
PSNR y:26.032777 u:21.658340 v:22.155699 average:24.185933 min:18.439697 max:46.194087

There was no error, and the result covered all 300 frames. Looking at the per-frame values, frame 150 was 43.57 dB and frame 151 dropped to 22.45 dB. The values at frame 300 (Y 23.71 dB, U 20.33 dB, V 19.71 dB) matched the comparison of the last frame of first5.mp4 with frame 300 of the original. This shows that from frame 151 onward, the original was being compared against the shorter video's last frame. Swapping the first and second inputs gave the same result.

To stop when the shorter input ends, add shortest=1. This is a framesync option available in some filters that take multiple inputs.

bash
ffmpeg -i first5.mp4 -i original.mp4 \
  -lavfi "[0:v][1:v]psnr=shortest=1;[0:v][1:v]ssim=shortest=1" -f null -
text
PSNR y:44.277911 u:44.899718 v:45.442659 average:44.553290 min:43.567612 max:46.194087
SSIM Y:0.994465 (22.568535) U:0.997536 (26.082729) V:0.998192 (27.428827) All:0.995598 (23.563233)

The stats_file output had 150 lines. When you convert only part of a video from the beginning, either add shortest=1 or trim the original to the same range before comparing. If the converted video was cut from the middle, you must also trim the original from the same starting point.

Warning

Even when the frame counts match, if the two videos use different timestamp units (time_base), the frames being compared may be offset from each other, which can cause PSNR to drop sharply. For the case where the containers differ, such as MKV and MP4, my earlier article "How changing CRF in FFmpeg affects quality and file size" covers how to align the timestamps.

Comparing images before and after conversion with the same commands

I extracted one frame from the video as a PNG and converted it to JPEG at different quality levels (-q:v, where a lower value means higher quality).

bash
ffmpeg -i frame.png -q:v 10 q10.jpg
ffmpeg -i q10.jpg -i frame.png -lavfi "[0:v][1:v]psnr;[0:v][1:v]ssim" -f null -
JPEG -q:v Size PSNR (average) SSIM (All)
Original PNG 107,608 bytes — —
2 105,090 bytes 52.79 dB 0.9985
10 51,143 bytes 41.47 dB 0.9902
31 27,551 bytes 35.56 dB 0.9716

The PNG uses RGB (rgb24) and the JPEG uses YUV (yuvj444p), so the pixel formats differ. There was still no error, and the video passed to psnr had the pixel format yuvj444p. The values reported are per component (Y, U, and V), not per RGB channel. Since these are single images, the min and max values of PSNR are the same as average.

​Posted by 86