How to generate gif from avi using ffmpeg?
I had a similar problem trying to generate high quality animated gif from a series of images extracted from a movie.
For some reasons the animated gif generated with ffmpeg only contains 103 colors assumable using a fixed 256 level system color palette resulting in horrific result. My solution was instead
ffmpeg -i video.avi -t 10 out%02d.gif
then
gifsicle --delay=10 --loop *.gif > anim.gif
Quality is then quite good. You can find gifsicle here
Edit: Updated the post to reflect Alex Kahn's suggestions.
Marconi
Updated on December 20, 2020Comments
-
Marconi over 3 years
I'm trying to extract a part of a video into an animated gif using the following command:
ffmpeg -i video.avi -t 5 out.gif
It generates an animated gif but the quality is insane. However when I generate gif image using:
ffmpeg -i video.avi -t 10 out%d.gif
It generates acceptable quality of gif images. How can i generate animated gif using the first command but the same quality as the second command?
-
Marconi about 13 yearsThanks for your reply. However I found out that ffmpeg is not really good at handling gif images so I used it to extract images but then use imagemagick to generate the gif.
-
Profane almost 13 yearsNot sure why I'm receiving lots of downvotes here. I stated up front I hadn't made animated gifs. Was trying to point the person with the question to other relevant parameters. The old FFMPEG project seemed to glory in having hidden many of its parameters outside of the docs. Maybe I'm getting downvoted for revealing its terrible, dark secrets?
-
Alex Kahn over 11 yearsThank you! One adustment: use out%02d.gif. This pads the number in the filename so that they are in proper alphabetical order. Otherwise the order ends up being 1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,23... which gives the resulting gif a weird skipping effect.
-
Mathias Bynens almost 11 yearsYou could also have
ffmpeg
output PNGs instead of GIFs to avoid the crappy quality. FWIW, here’s a nice write-up on creating GIFS in Bash: blog.room208.org/post/48793543478 -
funkaster over 10 yearsThis works great. One thing you might also want to do, is to skip frames: using
seq -f %02g.gif start skip stop
instead of*.gif
in the gifsicle command, you can skipskip
frames, starting atstart
and ending atstop
frames. Pretty useful. E.g.: gifsicle --delay=10 --loopseq -f %02g.gif 1 3 156
> anim.gif Will start at frame 1, skip 3 frames and stop at frame 156. -
Marat Buharov about 10 yearsin addition I suggest the next step: convert -layers Optimize anim.gif anim_opt.gif
-
Dims about 10 years
gifsicle
looks not working at all. says "gifsicle.EXE: *.gif: Invalid argument" in response to your command -
lepe over 9 yearsfor smaller GIF file size, you can adjust the fps, for example (5FPS):
ffmpeg -i input.mp4 -vf fps=fps=5 out%04d.gif; gifsicle --delay=20 --loop out*.gif > anim.gif; rm out*.gif;
In the case of ffmpeg, if you specify lower FPS the number of files will be reduced. In the case of gifsicle, you need to delay further to compensate the missing frames. gifscile uses a scale of 1/100sec, which 20/100 means 5FPS. -
wchargin over 9 yearsProtip:
gifsicle -O3
optimizes gifs nicely.