Get Started with HTML Video Captions
Published Friday, June 14, 2019 — 4 minute read
Did you know you can add captions to
<video> elements with a little HTML and a VTT file? I didn't know until today! It's really easy to do (so long as you format your VTT file correctly... shout out to Conlin Durbin for helping me find that error 😄). At a minimum, you need 3 files:
- Video, probably
- WebVTT, where the captions are
Let's get started
1<video width="100%" title="Puppy" controls> 2 <source src="Puppiness.mp4" type="video/mp4" /> 3 <track src="transcript.vtt" label="English captions" default /> 4</video> 5
This is the easiest step. You only need 3 elements. On the
<video> element, make you sure you include the
controls attribute. This will allow users to toggle captions or switch to a different langue, if applicable.
<source> element should be your video. Specify its
type attributes. The
<track> element is for the
.vtt file you'll create in the next step. There are multiple attributes to include here.
kinddefaults to "subtitles", but if you specify it you must also include the
labelis the title of the track and will be displayed in the video controls menu where captions can be toggled.
defaultis included to set a track as enabled.
1WEBVTT 2 31 400:00:00.000 --> 00:00:01.000 5SAND? OM NOM 6 72 800:00:02.000 --> 00:00:04.000 9NO, BLEH 10 113 1200:00:04.000 --> 00:00:08.000 13LEASH! OM NOM 14 154 1600:00:08.000 --> 00:00:10.000 17GIVE IT 18 195 2000:00:10.000 --> 00:00:14.000 21IS MINE 22
The format for this file is very specific. You must include "WEBVTT" at the top of the file, and then there are sections with white space in between which are called "cues". In the example file I've attached above, the first line of each cue is called the identifier. The next line specifies the timing for the cue. Finally, we have the cue payload. This is where you include the content for each caption. You can do some styling here.
The final product
Here's a GIF preview of the captions I added to a video of a puppy playing on a sandy beach.
I wasn't able to find a ton of examples out there, but there are plenty of resources that get into the differences between subtitles and captions, styling abilities, and additional ways to accomplish the same thing as above but with different transcript formats or third-party libraries.
- "How-to: Using captions with HTML's video element" (The A11Y Project)
- "Web Video Text Tracks Format (WebVTT)" (MDN)