乐闻世界logo
搜索文章和话题

How to compare/show the difference between 2 videos in ffmpeg?

1个答案

1

In FFmpeg, comparing differences between two videos is an advanced technique that can be achieved through specific methods. Here is a basic example demonstrating how to use FFmpeg to compare two video files and display the differences.

First, ensure that the latest version of FFmpeg is installed, as older versions may not support certain required filters.

Then, you can use the blend filter to compare two videos. This filter mixes corresponding frames of the two video streams. By appropriately setting parameters, the differences between corresponding frames can be visualized.

A simple command is as follows:

shell
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]blend=all_mode='difference'" -y diff.mp4

In this command:

  • -i video1.mp4 specifies the first video file.
  • -i video2.mp4 specifies the second video file.
  • -filter_complex is used to define the filter graph; here we use two input videos [0:v][1:v].
  • blend=all_mode='difference' is the core component, configuring the blend filter to operate in difference mode, so the output video displays the differences between corresponding frames of the two input videos.
  • -y parameter indicates overwriting the output file without prompting.
  • diff.mp4 is the output video file displaying the differences.

The above method outputs a video where each frame represents the difference between the corresponding frames of the input videos. If the videos are identical, the output video will be entirely black, as the differences are zero. Differences will appear as white or gray areas, with brightness varying according to the magnitude of the differences.

Additionally, for more complex analysis, such as calculating the Structural Similarity Index (SSIM) or Peak Signal-to-Noise Ratio (PSNR) between two videos, you can use the corresponding filters provided by FFmpeg.

For example, the command to compare using SSIM is:

shell
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]ssim=diff_ssim.log" -f null -

This command outputs the SSIM log to the diff_ssim.log file and does not produce a video output. An SSIM value closer to 1 indicates that the videos are more similar.

Ensure that both videos have the same resolution and frame rate; otherwise, FFmpeg may not process them correctly. If the videos have inconsistent specifications, you may need to transcode them to match before comparing differences.

2024年6月29日 12:07 回复

你的答案