Count Frames in an Animated GIF

By  on  

The animated GIF always makes me chuckle; on one hand they seem like a relic of the "old web", on the other hand they aren't going anywhere and their meme-like presence seem to only make them more popular.  If you browse through my media blog posts archives, you'll notice that I've written a ton about animated GIFs, including how to tell if a gif is animated, how to get a screenshot of the first frame, converting GIF to WEBP, and more.

My curiosity recently brought me to asking how to detect how many frames were present in an animated GIF; as expected, ImageMagick had the answer:

identify -format "%n\n" bitcoin.gif | head -n 1
>> 101

What is this calculation useful for?  I suppose if you have a media management site you could show each frame to a user so they could choose a poster when converting to video.  In any event, I was curious and simply had to find out!

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

Discussion

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