A clip can show motion or sound that is hard to describe in text. HTML lets you embed a video alongside the rest of the page, with browser controls for playback and a separate track viewers can turn on for captions.

WebVTT captions can include speech and sounds such as an alarm or a knock at the door. Each cue has a start and end time, so its text appears over the video when the sound occurs.

For a local preview, the HTML, video and captions can share a folder on a running test web server. Keeping them together lets the HTML refer to each file by name. The server should serve the WebVTT file as text/vtt.

Steps to embed a video in HTML:

  1. Save a copy of your H.264 MP4 as demo.mp4 in a fresh video-demo folder on your local test site.

    The preview uses an MP4 with H.264 video and AAC audio.

  2. Create a caption file in your text editor with cue times and text that match your video.
    WEBVTT
     
    00:00:03.000 --> 00:00:34.000
    [Low tone plays]
     
    00:00:36.000 --> 00:01:09.000
    [High tone plays]

    The sample describes two tones. Each timing line gives the start and end in hours, minutes, seconds and milliseconds. A blank line goes after WEBVTT and between cues.

  3. Save the caption file as UTF-8 plain text named captions.vtt in video-demo.
  4. Create video.html in video-demo with this starter markup in your text editor.
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Video demo</title>
    </head>
    <body>
      <h1>Video demo</h1>
    </body>
    </html>
  5. Insert the video element immediately after the Video demo heading in the markup.
      <video controls src="demo.mp4" width="640">
      </video>

    src points to the MP4 in the same folder. The controls attribute shows the browser's player controls.

  6. Insert the caption track immediately before </video>.
        <track kind="captions" src="captions.vtt" srclang="en" label="English">

    kind=“captions” identifies the track as captions, srclang=“en” declares the language, and label=“English” names the option shown in the player. For another language, the code and label should both match the caption text. The HTML Standard defines the caption track attributes.

  7. Save video.html.
  8. Open video-demo/video.html from your local test site in desktop Chrome.

  9. Open the three-dot menu at the right end of the video controls.
  10. Open Captions in the video menu.
  11. Select your track's label in the captions menu.

    The sample track is labeled English.

  12. Click Play at the left end of the video controls.
  13. Verify that the elapsed time advances from 0:00.
  14. Verify that the video displays your caption during its cue interval.

    With the sample file, [Low tone plays] appears over the video from 0:03 until 0:34. If the text is missing during the cue, check that captions.vtt is served from the same folder as video.html and that its first line is WEBVTT.

Share this guide