Home/Blog/Improve animated GIF performance with HTML5 video

Improve animated GIF performance with HTML5 video

Itย isย common among modern websites and applications toย use videos instead ofย GIFs. The trend has been prominent inย social networks and other content- orย message-driven apps. Letโ€™s take one ofย the most popular GIF-hosting websites, Giphy. Ifย weย goย toย DevTools and check the GIF animation source file URL atย first glance, weย can see anย actual video file without sound and playback controls.

A screenshot showing the *.mp4 link to the โ€œGIFโ€ file on GIPHY

But why? Inย this article, weโ€™ll review why video isย usually aย better choice than GIF and how toย convert GIF files into videos toย put onto web pages.

Media onย the Web: performance expectations

Toย provide optimal performance, any visual medium onย websites orย any other client-server application should balance visual quality and file size (i.e., compression). Itย isย even more important for users onย limited mobile data plans orย accessing web resources onย slower public WiFi orย 3Gย connections.

Inย addition, images, videos, orย GIFs that play presentational roles usually occupy aย significant amount ofย the viewport space, and aย particular visual may beย the largest element within the page viewport. Ifย itย takes too long toย load, itย may decrease the page performance byย increasing Largest Contentful Paint, one ofย the Core Web Vitals metrics.

Worth noting that images and videos are not render-blocking, but loading aย heavy asset still may impact the page performance, narrowing the bandwidth and potentially prolonging the loading ofย other page content.

Why isย video more suitable than GIF?

From myย perception, since the early 00s, there has been aย general feeling that video isย usually aย heavier asset than GIF (especially since GIF isย anย image format). Onย top ofย that, GIFs have been around since the first stages ofย the Web and have proven toย work, soย why use video instead ofย aย GIF?

Color resolution/depth

Leaving some hacks aside, GIF isย anย 8-bit format, meaning itย can encode upย toย 256ย colors. This results inย aย noticeable quality loss, especially with gradient-like color fades. Onย the other hand, with video, you can preserve many more colors and details, depending onย the codec, bitrate, and compression.

File size

GIFs tend toย have larger file sizes than modern formats because they use aย lossless compression method, which preserves every frame asย aย full image, resulting inย redundancy and larger files. Inย comparison, modern formats employ more efficient lossy compression techniques and video codecs that can achieve higher quality atย smaller file sizes.

Browser support

Since the introduction ofย HTML5, <video> tag has been aย standardized way toย use videos onย the Web. Back inย the day, this was achieved using things like Flash Player, which required dedicated software installed onto the clientโ€™s machine.

Bonusย โ€” LCP side-effect

Using <video> tag without a poster attribute wonโ€™t make the video file aย candidate for increasing LCP, whereas <img> elements are always considered when calculating LCP.

How toย replace aย GIF animation with aย video?

Knowing the advantages ofย using video instead ofย GIF, letโ€™s see how toย swap them. Inย aย nutshell, itย comes down toย aย few different steps based onย your input material.

Before weย start

Rendering your animation directly into video formats isย always better than converting aย GIF into aย video. This way, you skip the conversion process ifย the tool you used toย create anย animation gives enough flexibility toย adjust video parameters.

Web-friendly formats and codecs

Not any video format works for the Web. Here isย the list ofย ones supported byย modern browsers:

  • MP4 video encoded with H.264ย codes: widely supported video format well-suited for the Web. Itย offers good balance options between quality and file size.
  • WebM with VP8/VP9ย codec: relatively new format compared toย MP4/H.264. Itย provides better compression ratios.

Visit MDN toย learn more about web-video codecs.

Based on caniuse.com, most modern browsers support the WebMย format. Iย usually convert into both WebM and MP4. Ifย WebM offers better file size than MP4, preserving video quality, Iย include both file types onto aย page for aย browser toย choose, prioritizing WebM. Otherwise, itย isย enough toย have only MP4.

There are two ways ofย converting GIFs toย video: manually and using third-party services like Uploadcare. Inย this article, weโ€™re going toย overview them both.

Sample GIF

One ofย the popular cases ofย using animations onย the website isย schematically showcasing processes, for example, data flow inย SaaS tools. The closest representativeย I could findย on Wikimedia with aย Creative Commons license isย this:

Note from Blog Editors: The thing you see above isย aย video. Weย converted the GIF toย the video toย make sure you wonโ€™t download excessive data.

Itย isย close enough toย aย real-world scenario for the following reasons:

  1. Itย looks like aย generated GIF rather than aย recorded video.
  2. Some areas ofย high contrast and sharp edges need toย beย preserved.
  3. Relatively big file size for aย given color set, 1.18MB.

Converting GIF into web-ready video with FFmpeg

FFmpeg, aย free and open-source CLIย tool, isย used toย power some online tools.

Installing FFmpeg

Download the FFmpeg version based onย your operating system from the official website. This isย aย command-line tool, soย general experience with CLIs isย required. Then, put the FFmpeg executable and the test GIF into the same folder. Iโ€™m running Windows, soย inย myย case, itย isย ffmpeg.exe and sample.gif. When opening the folder inย the command line (Iย use Git Bash), here isย how itย looks:

Arthur@DESKTOP-RI3G960 MINGW64 ~/Desktop/Work/Uploadcare
$ ls
ffmpeg.exe* sample.gif

Converting GIF into WebM manually with FFmpeg

Letโ€™s start with the following command:

./ffmpeg.exe -i sample.gif  -c vp9 -b:v 0 -crf 32 sample.webm

Hereโ€™s aย breakdown ofย the options:

  • -i specifies the input file.
  • -c specifies aย particular WebM codec toย use.
  • -b:v isย aย target average bitrate. Intentionally zero toย provide anย ability toย set constant quality.
  • -crf isย the Constant Rate Factor. Inย simple terms, this sets the quality ofย the resulting video. Lower values give better quality, but the size also gets bigger. 32ย is the default value (for VP9), but Iโ€™m setting itย here explicitly for better representation.

The output file named sample.webm will appear inย the folder. Toย check the quality inย aย situation that isย closer toย aย real-life scenario, Iย took the 50th frame from each ofย the files using the commands below:

./ffmpeg -i sample.webm -vf "select=eq(n\,49)" samplewebm.png
./ffmpeg -i sample.gif -vf "select=eq(n\,49)" samplegif.png

Here are the results:

The 50th frame ofย sample.gif, original GIFThe 50th frame ofย sample.gif, original GIF
The 50th frame ofย sample.webm, converted fileThe 50th frame ofย sample.webm, converted file

Now, toย the interesting part. The resulting file isย only 127ย KB, andย I believe itย could beย even lower with aย bigger amount ofย compression, but this isย already almost 10x size optimization.

Converting GIF into MP4 manually with FFmpeg

Weโ€™re going toย use the following command:

ffmpeg -i sample.gif -movflags +faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -crf 23 sample.mp4

The command looks complicated, soย letโ€™s goย through the parameters:

  • sample.mp4 isย anย output file name, now inย MP4ย format.
  • -movflags +faststart allows the video toย start playing even ifย itย hasnโ€™t been completely loaded, which isย typical behavior for the Web.
  • -pix_fmt yuv420p specifies the pixel format that isย supported onย most browsers (contrary toย yuv444). Itย relates toย how aย pixel color isย broken into components and encoded. Followย my previous article toย discover more.
  • -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" makesย us sure that the videoโ€™s height isย divisible byย 2, which isย aย prerequisite for yuv420 toย work correctly.
  • -crf 23 sets CRF for the MP4ย files, usually isย aย default FFmpeg parameter. Iย duplicated the default value toย beย more verbose for the sake ofย example.

The file size ofย the resulting MP4 video turned out toย beย 113ย KB, and the extracted 50th frame ofย the video looks like this:

The 50th frame ofย sample.mp4, converted fileThe 50th frame ofย sample.mp4, converted file

Conversion analysis

Both formats offer lossy compression, meaning some initial data gets lost. Inย our default-setting conditions, WebM has slightly outperformed MP4 regarding video quality atย the expense ofย aย few extra kilobytes.

Embedding converted videos onto aย website

Ifย youโ€™ve built your website using aย CMS, adding aย video isย asย simple asย uploading the file through your WYSIWYG editor. Ifย you integrate videos manually, use the following HTML code instead ofย an <img> tag for GIF files:

<video autoplay loop muted playsinline>
  <source
    src="path/to/webm/video"
    type="video/webm"
  />
  <source
    src="path/to/mp4/video"
    type="video/mp4"
  />
  Your browser doesn't support HTML5 video tag.
</video>

Letโ€™s break this down:

  • autoplay allows video toย beย played automatically, just asย GIFs do.
  • muted turns off audio onย the sidenote. Safari wonโ€™t autoplay video that isย not muted.
  • loop allows video toย loop automatically.
  • playsinline allows video play right where itย isย onย the page without aย fullscreen player.

This mix ofย 4ย attributes makes the video behave like aย GIF but with superior performance.

Aย browser will first try loading the nearest available source, which isย WebM video inย our case, and ifย itย fails toย doย so, itย will goย toย the next item.

Converting GIF into video with Uploadcare

Uploadcare has gif2video transformation, which allows you toย solve the problem with several lines ofย code and forget about itย forever.

Once youโ€™ve signed up toย Uploadcare and created aย project, upload the file into the Files menu. After that, youโ€™ll see file details and aย GIF preview onย the right. You may notice aย unique string generated right under the file name onย the right, which isย UUID and isย used toย refer toย assets inย Uploadcare.

Uploadcare Dashboard displaying file information and UUID

Toย convert the uploaded GIF into aย video, youโ€™ll need toย access the resource via aย URL ofย the following format:

https://{subdomain}.ucarecd.net/:uuid/gif2video/-/format/:format-type/:filename

Where:

  • {subdomain} is a unique generated CDN subdomain based on your project.
  • :uuid isย the unique resource identifier (also displayed inย the file info box onย the right side ofย the previous screenshot).
  • :format-type isย anย output file format, either WebMย or MP4.
  • :filename isย the name ofย the output file. Inย our case, letโ€™s use sample.mp4 or sample.webm.

Accessing the result URI will bring you aย plain video ready toย beย embedded onto the page, just like weย did inย the manual example.

Letโ€™s convert the GIF into WebM and MP4. Hereโ€™s how the URL for WebM looks like:

https://6ca2u7ybx5.ucarecd.net/49c9077f-633c-4f11-a339-ce75212f4f10/gif2video/-/format/webm/sample.webm

And here isย the URL for MP4 version ofย the same GIF:

https://6ca2u7ybx5.ucarecd.net/49c9077f-633c-4f11-a339-ce75212f4f10/gif2video/-/format/mp4/sample.mp4

The WebM version weighs 102ย KB, and the MP4 version occupies 112ย KB ofย disc space.

Sizes ofย video files produced byย Uploadcare when converting from GIF

Letโ€™s also run our 50th frame test toย see the difference inย results produced using default Uploadcareโ€™s settings:

The 50th frame ofย the WebM video produced byย UploadcareThe 50th frame ofย the WebM video produced byย Uploadcare
The 50th frame ofย the MP4 video produced byย UploadcareThe 50th frame ofย the MP4 video produced byย Uploadcare

With the URI transformation approach that Uploadcare uses, itย isย possible toย place converted videos onto aย web page without downloading the files and storing them onย your side. Letโ€™s use our HTML <video> snippet mentioned before, but replace the links toย the WebM and MP4 files with Uploadcare links:

<video autoplay loop muted playsinline>
  <source
    src="https://6ca2u7ybx5.ucarecd.net/49c9077f-633c-4f11-a339-ce75212f4f10/gif2video/-/format/webm/sample.webm"
    type="video/webm"
  />
  <source
    src="https://6ca2u7ybx5.ucarecd.net/49c9077f-633c-4f11-a339-ce75212f4f10/gif2video/-/format/mp4/sample.mp4"
    type="video/mp4"
  />
  Your browser doesn't support HTML5 video tag.
</video>

Summing up

Tech has evolved above and beyond what itย used toย look like back when GIF was the main format, and some things that were considered unimaginable without extra tooling, like Flash Player, are now part ofย everyday standards. Onย aย footnote, letโ€™s compare the approaches weย discussed today.

Converting manually using tools such asย FFmpeg

Pros:

  • Freedom inย controlling different aspects ofย conversion.
  • Usually free.

Cons:

  • Sometimes takes trials and errors toย find suitable settings.
  • Poorly-scalable, may require custom automation for larger amount ofย assets.
  • Need toย run somewhere even ifย itย isย your own machine.
  • Requires storage.

Converting automatically using third-party services like Uploadcare

Pros:

  • Convenient URL-based interface.
  • Conversion arguments are grouped into more human-readable parameters.
  • Managed storage and content delivery.

Cons:

  • Third-party service dependency.
  • Usually paid toย get more features.

Iย encourage you toย use this guide asย aย starting point for your image and video optimizations, orย feel free toย use Uploadcareโ€™s solution that will handle all the heavy lifting mentioned inย the FFmpeg part for you.

Ready to get started?

Join developers who use Uploadcare to build fileย handling quickly andย reliably.

Sign up for free