Detect if a GIF is Animated

By  on  
WTF

If you tweet an animated GIF, you'll notice that Twitter takes it and converts it to an mp4.  I'd speculate that they do that conversion because an MP4 is better supported across platforms, at least in that they allow for playing/pause of the animation. And with a video you can prevent infinite looping, unlike a GIF.  How can you determine if a GIF is animated though?  I found a few good solutions.

Command Line with ImageMagick

ImageMagick, the amazing image manipulation library, provides a method for counting frames in a GIF:

./ImageMagick-6.9.0/bin/identify -format %n wtf.gif

The command above will provide you a count of the frames within the GIF.

animated-gif-detector

For those of you that prefer JavaScript (...my people...), I found a really simple animated GIF detection library on NPM:  animated-gif-detector.  Its API couldn't be shorter:

var fs = require('fs');
var animated = require('animated-gif-detector');

> animated(fs.readFileSync('wtf.gif'));
// true

> animated(fs.readFileSync('blank.gif'));
// false

The function call simply returns true or false, representing if the GIF is animated.  Exactly what I wanted!

As for what each library looks for, I've seen some ugly bit code I wont try to describe.  If you want to determine if a GIF is animated, however, it's as easy as the utilities above!

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

Incredible Demos

  • By
    MooTools Equal Heights Plugin:  Equalizer

    Keeping equal heights between elements within the same container can be hugely important for the sake of a pretty page. Unfortunately sometimes keeping columns the same height can't be done with CSS -- you need a little help from your JavaScript friends. Well...now you're...

  • By
    Chris Coyier’s Favorite CodePen Demos II

    Hey everyone! Before we get started, I just want to say it's damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...

Discussion

  1. Only as a side note, Twitter is using mp4 because it’s an stream. Gif loading prevents the browser to fire the “ready” state until all of them are loaded. By using the video the event is fired meanwhile all the “gif” are being loaded in parallel.

  2. And of course there is this webservice: https://doesthisgifcontainananimation.com/

    • Christian Læirbag

      Hey Robert: Any Idea on how to put that service to work? I can’t find the correct syntax to use with http://i.imgur.com/tYqyhJT.gif for example. If you have the solution, please let me know. Ok?

    • It’s easy! In Javascript, you can call: encodeURIComponent("http://i.imgur.com/tYqyhJT.gif")

      Then you append the result to the domain, just like this:
      https://doesthisgifcontainananimation.com/http%3A%2F%2Fi.imgur.com%2FtYqyhJT.gif

      You can then see, whether the GIF contains an animation or not. :)

    • Christian Læirbag

      Oh, now I get it. I wasn’t understanding that encoding stuff. Thanks for clearing it out! :)

      By the way: I’m now trying to get result to be fetched from a Javascript or Yahoo Pipe, but I’m not getting close. If you have or manage to find something, please bring some words once more O.K.?

    • Christian Læirbag

      Duhh!… Forget about it. Yahoo Pipes is shutting down, so…Nevermind.

  3. Peter Galiba

    Please, don’t advocate fs.readFileSync as it is not a good approach in 99% of the cases. animated-gif-detector supports streams, so use them instead. And you don’t even have to continue reading the file when you have already detected that it is a animated gif. Animated gifs can be big, and reading big files into memory is not too efficient, especially if they are not even on your local machine, but read from a network.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!