Calculate PSNR Between Two Videos with FFmpeg
This article provides a straightforward guide on how to calculate the Peak Signal-to-Noise Ratio (PSNR) between two videos using FFmpeg on Linux. You will find the exact command-line syntax, an explanation of its parameters, and how to interpret the resulting log data to evaluate video quality loss.
To calculate the PSNR between a reference video and a distorted
(compressed) video, use the FFmpeg psnr filter via the
-filter_complex flag. This ensures both videos are properly
synchronized and compared frame by frame.
The FFmpeg PSNR Command
Run the following command in your Linux terminal:
ffmpeg -i distorted_video.mp4 -i reference_video.mp4 -filter_complex "psnr" -f null -How the Command Works
-i distorted_video.mp4: The first input is typically the video you want to test (e.g., the compressed or transcoded file).-i reference_video.mp4: The second input is the original, uncompressed, or high-quality source video used as the benchmark.-filter_complex "psnr": This activates the complex filtergraph to map both inputs and compare their corresponding pixels.-f null -: Since you only need the text-based quality metrics and not a new video file, this tells FFmpeg to discard the video output payload while still printing the calculation results to the console.
Saving the Results to a Log File
If you want to analyze the frame-by-frame metrics or save the summary for later, you can instruct FFmpeg to output a log file by modifying the filter parameters:
ffmpeg -i distorted_video.mp4 -i reference_video.mp4 -filter_complex "psnr=stats_file=psnr_report.log" -f null -Understanding the Output
After the command finishes processing, FFmpeg will print a summary line in the terminal that looks similar to this:
[Parsed_psnr_0 @ 0x5555557a30] PSNR y:42.15 u:48.32 v:48.71 average:43.24 min:38.12 max:inf
- y, u, v: Represents the PSNR values for the individual color channels (Luminance and Chrominance). The y value is generally the most important for human visual perception.
- average: The overall average PSNR value across the entire video.
- min / max: The lowest and highest quality frames
detected during the comparison. An
inf(infinite) value means at least one frame was an identical bit-for-bit match.
Key Considerations
For the calculation to be accurate, both videos must have the exact same frame rate, duration, and resolution. If your distorted video has a different resolution due to downscaling, you must scale it back to match the reference video dimensions within the filter chain before applying the PSNR filter.