0

Some of the animations I export to animated gif with FFmpeg have long pauses where the frames are identical. Is there a way for FFmpeg to detect this and encode them as a single frame with a longer pause period? In other words, combine identical frames. I currently use

-vf palettegen=stats_mode=full
-f gif -lavfi paletteuse=diff_mode=rectangle:dither=none -loop -1

The file sizes are quite large as a result. Optimizing them with ezgif makes them a lot smaller by combining the frames.

  • Can you please post the complete FFmpeg command for exporting to animated gif? What is the input (assume you like to remove the duplicates while creating the GIF, and not after the GIF is created)? It's not clear from your question... – Rotem Jul 05 '23 at 17:03
  • To note, "ezgif' doesn't do it by "combining the frames" ("encode them as a single frame with a longer pause period"), it does it by setting transparency on the parts of the frame that are the same (see https://ezgif.com/help/optimizing-gifs) – Yisroel Tech Jul 05 '23 at 18:19
  • @Rotem I'm actually using a component in my code, which takes the above FFmpeg commands as input. I'm not sure what the commands would be to call FFmpeg directly. I may try doing that though. I would like it to combine as it makes the gif. If I know the command to make it happen then it should work either way. – XylemFlow Jul 10 '23 at 14:22
  • @YisroelTech It does that as well, but it definitely combines the frames. I can inspect the frames and delay periods in Gimp before and after optimizing in ezgif and see that the number of frames and periods have changed. – XylemFlow Jul 10 '23 at 14:22
  • Depending on which option you choose in the ezgif optimization, it might simply cut out frames, but nothing based on colors/similarity. It will simply cut out every Nth frame and change the duration. (If you think otherwise, can you share a GIF that you optimized, and which optimization option you uses, and ezgif changed frames and durations not in a specific order?) – Yisroel Tech Jul 10 '23 at 14:29
  • Try using [mpdecimate](https://ffmpeg.org/ffmpeg-filters.html#mpdecimate) filter, as described [here](https://superuser.com/a/1706241/635712) and [here](https://stackoverflow.com/a/37089629/4926757). – Rotem Jul 10 '23 at 15:15
  • 1
    @YisroelTech, read the second paragraph under 'Remove every nth frame' here https://ezgif.com/optimize . I used default options. In fact, I don't know how to change the options on that page. – XylemFlow Jul 11 '23 at 12:53
  • @Rotem, thanks this is what I was looking for. I hope that it will also modify the duration of the frames to keep the pauses? – XylemFlow Jul 11 '23 at 12:56
  • @Rotem, Unfortunately mpdecimate isn't available to me because I'm restricted to the LGPL version of FFmpeg. Looks like I can't do this then. – XylemFlow Jul 11 '23 at 13:40

1 Answers1

0

While not using ffmpeg, you can try accomplishing that with another popular command line tool ImageMagick.

This tool is way more opt for image processing/manipulation then is ffmpeg (which is for video stuff), and seems to be what the ezgif site most likely uses in the background.

See their Remove Duplicate Frames option (and all the other optmizitions there):
https://imagemagick.org/Usage/anim_opt/#removedups

Yisroel Tech
  • 9,687
  • 3
  • 23
  • 37