Export PSNR and SSIM Frame Scores with FFmpeg

This article provides a step-by-step guide on how to calculate and export frame-level Peak Signal-to-Noise Ratio (PSNR) and Structural Similarity Index Measure (SSIM) scores to an external log file using FFmpeg. By leveraging FFmpeg’s video filters, you can generate detailed per-frame quality metrics comparing a processed video against its original source.

To measure video quality accurately, both the reference (original) video and the distorted (compressed) video must have the same resolution, frame rate, and duration. FFmpeg allows you to calculate these metrics and write the results directly to text files using the stats_file option within the psnr and ssim filters.


Exporting PSNR Frame Scores

To generate a log file containing only PSNR scores for every frame, run the following command:

ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]psnr=stats_file=psnr_log.txt" -f null -

Command Breakdown: * -i distorted.mp4: The compressed or modified video file. * -i reference.mp4: The original, uncompressed source video. * -filter_complex "[0:v][1:v]psnr=stats_file=psnr_log.txt": Passes both video streams into the psnr filter and tells FFmpeg to write the frame-by-frame results to psnr_log.txt. * -f null -: Prevents FFmpeg from rendering a new output video file, saving processing time and disk space since you only need the text log.


Exporting SSIM Frame Scores

To generate a log file containing only SSIM scores for every frame, use the following command:

ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]ssim=stats_file=ssim_log.txt" -f null -

Command Breakdown: * ssim=stats_file=ssim_log.txt: Passes the two video streams to the ssim filter and outputs the results to ssim_log.txt.


Exporting Both PSNR and SSIM Simultaneously

You can calculate both metrics in a single command execution to save time. By chaining the filters, the distorted video passes through the PSNR filter first, and is then compared against the reference video again in the SSIM filter:

ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]psnr=stats_file=psnr_log.txt[psnr_out];[psnr_out][1:v]ssim=stats_file=ssim_log.txt" -f null -

Command Breakdown: * [0:v][1:v]psnr=stats_file=psnr_log.txt[psnr_out]: Compares the two inputs, writes the PSNR log, and outputs the processed stream as [psnr_out]. * [psnr_out][1:v]ssim=stats_file=ssim_log.txt: Takes the stream from the PSNR filter, compares it with the original reference video ([1:v]), and writes the SSIM log.


Understanding the Log Files

Once the command finishes, two text files will appear in your working directory.

PSNR Log Format

The psnr_log.txt file will contain lines structured like this:

n:1 mse_u:0.12 mse_v:0.08 mse_y:1.24 psnr_u:57.34 psnr_v:59.09 psnr_y:47.19 psnr_avg:48.31 

SSIM Log Format

The ssim_log.txt file will contain lines structured like this:

n:1 Y:0.987123 U:0.991234 V:0.990123 All:0.988543 (19.410293)