Add Subtitles to a Web Video With the HTML track Element and WebVTT
A working HTML example for subtitles and captions on a video element: track attributes, kinds, languages, CORS and the default track.
Try it now: AI Subtitle Generator — timed subtitles from speech in 20 languages, processed in your browser.
The HTML <video> element can display subtitles and captions natively, with no JavaScript, by pairing it with one or more <track> elements that point to WebVTT files.
A minimal working example
<video controls width="640" src="/media/lesson.mp4">
<track
default
kind="captions"
srclang="en"
label="English"
src="/media/lesson.en.vtt">
</video>And the file lesson.en.vtt:
WEBVTT 00:00:01.000 --> 00:00:03.500 Welcome to the lesson. 00:00:04.000 --> 00:00:06.500 Today we cover subtitles.
Rules for the file come from the WebVTT specification: it is UTF-8, starts with WEBVTT, and timestamps use mm:ss.ttt or hh:mm:ss.ttt with a full stop before the milliseconds. If your file is an SRT, convert it first; see SRT vs WebVTT or the code in converting SRT to WebVTT in JavaScript.
The track attributes
| Attribute | What it does |
|---|---|
src | URL of the .vtt file. Required. |
kind | subtitles, captions, descriptions, chapters or metadata. Default is subtitles. |
srclang | Language of the text as a BCP 47 tag, e.g. en, si, ta. Required when kind="subtitles". |
label | The name shown in the browser's track menu. |
default | Turns this track on unless the user's preferences choose another. Use on one track only. |
subtitles or captions?
MDN's definitions: subtitles transcribe or translate dialogue for viewers who can hear but may not understand; captions also include sound effects and music cues, for viewers who cannot hear. A track of speech-only text from a generator is closer to subtitles; add sound descriptions before calling it captions. See the terminology guide.
Several languages
<video controls src="/media/lesson.mp4"> <track kind="subtitles" srclang="en" label="English" src="lesson.en.vtt" default> <track kind="subtitles" srclang="si" label="සිංහල" src="lesson.si.vtt"> <track kind="subtitles" srclang="ta" label="தமிழ்" src="lesson.ta.vtt"> </video>
MDN notes that a media element cannot have more than one track with the same kind, srclang and label.
Same-origin and CORS
The src must be same-origin unless the <video> has a crossorigin attribute. If your VTT is on a CDN, add crossorigin="anonymous" to the video and serve the VTT with a proper CORS header; otherwise the track silently fails. Serve the file as text/vtt.
Reading cues in JavaScript
const track = document.querySelector("track").track;
track.addEventListener("cuechange", () => {
const cue = track.activeCues[0];
console.log(cue ? cue.text : "(no caption)");
});cuechange fires whenever the displayed cue changes.
Troubleshooting
| Symptom | Check |
|---|---|
| Nothing appears | Is the track enabled in the video's menu? Is it default? |
| Works locally, not on the server | CORS and text/vtt MIME type |
| Wrong characters | The file must be UTF-8 |
| Whole file ignored | The first line must be exactly WEBVTT |
Where the VTT comes from
The subtitle generator exports Web subtitles (.vtt) from the same captions as its SRT export, after a free Google sign-in. For non-web destinations, see soft vs burned-in captions.
Sources
Last updated