Sub topic 9
Adding captions and subtitles with track
This chapter assumes you can write a video element with controls and a source list, and that you know how HTML attributes work. Here you add timed text to a video with the track element and a WebVTT file.
Captions versus subtitles
Subtitles translate or transcribe speech for viewers who can hear the audio. Captions also include non-speech sound, such as "[door slams]" or "[music]", for viewers who cannot hear it. The track element covers both. You choose with the kind attribute.
| `kind` value | Purpose |
|---|---|
| `subtitles` | Translation or transcription of speech (the default) |
| `captions` | Speech plus important sounds |
| `descriptions` | Text describing the visuals, for screen readers |
| `chapters` | Chapter titles for navigation |
| `metadata` | Data for scripts. The browser does not display it |
Writing a WebVTT file
A track points to a WebVTT file. The file starts with the line WEBVTT. After a blank line come cues, each with a start time, an arrow, an end time, and the text.
WEBVTT
00:00:00.000 --> 00:00:03.500
Welcome to the tutorial.
00:00:03.500 --> 00:00:07.000
First, we will add a video element.Times use hh:mm:ss.mmm, with a period before the milliseconds. A comma, as in SRT files, is a common error that breaks the cue.
Attaching tracks to a video
Place track elements inside the video, after any source elements.
<video controls width="640" poster="poster.jpg" preload="metadata">
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
<track kind="captions" src="clip.en.vtt" srclang="en" label="English" default>
<track kind="subtitles" src="clip.es.vtt" srclang="es" label="Español">
<p>Your browser cannot play this video. <a href="clip.mp4">Download it instead</a>.</p>
</video>srcis the URL of the WebVTT file.srclangis the language code. It is required whenkindissubtitles.labelis the name shown in the player's captions menu.defaultturns the track on unless the viewer's settings choose another one. Use it on at most one track per kind.
Controlling tracks from JavaScript
Each track maps to a TextTrack object in video.textTracks. Its mode is disabled, hidden, or showing.
const video = document.querySelector('video');
for (const track of video.textTracks) {
track.mode = track.language === 'es' ? 'showing' : 'disabled';
}The hidden mode loads the cues and fires cue events but draws nothing. That is useful for custom captions.
Common errors
- Nothing appears. Open the network panel and check that the
.vttfile loads. A wrong path or a missingWEBVTTheader are the usual causes. - Wrong MIME type. Serve WebVTT as
text/vtt. - Cross-origin files. A track from another origin is fetched with CORS. Add
crossoriginto thevideoand send anAccess-Control-Allow-Originheader from the server hosting the.vttfile. - Local files. Opening the page from
file://often blocks the track. Use a local web server.
Exercise
A video has a Spanish subtitles track, but the captions menu shows no language name and the track never loads. The .vtt file begins with a blank line followed by 00:00:01,000 --> 00:00:03,000. Name two problems and their fixes.
Show answer
First, the file lacks the WEBVTT header line, so add it as the first line. Second, the timestamp uses a comma before the milliseconds, so change it to 00:00:01.000 --> 00:00:03.000. The missing menu name is fixed by adding label="Español", and a subtitles track also needs srclang="es".