39

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.

polarka
  • 546
  • 1
  • 5
  • 9

1 Answers1

70

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.

Denys Bulant
  • 316
  • 4
  • 10
llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thank you, I accept that. However, id like to know if its possible and how to create a video by the subtraction of every frame-pairs. My idea would be: convert to raw images –> perform the subtraction –> create the differential video. – polarka Sep 11 '14 at 19:53
  • Thanks for your fast response. [this page](https://trac.ffmpeg.org/wiki/FilteringGuide) helped me to understand the overlay filter method. – polarka Sep 13 '14 at 14:00
  • I noticed some desyncing in both results, it seems like one of the sources is ahead a little. I added setpts=PTS-STARTPTS to both methods, which definitely helped the overlay filter approach. I don't think it changed anything in the blend filter one. – polarka Sep 13 '14 at 14:11
  • @polarka If it's being weird with `ffplay` perhaps it just can't keep up in realtime (depending on your inputs, CPU, etc). I think blend itself can be slow, so you may try outputting a lossless file instead with `ffmpeg` and then watching that. – llogan Sep 13 '14 at 18:38
  • 1
    How to see rgb diff between frames? It's not clear green-white diff represent. – mrgloom Jan 24 '19 at 10:02
  • @mrgloom Difference between consecutive frames from one single stream, or the difference between two input streams/files? – llogan Jan 24 '19 at 22:04
  • difference between two input streams/files – mrgloom Jan 25 '19 at 05:57
  • @mrgloom Can't test or look into it at them moment, but you can try adding the format filter: `-filter_complex "[0]format=gbrp[v0];[1]format=gbrp[v1];[v0][v1]blend=all_mode=difference,format=yuv420p"`. – llogan Jan 25 '19 at 21:26
  • For steps 1 and 3, the ffmpeg commands are a lot faster if you add `-codec copy`. – pacoverflow Apr 02 '23 at 07:21