## HTML Audio/Video DOM: videoTracks Property
The `videoTracks` property is a powerful feature of the HTML5 video DOM API that allows developers to access and manage the available video tracks within a media element.
This tutorial provides a comprehensive reference for the `videoTracks` property, including its syntax, return values, and practical code examples.
---
## Definition and Usage
The `videoTracks` property returns a **`VideoTrackList`** object.
* The **`VideoTrackList`** object represents all the available video tracks for a given video element.
* Each individual video track within this list is represented by a **`VideoTrack`** object.
This property is particularly useful for multi-track video streams, allowing you to programmatically inspect, select, or switch between different video angles, quality levels, or alternative visual presentations.
---
## Syntax
```javascript
videoElement.videoTracks
```
### Return Value
The property returns a `VideoTrackList` object, which contains a collection of `VideoTrack` objects.
---
## API Reference
### 1. The VideoTrackList Object
The `VideoTrackList` represents the collection of video tracks. It provides the following properties and methods:
| Property / Method | Type | Description |
| :--- | :--- | :--- |
| `length` | `Number` | Returns the number of available video tracks in the video. |
| `selectedIndex` | `Number` | Returns the index of the currently selected/active `VideoTrack` object. Returns `-1` if no track is selected. |
| `getTrackById(id)` | `Function` | Returns the `VideoTrack` object matching the specified string `id`. |
| `` | `VideoTrack` | Accesses the `VideoTrack` object at the specified index (starting from `0`). |
### 2. The VideoTrack Object
Each individual `VideoTrack` object represents a single video track and contains the following properties:
| Property | Type | Description |
| :--- | :--- | :--- |
| `id` | `String` | The unique identifier of the video track. |
| `kind` | `String` | The category/type of the video track. Possible values include:
β’ `"alternative"` (alternative video representation)
β’ `"captions"` (video with burned-in captions)
β’ `"main"` (the primary video track)
β’ `"sign"` (sign-language translation track)
β’ `"subtitles"` (video with burned-in subtitles)
β’ `"commentary"` (commentary track)
β’ `""` (empty string for undefined types) |
| `label` | `String` | A user-readable label for the video track (e.g., "Director's Cut", "Main Angle"). |
| `language` | `String` | The language code of the video track (e.g., `"en"`, `"zh"`). |
| `selected` | `Boolean` | Gets or sets whether the video track is currently active (`true` or `false`). Only one video track can be active at a time. |
---
## Code Examples
### Example 1: Get the Number of Available Video Tracks
The following example retrieves the video element and alerts the total number of available video tracks.
```javascript
// Get the video element by ID
var myVid = document.getElementById("video1");
// Alert the number of available video tracks
alert("Available video tracks: " + myVid.videoTracks.length);
```
### Example 2: Inspecting and Selecting a Video Track
This example demonstrates how to loop through all available video tracks, inspect their properties, and programmatically enable a specific track.
```javascript
var video = document.getElementById("myVideo");
var tracks = video.videoTracks;
// Loop through all available video tracks
for (var i = 0; i < tracks.length; i++) {
console.log("Track ID: " + tracks.id);
console.log("Track Label: " + tracks.label);
console.log("Track Language: " + tracks.language);
console.log("Track Kind: " + tracks.kind);
// Enable the track if it is an alternative angle
if (tracks.kind === "alternative") {
tracks.selected = true;
console.log("Switched to alternative video track: " + tracks.label);
}
}
```
---
## Browser Compatibility
> **Warning:** The `videoTracks` property is **not supported** by default in most mainstream modern browsers (such as Google Chrome, Mozilla Firefox, and Microsoft Edge) without experimental flags enabled.
>
> For production environments, it is highly recommended to use specialized JavaScript media player libraries (such as **Hls.js**, **Dash.js**, or **Video.js**) to manage multi-track video streams, as they provide cross-browser polyfills and custom implementations for track selection.
π Categories
- β‘ JavaScript (1589)
- π PHP (872)
- π Python3 (810)
- π HTML (691)
- βοΈ C# (650)
- π Python (594)
- β Java (552)
- βοΈ PyTorch (534)
- π§ Linux (472)
- βοΈ C (432)
- π¦ jQuery (406)
- π¨ CSS (377)
- π XML (259)
- π¦ jQuery UI (231)
- π― Bootstrap (220)
- βοΈ C++ (215)
- π °οΈ Angular (205)
- π HTML DOM (201)
- π΄ Redis (188)
- π Web Building (142)
- π Vue.js (141)
- π R (131)
- πΌ Pandas (124)
- ποΈ SQL (105)
- βοΈ Docker (86)
- βοΈ TypeScript (73)
- βοΈ Highcharts (70)
- π AI Agent (70)
- βοΈ React (68)
- π Node.js (65)
- βοΈ Machine Learning (60)
- π Git (59)
- π΅ Go (58)
- π Markdown (58)
- π’ NumPy (55)
- π§ͺ Flask (54)
- βοΈ Scala (53)
- ποΈ SQLite (52)
- π JSTL (52)
- βοΈ VS Code (51)
- π MongoDB (49)
- π Perl (48)
- π Ruby (47)
- π Matplotlib (47)
- βοΈ Uncategorized (46)
- π Swift (46)
- ποΈ PostgreSQL (46)
- βοΈ Data Structures (46)
- π Playwright (46)
- π iOS (45)
- ποΈ MySQL (44)
- βοΈ LangChain (43)
- π FastAPI (40)
- βοΈ Ionic (38)
- π Design Patterns (37)
- βοΈ Eclipse (37)
- π¨ CSS3 (34)
- π Lua (34)
- βοΈ Codex (34)
- πΈ Django (32)
- βοΈ OpenCV (32)
- π Rust (31)
- π JSP (31)
- βοΈ Claude Code (31)
- π Pillow (30)
- βοΈ OpenCode (28)
- π AI Skills (27)
- π Flutter (26)
- π Maven (26)
- π¨ Tailwind CSS (25)
- π§ TensorFlow (25)
- π Servlet (24)
- π Dart (23)
- π Assembly (23)
- βοΈ Memcached (22)
- βοΈ SVG (22)
- βοΈ Electron (22)
- π NLP (22)
- π Regex (21)
- π Android (20)
- π£ Kotlin (19)
- π Julia (19)
- π SOAP (17)
- π Selenium (17)
- π PowerShell (17)
- π Sass (16)
- π HTTP (16)
- π Zig (15)
- π AI (15)
- π AJAX (14)
- π Swagger (14)
- βοΈ Scikit-learn (13)
- βοΈ ECharts (13)
- βοΈ Chart.js (13)
- βοΈ Cursor (13)
- βοΈ SciPy (12)
- π RDF (12)
- π Ollama (12)
- π Next.js (12)
- π Plotly Dash (12)
- π JSON (11)
- π RESTful API (11)
- π WSDL (9)
- βοΈ CMake (8)
- π Firebug (7)
- π Nginx (6)
- βΈοΈ Kubernetes (6)
- π Jupyter (6)
- π LaTeX (4)
- π UniApp (4)
- ποΈ SQL Server (1)
YouTip