6

According to the documentation, mpdecimate will "Drop frames that do not differ greatly from the previous frame in order to reduce frame rate."

I would like to be able to remove only exact duplicate frames from a video. I know the video has a ton of them that are similar, but I only want to get rid of exact duplicates.

Any advice on how to use ffmpeg with mpdecimate to do this (or some other tool)?

  • "losslessly" is not going to work (unless in rare cases, like when all the frames in the input video are key frames). There may be a way to mark the frames as "discarded", but I don't think FFmpeg supports it. – Rotem Feb 21 '22 at 23:22

1 Answers1

6

Values for hi and lo are for 8x8 pixel blocks and represent actual pixel value differences, so a threshold of 64 corresponds to 1 unit of difference for each pixel, or the same spread out differently over the block.

A frame is a candidate for dropping if no 8x8 blocks differ by more than a threshold of hi, and if no more than frac blocks (1 meaning the whole image) differ by more than a threshold of lo.

Default value for hi is 64 * 12, default value for lo is 64 * 5, and default value for frac is 0.33.

ffmpeg -i input.mkv -vf mpdecimate=hi=1:lo=1:frac=1:max=0 output.mkv

Should only drop an image if the whole image has no more than 1 pixel value difference to the previous.

My test with a static image rendered into a 5sec h264 video show that in reality 3 frames out of 125 get dropped. This is possibly due to compression artifacts.

When adjusting the high and low value to 200

ffmpeg -i input.mkv -vf mpdecimate=hi=200:lo=200:frac=1:max=0 output.mkv

then 21 frames are kept.

You might need to adjust the parameters according to your definition of exact duplicate.

mashuptwice
  • 2,929
  • 2
  • 12
  • 25
  • 1
    just to clarify, is mpdecimate supposed to actually *remove* frames from the output? because what i see is a bunch of identical frames to the first one replaced subsequent frames that were supposed to be dropped. – Michael May 12 '22 at 19:27
  • **Important**: The solution presented here only works if the input file has a variable framerate and/or the output file's codec defaults to a variable frame rate! For some codecs you must [explicitly state that with `-vsync vfr` for the output file](https://superuser.com/a/1792778/40829) in order to benefit the file size savings from the dropped frames, otherwise they may be filled up again with duplicate frames to form a constant frame rate output. – porg Jun 30 '23 at 23:52