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

polarka picture polarka · Sep 10, 2014 · Viewed 31.8k times · Source

I am a newbie at encoding. I have read and tried x264 in lossless mode (-qp 0), however I'd like to make sure that in my new video, every single pixel contains the same information as the source file (which is in YUV 420 so the loss of color conversion is avoidable, as far as I know). I want to be able to check that, because I don't believe in that if someone just says its lossless.

I welcome answers suggesting other codecs for lossless encoding, my only requirements for codecs are having one of the best compression rate and let me to pick different calculation times (such as the range from placebo to veryfast in x264) in order to adjust the compression level and calc time to my needs. But keep in mind that the original question is about how can I calculate the differences frame by frame of two videos and export it to a 3rd file, so I can watch it myself. I think that knowledge (if its possible and doesnt have serious limitations) will be useful for me in the future too.

Answer

llogan picture llogan · Sep 11, 2014

Comparison of decoded data with MD5 hash

You can use the FFmpeg MD5 muxer to show that the decoding results in the exact same output:

  1. Get MD5 hash of the video stream from your original input:

    $ ffmpeg -loglevel error -i original.vid -map 0:v -f md5 -
      MD5=5ee3ae1ee5feaf30618938290225f682
    
  2. Convert to a lossless output:

    $ ffmpeg -i original.vid -c:v libx264 -qp 0 lossless.mkv
    
  3. Compare MD5 hash of the lossless video:

    $ ffmpeg -loglevel error -i lossless.mkv -map 0:v -f md5 -
      MD5=5ee3ae1ee5feaf30618938290225f682
    

Notes:

  • You may not get the same hash even with a lossless encoder. Changes to various attributes can occur that can alter the MD5 hash such as the colorspace or chroma subsampling.

  • You can see that the MD5 hash can change if you output to a lossy format.

  • Other losslessly compressed video encoders supported by FFmpeg include: ffv1, ffvhuff, huffyuv, and utvideo.

  • See the framemd5 muxer to view the hash for each frame.


Visual comparison

With the blend filter

Viewing the difference of a lossy output Viewing the difference of a lossy output.

You can use the blend filter to visually compare the difference.

Using ffplay

ffplay -f lavfi \
"movie=original.mkv[org]; \
 movie=encoded.mkv[enc]; \
 [org][enc]blend=all_mode=difference"
  • blend is slow, and this command may not play in real time depending on your CPU and the inputs. Alternatively you could output a video with ffmpeg then watch it as shown below.

  • There are modes other than difference that may fit your needs. See the documentation.

Using ffmpeg

ffmpeg -i original.mkv -i encoded.mkv \
-filter_complex "blend=all_mode=difference" \
-c:v libx264 -crf 18 -c:a copy output.mkv
  • You may need to add ,format=yuv420p to the end of your filterchain (immediately after difference) to view the output in non-FFmpeg based players.

With the overlay filter

See Display video difference with ffmpeg’s overlay filter.