How to convert video to animated gif image on Linux

Once thought of as outdated art forms, animated GIF images have now come back. If you haven't noticed, quite a few online sharing and social networking sites are now supporting animated GIF images, for example, on Tumblr, Flickr, Google+, and partly on Facebook. Due to their ease of consumption and sharing, GIF-ed animations are now part of mainstream Internet culture.

So some of you may wonder how you can create such animated GIF images. There are various online or offline tools dedicated to create animated GIF images. Another option is to create an animated GIF image off of an existing video clip. In this tutorial, I will describe how to convert a video file to an animated GIF image on Linux.

As a more useful example, let me demonstrate how to convert a YouTube video to an animated GIF image.

Step 1: Download a YouTube Video

First, download a YouTube video that you would like to convert. You can use youtube-dl tool to save a YouTube video as an MP4 file. Suppose you saved your favorite YouTube video as "funny.mp4".

Step 2: Extract Video Frames from a Video

Next, install FFmpeg on your Linux system, which I will use to extract video frames from the video.

The following command will extract individual video frames, and save them as GIF images. Make sure to use the output file format ("out%04d.gif") as is. That way, individual frames will be named and saved properly.

ffmpeg -t <duration> -ss <starting position in hh:mm:ss format> -i <input_video> out%04d.gif

For example, if you want to extract the video frames of input video, for 5 seconds, starting at 10 seconds from the beginning, run the following command.

$ ffmpeg -t 5 -ss 00:00:10 -i funny.mp4 out%04d.gif

After FFmpeg is completed, you will see a list of GIF files created, which are named as "out[\d+].gif".

Step 3: Merge Video Frames into an Animated GIF

The next step is to merge individual GIF files into one animated GIF image. For that, you can use ImageMagick.

First, install ImageMagick on your Linux system if you haven't done so.

Then, run the following command to merge created GIF images into a single animated GIF file.

convert -delay <ticks>x<ticks-per-second> -loop 0 out*gif <output-gif-file>

In the command, "-delay" is an option that controls the animation speed. This option indicates that [ticks/ticks-per-second] seconds must elapse before the display of the next frame. The "-loop 0" option indicates infinite loops of animation. If you want, you can specify "-loop N", in which case the animation will repeat itself N times.

For example, to create an animated GIF image with 20 frames-per-second and infinite loop, use the following command.

$ convert -delay 1x20 -loop 0 out*.gif animation.gif

Step 4 (Optional): Reduce the Size of an Animated GIF

The last (optional) step is to reduce the size of the created GIF file, by using ImageMagick's GIF optimizer.

Use the following command to reduce the GIF size.

$ convert -layers Optimize animation.gif animation_small.gif

Now you are ready to share the GIF image on your social networks. The following shows a sample GIF image that I created from a cute YouTube video.

Enjoy. :-)

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

6 thoughts on “How to convert video to animated gif image on Linux

  1. Thanks a lot for your tutorial. Unfortunately, it does not work with the only video I got downloaded from Youtube.
    I get this error message:

    [gif @ 0x1a6d140] ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24.

  2. I tried it, and I also made a bash script with it, but why is the output gif black and white? The video had colours!

Leave a comment

Your email address will not be published. Required fields are marked *