How to Export SSIM Scores to a Log File Using FFmpeg
This article provides a straightforward guide on how to use FFmpeg to calculate the Structural Similarity Index Measure (SSIM) between two videos and export both the frame-level and overall global SSIM scores to external log files.
To calculate and export SSIM scores, you will use FFmpeg’s
ssim filter. This filter compares a distorted (processed)
video against a reference (original) video. Both videos must have the
same resolution and frame rate for an accurate comparison.
The Command for Frame-Level SSIM
To output individual frame-by-frame SSIM scores to a log file, use
the stats_file option within the ssim filter.
Run the following command in your terminal:
ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]ssim=stats_file=frame_ssim.log" -f null -Command Breakdown: * -i distorted.mp4:
Path to the video you want to evaluate. * -i reference.mp4:
Path to the original source video. *
-filter_complex "[0:v][1:v]ssim=stats_file=frame_ssim.log":
Feeds both video streams into the ssim filter and instructs
FFmpeg to write the frame-level results to frame_ssim.log.
* -f null -: Prevents FFmpeg from rendering a new output
video file, which speeds up the process since you only need the log
data.
Understanding the Frame-Level Log File
Once the process finishes, open frame_ssim.log. Each
line represents a frame and displays the SSIM values for the Y
(luminance), U (chrominance blue), and V (chrominance red) channels,
followed by the combined average:
n:1 Y:0.985211 U:0.991203 V:0.990145 All:0.986812 (18.80)
n:2 Y:0.985140 U:0.991150 V:0.990098 All:0.986745 (18.78)
n: The frame index (starting at 1).All: The combined SSIM score for that specific frame (ranging from 0 to 1, where 1 is identical).- The value in parentheses (e.g.,
18.80) is the SSIM expressed in decibels (dB).
Exporting the Global SSIM Score
FFmpeg calculates the overall (global) SSIM score for the entire video and prints it to the console (stderr) at the end of the process.
To automatically capture this global summary into a separate text
file, redirect the terminal output using 2>:
ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]ssim=stats_file=frame_ssim.log" -f null - 2> global_ssim.logAfter the command runs, search the global_ssim.log file
for the line starting with [Parsed_ssim_0. It will look
like this:
[Parsed_ssim_0 @ 0x7fa89bc0c300] SSIM Y:0.984120 U:0.990112 V:0.989945 All:0.985810 (18.480124)
This single line provides the final, average global SSIM score across the entire duration of the video.