How to convert video to animated gif image on Linux

Last updated on December 22, 2020 by Dan Nanni

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 Twitter, Reddit and 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. :-)

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean