Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Images, Fonts and Media

Video and Audio Performance

Keep pages fast with video and audio: choose codecs and containers, use poster images, preload and autoplay correctly, avoid GIFs, lazy-load embeds, and stream adaptively.

Video is the heaviest content a web page can carry. A few seconds of poorly encoded footage can outweigh the rest of a page combined, and an autoplaying hero video can ruin LCP and mobile data plans. With some care, though, video and audio can be both rich and fast.

This lesson covers choosing formats, controlling what the browser downloads, and embedding media without wrecking Core Web Vitals.

Codecs and containers

A video file has a container (MP4, WebM) holding streams encoded with codecs (H.264, VP9, AV1 for video; AAC, Opus for audio).

Codec Compression Support Notes
H.264 (AVC) Good Universal The safe fallback in MP4
VP9 Better Wide Usually in WebM
AV1 Best Growing; hardware decode varies Smallest files, slow to encode
AAC / MP3 Audio, good Universal Fallback audio
Opus Audio, excellent Wide Best quality per byte, especially speech

List sources from best to most compatible and let the browser use the first it understands:

HTML
<video controls width="640" height="360" poster="/img/intro-poster.avif" preload="metadata">
  <source src="/video/intro.av1.mp4" type='video/mp4; codecs="av01.0.05M.08"'>
  <source src="/video/intro.webm" type='video/webm; codecs="vp9, opus"'>
  <source src="/video/intro.mp4" type='video/mp4; codecs="avc1.64001E, mp4a.40.2"'>
  Your browser does not support video.
</video>

Control what is downloaded

The preload attribute is a hint:

Value Behaviour
none Download nothing until the user plays
metadata Fetch duration, dimensions and the first frame info only
auto Let the browser download as much as it wants

For videos that are not the main content, use preload="none" or "metadata", plus a poster image so the area shows something meaningful instead of black. Give the video width and height (or CSS aspect-ratio) so it reserves space, avoiding layout shift.

Check preload and poster settings

Nothing has downloaded, yet the player shows a poster at the right size. The user pays for bytes only if they press play.

Replace GIFs

A looping animation as a GIF can be 5 to 20 times larger than the same clip as a video. Use:

HTML
<video autoplay loop muted playsinline width="400" height="300">
  <source src="/video/demo.webm" type="video/webm">
  <source src="/video/demo.mp4" type="video/mp4">
</video>

Browsers only autoplay video that is muted (and playsinline on iOS). Because it is decorative, hide it from assistive technology or provide a pause control.

Autoplay hero videos

A full-width hero video competes with the LCP image for bandwidth. If you need one:

  • Keep it short and loop it.
  • Compress hard: aim for a few hundred kilobytes to a couple of megabytes at most.
  • Show a poster immediately and make the poster the LCP element.
  • Do not load it on slow or data-saving connections:
JS Skip heavy media on slow connections

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The Network Information API (navigator.connection) is only supported in some browsers, so always code a safe default.

Adaptive streaming

For long videos, a single big file is wasteful. Adaptive bitrate streaming (HLS or MPEG-DASH) splits the video into short segments at several quality levels. The player switches between them as bandwidth changes and fetches only the portion being watched. Services such as Mux, Cloudflare Stream or Bunny Stream, and CDNs, can produce these renditions for you. Playing HLS outside Safari typically uses a library such as hls.js.

flowchart LR
  A["Master video"] --> B["Encode: 240p / 480p / 720p / 1080p"]
  B --> C["Segment into short chunks"]
  C --> D["Player picks quality per chunk"]

The player moves up or down in quality as the network changes.

Embeds: YouTube, maps and others

An embedded player can load 500 KB or more of JavaScript before anyone presses play. Use the facade pattern: show a thumbnail with a play button, and load the real iframe on click. At minimum, lazy-load it:

HTML
<iframe src="https://example.com/embed/xyz" loading="lazy"
        width="560" height="315" title="Product tour"></iframe>
A click-to-load facade

Audio

  • Offer Opus (in WebM or Ogg) with an AAC or MP3 fallback.
  • Use preload="none" or metadata for podcast episodes; do not download 40 MB speculatively.
  • Lower the bitrate for speech (64 kbps Opus is often enough).
  • Never autoplay audio; browsers block it and users dislike it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which preload value downloads nothing until the user presses play?

    preload=none postpones all media downloads until playback is requested.
  2. 2. Why replace animated GIFs with video?

    Video codecs compress motion much better than GIF.
  3. 3. What does a poster attribute provide?

    A poster fills the player with a meaningful image without downloading the video.
  4. 4. What is a facade for an embed?

    It avoids loading heavy third-party player code until the user shows interest.

Summary

  • Offer modern codecs first with H.264 or AAC fallbacks; compress hard.
  • Use preload="none" or "metadata", a poster and reserved dimensions.
  • Replace GIFs with muted looping video; keep hero videos tiny and skip them on slow connections.
  • Use adaptive streaming for long video and facades or lazy loading for embeds.

Next: The Main Thread and Long Tasks