Cloudinary Blog

Reducing the Size of Animated GIFs and Converting Them to WebM or MP4 Through Automation

Animated GIF? Convert to WebM or MP4

Short videos of animated GIFs are spreading like wildfire around the web, especially in media and news sites, and people frequently share animated GIFs on social apps. However, because those GIFs are not optimized, their sizes are huge, consuming heavy bandwidth and slowing down page loads. Also, resizing and transforming a large number of animated GIFs, one by one, to match the graphic design of your site or app is a lengthy, CPU-intensive process.


Sign up for Cloudinary free today!


Since animated GIFs serve mainly as short videos, an effective way to reduce their file size is to convert the image format to a modern video format like WebM or MP4. Afterwards, you can still display the same animated content but would consume less bandwidth, accelerate page loads, and save server resources. All modern browsers support the HTML5 <video> tag and embedding of short videos as MP4 or WebM.

The first step is to convert all uploaded GIFs to video. However, not all browsers support all video formats; the MP4 or WebM container format works on only certain browsers. That means you must selectively convert your GIFs to the appropriate format for the viewing browser, which is a daunting challenge.

Look to Cloudinary's cloud-based image-management service for an automated conversion of animated GIFs to MP4s or WebMs for all modern browsers. Cloudinary also dynamically resizes, crops, and transforms those short videos to match your site design.

Conversion of Animated GIFs to MP4 and WebM Videos

To start using Cloudinary, first set up an account there and then upload images to your account’s Media Library. Cloudinary transforms those images on the fly according to the parameters you add to the dynamic URLs, which you can easily construct with one of Cloudinary’s client libraries (SDKs) for all popular development frameworks.

To automatically convert an animated GIF to a WebM or MP4 video, set the format parameter in the dynamic URL to webm or mp4. For example, the following high-quality, animated GIF was uploaded to Cloudinary, which assigned the image the kitten_fighting identifier:

Ruby:
Copy to clipboard
cl_image_tag("kitten_fighting.gif")
PHP v1:
Copy to clipboard
cl_image_tag("kitten_fighting.gif")
PHP v2:
Copy to clipboard
(new ImageTag('kitten_fighting.gif'));
Python:
Copy to clipboard
CloudinaryImage("kitten_fighting.gif").image()
Node.js:
Copy to clipboard
cloudinary.image("kitten_fighting.gif")
Java:
Copy to clipboard
cloudinary.url().imageTag("kitten_fighting.gif");
JS:
Copy to clipboard
cloudinary.imageTag('kitten_fighting.gif').toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("kitten_fighting.gif")
React:
Copy to clipboard
<Image publicId="kitten_fighting.gif" >

</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="kitten_fighting.gif" >

</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="kitten_fighting.gif" >

</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.BuildImageTag("kitten_fighting.gif")
Android:
Copy to clipboard
MediaManager.get().url().generate("kitten_fighting.gif");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().generate("kitten_fighting.gif")!, cloudinary: cloudinary)
https://res.cloudinary.com/demo/image/upload/kitten_fighting.gif

Weighing 6.3 MB, this GIF takes a long time to load. Setting the format (file extension) to mp4 dynamically converts the GIF to an MP4 video, after which Cloudinary caches it persistently and delivers it through a content delivery network (CDN):

The automatically generated MP4 video weighs 311 KB, only 5 percent of the original GIF, a considerable savings in bandwidth and load time.

Converting the same GIF to a WebM video involves setting the file extension to webm. Below is the video generated by Cloudinary, weighing only 467 KB.

A common practice of implementing animation with video is to display a single frame with a Play button, clicking which starts the actual video. Cloudinary can handle that implementation dynamically. The following image, converted to JPEG by Cloudinary and weighing only 5.6 KB, is a 150x100 thumbnail with a semitransparent overlay of a previously uploaded Play button:

Ruby:
Copy to clipboard
cl_image_tag("kitten_fighting.jpg", :transformation=>[
  {:width=>150, :height=>100, :gravity=>"north", :crop=>"fill"},
  {:overlay=>"play_button", :width=>0.4, :flags=>"relative", :opacity=>60}
  ])
PHP v1:
Copy to clipboard
cl_image_tag("kitten_fighting.jpg", array("transformation"=>array(
  array("width"=>150, "height"=>100, "gravity"=>"north", "crop"=>"fill"),
  array("overlay"=>"play_button", "width"=>"0.4", "flags"=>"relative", "opacity"=>60)
  )))
PHP v2:
Copy to clipboard
(new ImageTag('kitten_fighting.jpg'))
  ->resize(Resize::fill()->width(150)->height(100)->gravity(Gravity::compass(Compass::north())))
  ->overlay(
      Overlay::source(Source::image('play_button')
        ->transformation((new ImageTransformation())
          ->resize(Resize::scale()->width(0.4)->relative())
          ->adjust(Adjust::opacity(60))
  )));
Python:
Copy to clipboard
CloudinaryImage("kitten_fighting.jpg").image(transformation=[
  {'width': 150, 'height': 100, 'gravity': "north", 'crop': "fill"},
  {'overlay': "play_button", 'width': "0.4", 'flags': "relative", 'opacity': 60}
  ])
Node.js:
Copy to clipboard
cloudinary.image("kitten_fighting.jpg", {transformation: [
  {width: 150, height: 100, gravity: "north", crop: "fill"},
  {overlay: "play_button", width: "0.4", flags: "relative", opacity: 60}
  ]})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation()
  .width(150).height(100).gravity("north").crop("fill").chain()
  .overlay(new Layer().publicId("play_button")).width(0.4).flags("relative").opacity(60)).imageTag("kitten_fighting.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('kitten_fighting.jpg', {transformation: [
  {width: 150, height: 100, gravity: "north", crop: "fill"},
  {overlay: new cloudinary.Layer().publicId("play_button"), width: "0.4", flags: "relative", opacity: 60}
  ]}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("kitten_fighting.jpg", {transformation: [
  {width: 150, height: 100, gravity: "north", crop: "fill"},
  {overlay: new cloudinary.Layer().publicId("play_button"), width: "0.4", flags: "relative", opacity: 60}
  ]})
React:
Copy to clipboard
<Image publicId="kitten_fighting.jpg" >
  <Transformation width="150" height="100" gravity="north" crop="fill" />
  <Transformation overlay="play_button" width="0.4" flags="relative" opacity="60" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="kitten_fighting.jpg" >
  <cld-transformation width="150" height="100" gravity="north" crop="fill" />
  <cld-transformation :overlay="play_button" width="0.4" flags="relative" opacity="60" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="kitten_fighting.jpg" >
  <cl-transformation width="150" height="100" gravity="north" crop="fill">
  </cl-transformation>
  <cl-transformation overlay="play_button" width="0.4" flags="relative" opacity="60">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation()
  .Width(150).Height(100).Gravity("north").Crop("fill").Chain()
  .Overlay(new Layer().PublicId("play_button")).Width(0.4).Flags("relative").Opacity(60)).BuildImageTag("kitten_fighting.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation()
  .width(150).height(100).gravity("north").crop("fill").chain()
  .overlay(new Layer().publicId("play_button")).width(0.4).flags("relative").opacity(60)).generate("kitten_fighting.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation()
  .setWidth(150).setHeight(100).setGravity("north").setCrop("fill").chain()
  .setOverlay("play_button").setWidth(0.4).setFlags("relative").setOpacity(60)).generate("kitten_fighting.jpg")!, cloudinary: cloudinary)
https://res.cloudinary.com/demo/image/upload/w_150,h_100,c_fill,g_north/l_play_button,w_0.4,fl_relative,o_60/kitten_fighting.jpg

Resizing and Cropping of Animated GIFs and Conversion to Video

Your users might have uploaded to your site photos and animations of various dimensions and aspect ratios. Like most websites and mobile apps, you must resize and crop that content to match the site design, such as by creating a uniform square thumbnail for all user-uploaded animations.

Cloudinary can resize and crop animated GIFs in the cloud through dynamic transformation and delivery URLs. The following URL creates a 150x150, centered-cropped, and resized thumbnail of the original:

Ruby:
Copy to clipboard
cl_image_tag("kitten_fighting.gif", :width=>150, :height=>150, :crop=>"fill")
PHP v1:
Copy to clipboard
cl_image_tag("kitten_fighting.gif", array("width"=>150, "height"=>150, "crop"=>"fill"))
PHP v2:
Copy to clipboard
(new ImageTag('kitten_fighting.gif'))
  ->resize(Resize::fill()->width(150)->height(150));
Python:
Copy to clipboard
CloudinaryImage("kitten_fighting.gif").image(width=150, height=150, crop="fill")
Node.js:
Copy to clipboard
cloudinary.image("kitten_fighting.gif", {width: 150, height: 150, crop: "fill"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().width(150).height(150).crop("fill")).imageTag("kitten_fighting.gif");
JS:
Copy to clipboard
cloudinary.imageTag('kitten_fighting.gif', {width: 150, height: 150, crop: "fill"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("kitten_fighting.gif", {width: 150, height: 150, crop: "fill"})
React:
Copy to clipboard
<Image publicId="kitten_fighting.gif" >
  <Transformation width="150" height="150" crop="fill" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="kitten_fighting.gif" >
  <cld-transformation width="150" height="150" crop="fill" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="kitten_fighting.gif" >
  <cl-transformation width="150" height="150" crop="fill">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(150).Height(150).Crop("fill")).BuildImageTag("kitten_fighting.gif")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().width(150).height(150).crop("fill")).generate("kitten_fighting.gif");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setWidth(150).setHeight(150).setCrop("fill")).generate("kitten_fighting.gif")!, cloudinary: cloudinary)
https://res.cloudinary.com/demo/image/upload/w_150,h_150,c_fill/kitten_fighting.gif

Though optimized, the above high-quality animated GIF still weighs 2.5 MB, which seems an overkill for such a small image. Converting animated GIFs to WebM or MP4 videos results in much smaller files while maintaining the same visual quality. With the file extension set to mp4, the following URL dynamically creates the same 150x150, cropped version of the animation while converting it to the MP4 video format. The generated MP4 looks great, weighing only 106 KB, a size reduction of 96 percent.

HTML5

Not all browsers support all video formats. Chrome supports both MP4 and WebM; Firefox, only WebM; and Internet Explorer, only MP4. However, the HTML5 <video> tag, with which you can specify multiple source types for the same video, works in most modern browsers. That means you can specify both the dynamic Cloudinary URL, which converts the animation to MP4; and the alternative URL, which converts the animation to WebM. Afterwards, browsers would play the video in the format that they support.

The following HTML snippet creates a 320x180 auto-play <video> tag, specifying the MP4 and WebM URLs in addition to a still-frame JPEG poster. Also included is alternative content that links to the original GIF.

Copy to clipboard
<video width="320" height="180" autoplay loop muted="muted" 
  poster="https://res.cloudinary.com/demo/image/upload/kitten_fighting.jpg">

  <source type="video/mp4"
      src="https://res.cloudinary.com/demo/image/upload/kitten_fighting.mp4">

  <source type="video/webm"
      src="https://res.cloudinary.com/demo/image/upload/kitten_fighting.webm">

  Your browser does not support HTML5 video tag. 

  <a href="https://res.cloudinary.com/demo/image/upload/kitten_fighting.gif">Click here to view original GIF</a> 

</video>

Summary

Resizing and cropping animated GIFs, a significant part of online media, takes substantial bandwidth and server resources. Instead, convert uploaded animated GIFs to videos, which offer the same visual quality for considerably less bandwidth, faster loading, and an enhanced user experience.

Cloudinary handles that conversion for you through automation in the cloud in three steps:

  1. Convert animated GIFs to MP4s and WebMs through dynamic URLs.
  2. Resize, crop, and transform the videos generated in step 1.
  3. Optimize the transformed videos through a fast CDN.

Note
The animated WebP format, an alternative to animated GIF, also works on Chrome. Cloudinary supports automated conversion to WebP, serving animated WebPs to browsers that support that format and other formats to other browsers, as appropriate. For more details, see our blog post on animated WebPs.

Conversion of animated GIFs to MP4s and WebMs, as well as all other transformation capabilities, are available in all Cloudinary plans, including the free tier. We welcome your feedback.


Further Reading on Video Transformation

Recent Blog Posts

Create Lightweight Sites With Low-Code and No-Code Technology

Consumers expect modern websites to be mainly visual. But, the more compelling and complex the related media is, the more data is involved, compounding the site’s weight. In today’s content-craving world, delivering unoptimized media can cost you because it leads to sluggish page loads, resulting in visitors abandoning your site in search of a faster alternative. In fact, a page load that takes more than three seconds can cause as many as 40% of your visitors to bounce. Given this competitive, digital-first environment, you can’t afford to lose page views, for time is of the essence.

Read more
A Blueprint for AWS-Secured Webhook Listeners for Cloudinary

tl;dr: An AWS-secured and optimized Cloudinary webhook listener for extending the Cloudinary service

Code: Github

A webhook is a communication medium for sending notifications from one platform to another about events that occurred. In place are user-defined HTTP callbacks that are triggered by specific events. When a triggered event takes place on the source site, the webhook listens to the event, collects the data, and sends it to the URL you specified in the form of an HTTP request.

Read more
New Accessibility Features for Cloudinary’s Product Gallery Widget

Cloudinary’s Product Gallery widget, which launched in 2019, has enabled many brands to effectively and efficiently showcase their products in a sleek and captivating manner, saving countless hours of development time and accelerating release cycles. By adding Cloudinary’s Product Gallery widget with its customizable UI to their product page, retailers reap numerous benefits, often turning visitors into customers in short order.

Read more
Why Successful Businesses Engage With and Convert Audiences With Visual Media

Most business buyers prefer to research purchase options online, as do many shoppers. No wonder online retail sales in the U.S. rose by 32.4% in 2020—an impressive gain of $105 billion.

For B2B and B2C businesses, text-heavy websites are no longer adequate in attracting shoppers. Instead, engaging visual media—spin images, videos, 3D models, augmented reality—are becoming a must for conveying eye-catching details and differentiators about products or services.

Read more
Making User-Generated Content (UGC) Shoppable With Cloudinary

User-generated content (UGC) is a powerful marketing tool. Not only does video complement marketing efforts for e-commerce by enabling customers to explore products in greater detail, but UGC also adds an element of trust. As a bonus, user-generated video is an exceptional opportunity for e-businesses to attract website traffic without their marketing team having to create promotional videos from scratch. User-generated content drives conversions and brand loyalty as a direct result of authentic interaction.

Read more