How to Use FFmpeg SSIM Filter to Compare Videos
This article provides a straightforward guide on how to use the
FFmpeg ssim filter to calculate the Structural Similarity
Index (SSIM) between two video files. You will learn the exact
command-line syntax required to run the comparison, how to interpret the
resulting SSIM scores, and how to export the data to a log file for
analysis.
The Basic FFmpeg SSIM Command
To calculate the SSIM between a modified (distorted) video and the original (reference) video, both files must have the same resolution and framerate. Use the following command in your terminal:
ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]ssim" -f null -Command Breakdown: * -i distorted.mp4:
Path to the video you want to test (input 0). *
-i reference.mp4: Path to the original, high-quality video
(input 1). * -filter_complex "[0:v][1:v]ssim": Takes the
video streams of both inputs and passes them through the
ssim filter. * -f null -: Tells FFmpeg not to
write an output video file, but to process the frames and display the
results in the console.
Interpreting the Output
Once processing is complete, FFmpeg will print the SSIM results in the console. The output will look similar to this:
Detailed SSIM results: Y:0.985210 U:0.991204 V:0.990855 All:0.986841 (18.807812)
- Y, U, V: These represent the similarity index for the luminance (Y) and chrominance (U/V) channels. The Y value is generally the most important for human visual perception.
- All: The overall global SSIM value.
- Scale: SSIM values range from 0 to 1. A value of 1.0 means the two videos are completely identical. Values closer to 1 indicate higher similarity and less visual degradation.
- dB Value (in parentheses): This is the SSIM expressed on a decibel scale, where higher numbers (e.g., above 30-40 dB) represent better quality.
Saving SSIM Results to a Log File
If you need to analyze the SSIM score frame-by-frame, you can save
the results directly to a text file using the stats_file
option:
ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]ssim=stats_file=ssim_log.txt" -f null -This command generates a file named ssim_log.txt in your
current working directory, listing the precise SSIM scores for every
individual frame of the video.
Handling Different Resolutions or Framerates
The ssim filter requires both video inputs to have
matching dimensions and framerates. If your distorted video has a
different resolution, you must scale it to match the reference video
within the filter graph:
ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v]scale=1920:1080[dist];[dist][1:v]ssim" -f null -In this example, the distorted video is scaled to 1920x1080 before being compared to the reference video of the same size.