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
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

Discussion

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