Animated Gifs on Linux with ffmpeg and imagemagick

By | June 11, 2013

This post is somewhat inspired by the fact that Google+ now makes cool little gif animations from photo bursts of 5 pics or more when you upload.  Very nifty.  But, ugh, the images are SMALL and often choppy.  What if you want larger picture dimensions, smoother or longer animations?  For those who care about making these *ridiculously cute* animations out of mpeg or avi movies from your phone/camera/youtube… this is for you. Just be careful not to use movie clips longer than 15 to 20 seconds or you’ll end up with a huge image file.

The workflow:

Install Linux, ffmpeg, imagemagick, and stuff.  Mostly just install Linux.  If you don’t, you can stop reading here and we can still be friends.  But no gif for you…

Alright then.  So just get your mpeg 4 movie and open a terminal in the directory where your movie has been saved, then …

$ mkdir gif
$ ffmpeg -i YOUR_MOVIE.mpg -an -r 10 -y -s 640x480 gif/capture%03d.png # *see footnote
$ cd gif
$ for file in *png; do echo converting $file ...; convert $file `basename $file .png`.gif; done
$ convert -delay 10 cap*gif -loop 0 anim.gif
$ convert -layers Optimize anim.gif anim_optimized.gif
$ mv anim_optimized.gif ../
$ cd ../
$ rm -rf ./gif

… and enjoy anim_optimized.gif !!!

Adjust the gif image dimensions to taste (my 1080p mp4 file needed to be scaled way down to 640×480.  You might want yours higher/lower res).

This workflow solves all the problems with the stuff you’ll find on stack exchange and the intergoogles, it will save you all the time needlessly tinkering and swearing loudly while debugging your way through all the bad information you keep finding online and the arcane syntax in the ffmpeg man page.

Click the image for the larger GIF

Click the image for the (much) larger GIF

*Footnote: this “%03d” bit should cover you for movie clips with (way too) many frames, but really you shouldn’t make gifs longer than 15 seconds.  They can become very large in file size and take a lot of time to download. The gif image you see here is about 70 frames.