How to compress gif effectively to reduce size?

爱⌒轻易说出口 提交于 2021-01-29 11:24:32

问题


We use gifs for our blog extensively. We used to embed tenor nano gifs(90px height maintaining aspect ratio, used for GIF previews and shares on mobile) in it. Now we wanted to create our own gifs and are using the following command to convert mp4 to gif while maintaining the properties of tenor's nano gif. using ffmpeg version 4.1.4

But we observed a huge difference in size between the gif we created and the one created using tenor.

ffmpeg -i input.mp4 -filter_complex "[0:v]fps=10,scale=-1:90:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" -y output.gif

[Original MP4] - 845KB

Tenor Nano gif - 42KB

ffmpeg gif - 106KB

We even tried changing dithering algorithm to further reduce size but it ended up adding noise and damaged the gif quality

paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle

We tried tweaking colour quantization in gifsicle as well but it was of no use.

gifsicle --resize _x90 --colors 256 --color-method diversity --dither=ordered --resize-method sample input.gif > output.gif


回答1:


Try this bat file. I wrote it for myself. I have Windows os.
It converts FFmpeg mp4 to gif

  1. Create two packages Your_files and Result put the bat file next to the folders.
    Put these folders and baht file next to ffmpeg.exe
  2. Drag your files (file) to the Your_files folder
  3. Run bat file
  4. We take the finished files from the Result folder
color a
@echo off
set a="Your_files\*.mp4"
set b="Result\%%~na.gif"
set c=ffmpeg
set f=-filter_complex "[0:v] fps=10,scale=-1:-1:flags=full_chroma_int,split [a][b];[a] palettegen=max_colors=255:reserve_transparent=1:stats_mode=diff [p];[b][p] paletteuse=dither=none:bayer_scale=5:diff_mode=rectangle:new=1:alpha_threshold=128" -gifflags -offsetting
for %%a in (%a%) do (%c% -y -i "%%a" %f% %b%)

If it crashes, remove these lines: -gifflags -offsetting



来源:https://stackoverflow.com/questions/57391632/how-to-compress-gif-effectively-to-reduce-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!