W3cubDocs

/Web APIs

VideoTrack

The VideoTrack interface represents a single video track from a <video> element.

The most common use for accessing a VideoTrack object is to toggle its selected property in order to make it the active video track for its <video> element.

Instance properties

selected

A Boolean value which controls whether or not the video track is active. Only a single video track can be active at any given time, so setting this property to true for one track while another track is active will make that other track inactive.

id Read only

A string which uniquely identifies the track within the media. This ID can be used to locate a specific track within a video track list by calling VideoTrackList.getTrackById(). The ID can also be used as the fragment part of the URL if the media supports seeking by media fragment per the Media Fragments URI specification.

kind Read only

A string specifying the category into which the track falls. For example, the main video track would have a kind of "main".

label Read only

A string providing a human-readable label for the track. For example, a track whose kind is "sign" might have a label of "A sign-language interpretation". This string is empty if no label is provided.

language Read only

A string specifying the video track's primary language, or an empty string if unknown. The language is specified as a BCP 47 (RFC 5646) language code, such as "en-US" or "pt-BR".

sourceBuffer Read only

The SourceBuffer that created the track. Returns null if the track was not created by a SourceBuffer or the SourceBuffer has been removed from the MediaSource.sourceBuffers attribute of its parent media source.

Usage notes

To get a VideoTrack for a given media element, use the element's videoTracks property, which returns a VideoTrackList object from which you can get the individual tracks contained in the media:

js

const el = document.querySelector("video");
const tracks = el.videoTracks;

You can then access the media's individual tracks using either array syntax or functions such as forEach().

This first example gets the first video track on the media:

js

const firstTrack = tracks[0];

The next example scans through all of the media's video tracks, activating the first video track that is in the user's preferred language (taken from a variable userLanguage).

js

for (const track of tracks) {
  if (track.language === userLanguage) {
    track.selected = true;
    break;
  }
}

The language is in standard (RFC 5646) format. For US English, this would be "en-US", for example.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
VideoTrack 37 7912–79 33 10 24 7 No 37 No No 7 No
id 37 7912–79 33 10 24 7 No 37 No No 7 No
kind 37 7912–79 33 10 24 7 No 37 No No 7 No
label 37 7912–79 33 10 24 7 No 37 No No 7 No
language 37 7912–79 33 10 24 7 No 37 No No 7 No
selected 37 7912–79 33 10 24 7 No 37 No No 7 No
sourceBuffer 51 7912–79 No No 38 7 No 51 No No
13Exposed in Mobile Safari on iPad but not on iPhone.
No

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/VideoTrack