How to generate gif from avi using ffmpeg?

47,728

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.

Share:
47,728
Marconi
Author by

Marconi

Updated on December 20, 2020

Comments

  • Marconi
    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
    Marconi about 13 years
    Thanks 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
    Profane almost 13 years
    Not 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
    Alex Kahn over 11 years
    Thank 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
    Mathias Bynens almost 11 years
    You 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
    funkaster over 10 years
    This 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 skip skip frames, starting at start and ending at stop frames. Pretty useful. E.g.: gifsicle --delay=10 --loop seq -f %02g.gif 1 3 156 > anim.gif Will start at frame 1, skip 3 frames and stop at frame 156.
  • Marat Buharov
    Marat Buharov about 10 years
    in addition I suggest the next step: convert -layers Optimize anim.gif anim_opt.gif
  • Dims
    Dims about 10 years
    gifsicle looks not working at all. says "gifsicle.EXE: *.gif: Invalid argument" in response to your command
  • lepe
    lepe over 9 years
    for 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
    wchargin over 9 years
    Protip: gifsicle -O3 optimizes gifs nicely.