Convert MPEG/AVI video to animated GIF

Image credit: Frank Starmer

Today a friend asked me how to convert an MPEG video file into an animated GIF file and I would like to share here our findings.

In Linux (e.g. Ubuntu) you can use the FFMPEG and ImageMagick tools to do the following:

Convert an MPEG file into an AVI file

ffmpeg -i video_origin.mpg video_final.avi

Convert an AVI file into an animated GIF file

ffmpeg -i video_origin.avi gif_anime.gif

Now, if you get the following error after the last step

ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24

you can do what it’s suggested and re-run the last command like this

ffmpeg -i video_origin.avi -pix_fmt rgb24 gif_anime.gif

Note: It matters to put

-pix_fmt rgb24

where it’s indicated, otherwise you may get an

Option pixel_format not found


At this point you may have ended with a fairly large GIF file. Here is where we can use ImageMagick to reduce its size using

convert -layers Optimize output.gif output_optimized.gif

If your final gif file is still too large, an option may be to select only one segment of your original MPEG file to convert it into an animated gif. For this you can do, for example:

ffmpeg -i source.mpg -ss 00:00:13 -t 00:00:09 out.mpg

Here, we are seeking to the 13 seconds position and set the duration of the segment to 9 seconds.

Finally, perhaps you want your animated gif playing in an infinite loop. We can use ImageMagick convert command:

convert -loop 0 -layers Optimize out.gif outloop.gif

For Windows, there is the following software that looks promising for achieving our objective: Movie to animated GIF converter

For Mac OSX, here is an example of a commercial software that offers to do what we want: Xilisoft Video Snapshot for Mac

What is your solution of choice to convert MPEG/AVI video files into animated GIF files?

Related links:

How can I get ffmpeg to convert a .mov to a .gif?

19 ffmpeg commands for all needs


Leave a Reply