Lesson 44 of 51 · Module 7, lesson 4 of 11 · 5 min

Video with captions

Add video with captions so that everyone can follow it, and run your folder as a local website with Live Server.

What you'll learn

  • How to add a video with <video>
  • How to add captions with <track>, and why every video needs them
  • How to run your folder as a small local website with Live Server

Why this matters

Video explains some things better than any text. But many people watch without sound — on a bus, at work, or because they are deaf or hard of hearing. Captions (the spoken words as text on the video) make your video useful to all of them, and they help people who are still learning the language.

Get the sample files

Create a folder media in my-website. Save these two files into it (right-click each link → Save link as…):

A video with captions

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>How to make tomato soup</title>
</head>
<body>
  <h1>How to make tomato soup</h1>
  <video controls width="320" height="180">
    <source src="media/soup-steps.mp4" type="video/mp4">
    <track src="media/soup-steps.vtt" kind="captions" srclang="en" label="English" default>
    <p>Your browser cannot play this video. <a href="media/soup-steps.mp4">Download it</a>.</p>
  </video>
</body>
</html>

What you'll see: a video player with play, volume and full-screen buttons. Press play: the narrator describes three steps, and the words appear at the bottom of the video.

The page "How to make tomato soup" with a video player showing three red tomatoes and the player controls: play, the time 0:00 / 0:09, volume and full screen.

PartJob
controlsShows play, pause, volume and full-screen buttons. Always include it.
width, heightThe player's size, like an image's.
<source>The video file and its type. You can list several; the browser uses the first it can play.
<track kind="captions">The captions file. srclang is its language, label is the name in the menu, default turns it on.
The paragraph insideShown only if the browser cannot play video at all.

The captions file

Captions are a plain text file in the WebVTT format (.vtt). Open soup-steps.vtt in VS Code:

WEBVTT

1
00:00:00.000 --> 00:00:02.240
Step one: cut the tomatoes.

2
00:00:02.540 --> 00:00:06.010
Step two: cook them with a little water for twenty minutes.

Each block has a start time, an arrow -->, an end time, and the text shown during that time.

Captions need a small web server

Open your page by double-clicking it, and the video plays — but the captions do not appear. For safety, browsers do not let a page opened from your disk load a captions file. On a real website they work. You can run a real website on your own computer with a free VS Code extension:

  1. In VS Code, click the Extensions icon on the left (four small squares), or press Ctrl + Shift + X (Windows) / Command + Shift + X (macOS).
  2. Search for Live Server (by Ritwick Dey) and click Install.
  3. Open index.html (or your video page), right-click inside the code, and choose Open with Live Server. Or click Go Live in the blue status bar at the bottom.
  4. Your browser opens an address like http://127.0.0.1:5500/video.html. This is your folder, served as a website that only you can see.

Now the captions appear. A bonus: Live Server refreshes the browser for you every time you save. Many developers use it all day. Keep using it from now on.

Formats and autoplay

  • MP4 (with the common H.264 video format) plays in every current browser. WebM is a good second <source> if you have it.
  • autoplay starts the video by itself — but browsers block it unless the video is also muted, and a moving picture that starts on its own distracts many people. Let people press play.

Captions, subtitles and transcripts

  • Captions show the spoken words and important sounds, in the same language.
  • Subtitles are a translation: kind="subtitles" with its own srclang.
  • A transcript is the whole text below the video. It helps people who cannot watch at all, and search engines. For short videos, add one.

Try it yourself

  1. Select the example and choose Try it. Play the video in the playground: the captions work there.
  2. Build the page in your own folder, open it by double-clicking, and see that the captions are missing. Then open it with Live Server: they appear.
  3. Edit a line of text in soup-steps.vtt, save, and play the video again.

Common mistakes

  • No controls. Visitors cannot start or stop the video. Fix: always add it.
  • No captions. Many people cannot follow. Fix: add a <track> — or at least a transcript.
  • Captions missing when you open the file from disk. That is the browser's safety rule. Fix: use Live Server.
  • Autoplay with sound. Browsers block it, and people dislike it. Fix: let people press play.

Check yourself

  1. Which attribute shows the play and pause buttons?
  2. Which element adds captions, and what format is the captions file?
  3. Why do captions not appear when you double-click the page file?

Answers

  1. controls.
  2. <track kind="captions">, with a WebVTT (.vtt) file.
  3. Browsers do not let a page opened from disk load the captions file. Use Live Server (or a real website).

Next: audio works almost the same way — with one important difference for accessibility.

Chat with us on WhatsApp