Create Animated GIFs from Videos on Linux

Animated gifs are ubiquitous on social media websites, messengers and more. They are usually short and straight to the point. Have you ever wanted to share a short clip of an interesting part of a video? You can create an animated gif from a video file which you can share via multiple social sites and/or chat applications.

This article is a discussion on how to create an animated gif of a snippet of a video on Linux using command line tools.

To create an animated gif from a video, we require two tools. The first would be used to snip or cut just the part of the video we are interested in and at the same time take snapshots and generate the individual gif images. The second tool would combine these generated gifs into the final animated gif.

Tool 1 is ffmpeg, which, according to it’s own definition, is able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. Unfortunately, for reasons outside the scope of this article, ffmpeg is not available on the official Ubuntu repositories for Ubuntu versions between 11.10 and 14.10. Thankfully, ffmpeg is scheduled to return to the official Ubuntu repositories from Ubuntu 15.04 to be released in April 2015.

To install ffmpeg in Ubuntu versions where it isn’t in the official repositories, you can either download the static builds from here, clone the source code using git,

or add Jon Svenrinsson’s PPA using the following commands:

The second tool is imagemagick which enables users to manipulate images in a variety of ways. Imagemagick is available via the official Ubuntu install channels and can be installed with the following command:

Animated gifs are better as short clips. An animated gif is a sequence of different gif images displayed one after the other. If each gif is 1MB, and your animation has 100 images, your animated gif would be 100MB. You definitely do not want to convert a two hour movie into an animated gif. The first thing we would do is to take snapshots of the video between a chosen start and stop point and save these snapshots as gif images.

First create a directory where the generated gifs would be saved.

Create a directory to save generated GIFs.

Then run the command, replacing “video.mp4” with your desired video file:

Run this command, replacing video.mp4 with video file.

In the command above, the options and their descriptions are:

  • ss – start recording at position. Position can either be a number in seconds or an absolute position in the video expressed in “hh:mm:ss[.xxx]” (with milliseconds optional)
  • i – the input video.mp4
  • to – stop recording at position. Position can also be a number in seconds (which means record for the given number of seconds), or it can be an absolute position in the video.
  • vf – filter graph. We use it to scale the generated images to our desired size (scale=width:height). We specified -1 for height, which indicates that the aspect ratio should be maintained, while the width should be 400 pixels. The generated images can be distorted by specifying arbitrary width and height values
  • r – frame rate (frames per second). Higher value means more images will be captured, which would result in a smoother animation but a bigger animated gif.

The folder “ffmpeg_dir” should now contain gif images. The total number of images depends on the length of your desired clip and the frame rate.

To merge the gifs into a giant animated gif, we use an imagemagick tool called convert.

Merge the GIFs into a giant animated GIF.

  • delay – specifies the length of time to pause between images (ie the animation speed). 1×5 indicates 5 frames per second.
  • loop – indicates the number of iterations required. 0 means infinite.

At this point you should have a custom built animation, ready to share with reddit, google+, facebook, bbm and more. Experiment with the frame rate on ffmpeg (-r) and convert (-delay) to achieve the smoothest animation at the smallest total file size. Now you can create animated gif images of snippets of your favourite videos.