Using Cloudinary’s Fetch API to convert an animated GIF to a video

I like animated GIFs, like most people these days I think, but they are really heavy, hurting the performance of web pages, and consuming data plans faster than should be needed. So we need to convert them to videos, which are much lighter, for the same visual result. Let’s use Cloudinary.

You need a Cloudinary account to try all of this, but no fear, its free plan is really confortable for personnal use or just testing: signup for Cloudinary.

The animations in this post are animated GIFs provided by Giphy and obviously converted by Cloudinary.

Cloudinary has been providing animated GIFs to video conversion for a while, as this 4 years old blog post shows: Reduce size of animated GIFs, automatically convert to WebM and MP4.

Here is the simple process it described:

First upload the animated GIF to Cloudinary, so that it is available at this URL:

Then, change the file extension at the end of the URL to ask Cloudinary to convert it into MP4 or Webm video:

Easy! Magical!

But I want my publication process to be even easier, not requiring any upload (manual or automated) of my digital assets.

That’s why I find Cloudinary’s Fetch API awesome!

You can also use Cloudinary’s Auto-Upload, which provides a lot more features, but I like to keep things simple, and my master pristine images are hosted on my site anyway. Actually, my Jekyll-Cloudinary plugin uses the Fetch API to provide simple and efficient responsive images to Jekyll users.

So, how can we use the Fetch API to convert animated GIFs to videos?

Let’s say the pristine animated GIF is located at

The simple Fetch API URL to serve this image though Cloudinary, but untouched, would be this:<cloud_name>/image/fetch/

<cloud_name> should be replaced by your own cloud_name.

If we try to replace .gif with .mp4 at the end of this URL, like in the 4 years old Cloudinary post, it won’t work, because Cloudinary will try to fetch a video located at, which doesn’t exist.

The solution is to use the explicit format conversion parameter (f_) you can set in your Fetch URL, before the pristine image URL:<cloud_name>/image/fetch/f_mp4/

So, we can replace this:

<img src="" alt="an animation">

With this:

<video autoplay loop muted playsinline>
  <source src="<cloud_name>/image/fetch/f_mp4/" type="video/mp4">
  <source src="<cloud_name>/image/fetch/f_webm/" type="video/webm">
  <p>Your browser doesn't support HTML5 video, <a href="">download the animated GIF</a>.</p>

Don’t put an <img> tag with the animated GIF as the fallback, it would be downloaded anyway, even if the browser supports and shows the video.

Be careful, the transformation can take some time if the animated GIF is really heavy, so you might have to consider uploading it and perform the transformation asynchronously, without using the Fetch API.

I didn’t find any of this explained in Cloudinary documentation, maybe because it mixes images and videos.

One more thing…

If you want to dive deeper in this topic, you can discover how — in the near future — animated GIFs converted to videos could be better loaded in <img> tags, with Colin Bendell’s post in the 2017 edition of Performance (Advent) Calendar: Evolution of <img>: Gif without the GIF.

Additional resources

Si vous voulez signaler une erreur ou proposer une modification de ce texte, n’hésitez pas à l’éditer directement à la source sur Github.