×

Search anything:

Web Video Text Tracks Format (WebVTT): for adding Video Subtitles

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

TABLE OF CONTENTS

  • What is WebVTT?
  • WebVTT files
  • WebVTT body
  • WebVTT cues
  • WebVTT comments
  • Styling WebVTT cues
  • Instance methods and properties
  • Specifications
  • Browser compatibility
  • WebVTT Tutorial

Key Takeaways (WebVTT)

WebVTT is a format for HTML5 video captions, subtitles and overlays. It is a text file with timed cues, and it starts with a WebVTT body. Cues display text at specific times, while comments are added as notes. You can style cues to enhance presentation.

What is WebVTT?

WebVTT (Web Video Text Tracks) is a format used for displaying captions, subtitles, and other text overlays in HTML5 videos. It is a text-based format that allows you to specify the timing and positioning of text tracks within a video. WebVTT files are commonly used for providing accessibility features in videos, making them accessible to a wider audience.

In this article, we'll explore the WebVTT format, including its structure, comments, cues, styling, instance methods and properties, and how to write a WebVTT file. We'll also discuss CSS pseudo-classes, specifications, and browser compatibility.

WebVTT Files

A WebVTT file is a simple text file with a .vtt extension. It contains a series of cues(timed text element that specifies when and how a particular piece of text should be displayed), each associated with a specific time range in the video. These cues can include text, timestamps, and optional styling information.

WebVTT Body

A WebVTT file typically starts with the line WEBVTT, which indicates the beginning of the WebVTT body. Following this line, you can define cues and styles as needed.


    WEBVTT

    0:00:00.000 --> 0:00:05.000
    Hey, how are you doing mate?.

WebVTT Cues

A cue in a WebVTT file consists of a time span during which the text should be displayed and the actual text content. The time span is defined using timestamps in the format hh:mm:ss.sss. Each cue should have a start time and an end time separated by -->.


    WEBVTT

    0:00:00.000 --> 0:00:05.000
    This is a cue.

WebVTT Comments

You can add comments to a WebVTT file by starting a line with the NOTE keyword. Comments are ignored by the WebVTT parser and are useful for providing information to humans reading the file.

   
    WEBVTT

    NOTE
    This is a comment.

Styling WebVTT Cues

WebVTT allows you to apply basic styles to your cues using CSS-like syntax. You can specify styles for a cue using the ::cue pseudo-element followed by the desired CSS properties.

Here's an example of styling a cue:


    WEBVTT

    0:00:00.000 --> 0:00:05.000
    ::cue {
      color: yellow;
      background: black;
      font-size: 18px;
    }

Instance Methods and Properties

When working with WebVTT cues in JavaScript, you can use instance methods and properties to manipulate and retrieve information about cues. Here are some common instance methods and properties:

getCueAsHTML(): Converts the cue's text to an HTML element.
getStartTime(): Returns the start time of the cue.
getEndTime(): Returns the end time of the cue.
text: Property containing the text content of the cue.


    const track = video.textTracks[0];
    const cue = track.cues[0];

    const startTime = cue.getStartTime();
    const endTime = cue.getEndTime();
    const text = cue.text;

    const cueElement = cue.getCueAsHTML();

Specifications

WebVTT is defined in the WebVTT specification, which provides detailed information on its syntax and usage(Feel free to check it out). It is maintained by the World Wide Web Consortium (W3C).

Browser Compatibility

WebVTT is supported by most modern web browsers, including:

  • Google Chrome
  • Mozilla Firefox
  • Microsoft Edge
  • Apple Safari

Tutorial on How to Write a WebVTT File

Step 1: Create a New Text File

Start by creating a new text file using a plain text editor. You can use any text editor, such as Notepad (Windows), TextEdit (macOS), or a code editor like Visual Studio Code.

Step 2: Define the WebVTT Header

Begin your WebVTT file with the line WEBVTT to indicate the start of the WebVTT body.


    WEBVTT

Step 3: Add Cues

For each cue you want to include, specify the cue's time range and content. Use the --> delimiter to separate the start and end times.

    // your example.vtt file

    WEBVTT
    
    0:00:00.000 --> 0:00:05.000
    This is the first cue.

    0:00:05.000 --> 0:00:10.000
    And this is the second cue.

Step 4: Style with CSS Pseudo-Classes(optional)

WebVTT supports CSS pseudo-classes to target cues in your stylesheet. You can use these pseudo-classes to style different aspects of your text tracks, such as the default color and background.

Here are some commonly used css pseudo-classes:

::cue: Styles the entire cue.
::cue-backdrop: Styles the backdrop behind the cue text.
::cue-region: Styles the region containing the cues.

    /* your styles.css */

    ::cue {
      color: white;
      background: black;
    }

    ::cue-region {
      font-size: 18px;
    }

Step 5: Save the File

Save the text file with a .vtt extension. For example, you can name it example.vtt. Ensure that the file encoding is set to UTF-8.

Step 6: Use the WebVTT File

To use the WebVTT file with an HTML5 video element, you can specify the track element within the video tag. Set the src attribute to the url of your WebVTT file.

  *your index.html*

    <video controls>
      <source src="example.mp4" type="video/mp4">
      <track src="example.vtt" kind="subtitles" label="English" srclang="en">
    </video>

Summary

  • WebVTT (Web Video Text Tracks) is a popular format for displaying timed text, captions, and subtitles in HTML5 videos.

  • A WebVTT file consists of a header and a body, with the body containing the text cues and their timing information.

  • Cues are the heart of WebVTT files, defining what text to display and when to display it during video playback.

  • Comments in WebVTT files provide helpful context or notes to anyone working with the file but are ignored during rendering.

  • WebVTT allows you to apply basic styles to cues using CSS-like syntax or CSS pseudo-classes based on their state or timing, enhancing the appearance of captions and subtitles.

  • WebVTT provides instance methods and properties for manipulating cues and track settings programmatically.

This article provides a comprehensive overview of WebVTT, from its file structure to cue styling, and offers practical guidance on creating and using WebVTT files for accessible and visually appealing video content.

Thanks for reading and Happy coding...

Web Video Text Tracks Format (WebVTT): for adding Video Subtitles
Share this