Ch. 20 · Optimizing Web Images and Animations

한국어日本語
8 min

Short Videos Instead of GIFs: MP4, WebM, and Animated Images

For moving images, short MP4/WebM videos are known to come out much smaller than GIFs. Use autoplay loop muted playsinline to make them behave like a GIF, and take care of accessibility and fallbacks.

GIF is a format from 1987 that uses a palette of up to 256 colors per frame and a lossless method (LZW). It compresses very inefficiently for photo-like images that keep moving. Video codecs, on the other hand, remove the redundancy between consecutive frames with the inter-frame prediction we've been covering since Chapter 3 of this course. That's why converting short demos and reaction clips from GIF to short MP4 or WebM is commonly recommended in web performance guides. This site has not measured the size of GIFs versus video, so we don't give a figure like 'X times smaller'. Convert the same animation from your own file and check it directly.

Converting GIF → MP4 / WebM

GIF to a highly compatible H.264 MP4 (check the CRF value with your own file)
ffmpeg -i demo.gif \
  -movflags +faststart -pix_fmt yuv420p \
  -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" \
  -c:v libx264 -crf 23 -an demo.mp4
  • scale=trunc(iw/2)*2:trunc(ih/2)*2**: because of chroma subsampling, yuv420p needs even width and height. If the GIF's dimensions are odd, this filter rounds them to even.
  • `-pix_fmt yuv420p`: sets a widely playable pixel format (web embed recipe).
  • `-movflags +faststart`: makes playback start sooner (faststart).
  • `-an`: a GIF has no sound, so no audio is created.
The same GIF to VP9 WebM. You have to use -b:v 0 together with -crf to get CRF mode
ffmpeg -i demo.gif -c:v libvpx-vp9 -crf 33 -b:v 0 -an demo.webm

Markup that makes it behave like a GIF

Autoplay, loop, muted, inline playback
<video autoplay loop muted playsinline width="480" height="270"
       poster="demo-poster.jpg" aria-label="Demo of using the settings screen">
  <source src="demo.webm" type="video/webm">
  <source src="demo.mp4" type="video/mp4">
</video>
  • `muted` is effectively a condition for autoplay. Browsers tend to restrict autoplay with sound, so using a silent video for autoplay is the norm.
  • `playsinline` is an attribute meant to make the video play inside the page instead of opening full screen on some mobile browsers.
  • `poster` is the image shown before the video loads. It reduces the empty space while loading and can also serve as the first-screen image.
  • Putting WebM on top and MP4 below is just an example order. Compare which is smaller using your own file, and always keep the most widely playable H.264 MP4 (codec support checker).
  • Accessibility: a video that keeps moving on its own can be distracting for some users, so avoid long repeating loops and consider a way to pause. For a short demo, it's better to add controls so users can stop it.

So what about animated WebP and AVIF?

WebP and AVIF can also hold animation (Chapter 4), which has the advantage that you can use a single <img>. However, a video file gets <video> features like seeking and pausing and the benefit of hardware decoding (hardware decoding), while an animated image is simple with just one <img>. Which is smaller and smoother depends on the content, so make GIF, WebP, and MP4 all from the same source and compare file size and playback directly to decide.

Making an animated WebP with ffmpeg (requires a libwebp build)
ffmpeg -i demo.mp4 -vf "fps=15,scale=480:-2" -c:v libwebp -quality 70 -loop 0 -an demo-anim.webp
For screen recordings and UI demos, check the color and the text

Screen recordings with lots of text and lines can show noticeable ringing and color bleeding even in a video codec. This site's measured clips include no screen recordings (limits of the encoding recipes chapter), so we can't say which CRF is appropriate; after converting, check by eye that the text isn't smeared.

GIFGIF

An image format from 1987. It uses a palette of up to 256 colors and lossless compression, and supports animation.

playsinlineplaysinline

A video tag attribute. On some mobile browsers, it makes the video play inside the page instead of full screen.

PosterPoster

The image shown before a video plays.

Check yourself

Why do you make the width and height even when converting a GIF to MP4 (H.264, yuv420p)?

Check yourself

Which is closest to the combination of attributes a <video> needs to autoplay and loop like a GIF?