JavaScript MediaStream getVideoTracks() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

The getVideoTracks() method of MediaStream returns an array of video MediaStreamTrack objects in the stream. Learn the MDN ImageCapture camera-track pattern, empty arrays for audio-only streams, filtering with track.kind, stopping the camera while keeping the mic, and comparison with getTracks()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Track array

03

Filter

kind video

04

Empty

no camera

05

Use case

ImageCapture

06

Status

Baseline widely

Introduction

A MediaStream can hold both audio and video tracks. When you call getUserMedia({ video: true }), the browser adds at least one camera track. getVideoTracks() gives you only the video ones—handy for stopping the camera, passing a track to ImageCapture, or inspecting screen-share output without touching audio.

MDN: the return value is an array where each entry is a MediaStreamTrack with kind === "video". If there are no video tracks, you get an empty array []. The order of tracks is not guaranteed and may change between calls.

💡
Beginner tip

Use getVideoTracks()[0] when you expect one camera—but always check .length first so you do not call methods on undefined.

Understanding MediaStream.getVideoTracks()

An instance method that filters the stream’s track set to video tracks only.

  • Parameters — none.
  • Return value — array of MediaStreamTrack with kind "video".
  • Empty array — when the stream has no video tracks (e.g. microphone only).
  • Order — not defined by the spec; may vary between calls (MDN).
  • Pair withgetAudioTracks() for microphone tracks.
  • Baseline Widely available on MDN (since September 2017).

📝 Syntax

JavaScript
getVideoTracks()

Parameters

None.

Return value

An array of MediaStreamTrack objects for video tracks. Empty if none exist.

Typical pattern (MDN idea)

JavaScript
let imageCapture;

navigator.mediaDevices.getUserMedia({ video: true }).then((mediaStream) => {
  document.querySelector("video").srcObject = mediaStream;

  const track = mediaStream.getVideoTracks()[0];
  imageCapture = new ImageCapture(track);

  return imageCapture.getPhotoCapabilities();
});

⚡ Quick Reference

GoalCode / note
List video tracksstream.getVideoTracks()
Count video tracksstream.getVideoTracks().length
First camera trackstream.getVideoTracks()[0]
Stop cameragetVideoTracks().forEach((t) => t.stop())
ImageCapture (MDN)new ImageCapture(getVideoTracks()[0])
MDN statusBaseline Widely available (since September 2017)

🔍 At a Glance

Four facts to remember about MediaStream.getVideoTracks().

Returns
array

Video tracks

Filter
kind video

Built-in

Empty
[]

No video

Baseline
widely

Since Sep 2017

Examples Gallery

Examples follow MDN MediaStream: getVideoTracks(). Camera examples use getUserMedia (secure context + permission); audio-only demos use microphone-only capture.

📚 Getting Started

MDN pattern: get the camera track for ImageCapture.

Example 1 — MDN: Camera Track for ImageCapture

Get video from getUserMedia, then getVideoTracks()[0].

JavaScript
let imageCapture;

navigator.mediaDevices.getUserMedia({ video: true }).then((mediaStream) => {
  document.querySelector("video").srcObject = mediaStream;

  const track = mediaStream.getVideoTracks()[0];
  imageCapture = new ImageCapture(track);

  return imageCapture.getPhotoCapabilities();
});
Try It Yourself

How It Works

MDN: getVideoTracks()[0] isolates the camera track for photo APIs.

Example 2 — Video-Only getUserMedia

Request only video: true—typically one video track.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: false, video: true })
  .then((stream) => {
    const videoTracks = stream.getVideoTracks();
    console.log(videoTracks.length, videoTracks[0].kind);
  });
Try It Yourself

How It Works

Every returned track has kind "video" by definition.

📈 Empty Arrays & Comparisons

Audio-only streams and filtering alternatives.

Example 3 — Audio-Only Stream Returns []

getUserMedia({ audio: true, video: false })—no video tracks.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: false })
  .then((stream) => {
    const video = stream.getVideoTracks();
    const audio = stream.getAudioTracks();
    console.log(video.length, audio.length);
  });
Try It Yourself

How It Works

MDN: empty array when the stream contains no video tracks.

Example 4 — getVideoTracks() vs getTracks().filter()

Both select video tracks; the dedicated method is clearer.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    const direct = stream.getVideoTracks();
    const filtered = stream.getTracks().filter((t) => t.kind === "video");
    console.log(direct.length, filtered.length);
  });
Try It Yourself

How It Works

Prefer getVideoTracks() for readability; counts should match for standard captures.

Example 5 — Stop Camera, Keep Microphone

Stop only video tracks while audio continues.

JavaScript
function stopCamera(stream) {
  stream.getVideoTracks().forEach((track) => {
    track.stop();
  });
}

navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    stopCamera(stream);
    console.log(
      stream.getVideoTracks().length,
      stream.getAudioTracks().length
    );
  });
Try It Yourself

How It Works

stop() on video tracks ends the camera; audio tracks are untouched.

🚀 Common Use Cases

  • Pass a camera track to ImageCapture for still photos (MDN pattern).
  • Stop the camera while keeping the microphone in a video call.
  • Build a camera-off toggle with track.enabled = false.
  • Count how many video inputs are in a composed stream.
  • Verify a stream has video before attaching to a <video> element.

🔧 How It Works

1

Get a MediaStream

From getUserMedia, captureStream, WebRTC, or addTrack composition.

Context
2

Call getVideoTracks()

Browser collects tracks where MediaStreamTrack.kind is "video".

Filter
3

Receive array

Zero or more MediaStreamTrack objects—order not guaranteed by spec.

Result
4

Act on tracks

Stop camera, disable video, or pass to ImageCapture—without affecting audio.

📝 Notes

  • MDN: Baseline Widely available (since September 2017) — no Deprecated / Experimental / Non-standard banner.
  • Order not defined — do not rely on [0] being the same track every call without checking id or label.
  • Secure contextgetUserMedia examples need HTTPS or localhost.
  • Empty array — normal for audio-only streams.
  • ImageCapture — MDN example; check browser support before using in production.
  • Related learning: getAudioTracks(), getTracks(), addTrack(), getUserMedia().

Universal Browser Support

MediaStream.getVideoTracks() is marked Baseline Widely available on MDN (since September 2017). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

MediaStream.getVideoTracks()

Returns an array of video MediaStreamTrack objects in the stream.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer No MediaStream.getVideoTracks
Not supported
MediaStream.getVideoTracks() Excellent

Bottom line: Call getVideoTracks() to list, stop, or pass camera tracks to other APIs.

Conclusion

MediaStream.getVideoTracks() returns every video track in a stream as an array. Use it to stop or disable the camera, count video inputs, or pass a track to ImageCapture—MDN’s example uses getVideoTracks()[0] for photo capabilities.

Continue with getAudioTracks(), getTracks(), addTrack(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check .length before using [0]
  • Use getVideoTracks() instead of manual filtering
  • Stop video tracks when the user turns camera off
  • Pair with getAudioTracks() for full stream control
  • Feature-detect ImageCapture before using it

❌ Don’t

  • Assume track order stays the same between calls
  • Call tracks[0].stop() without checking length
  • Expect video from audio-only getUserMedia
  • Confuse enabled (disable) with stop() (end)
  • Forget secure-context requirements for getUserMedia

Key Takeaways

Knowledge Unlocked

Five things to remember about getVideoTracks()

List and control camera tracks in a stream.

5
Core concepts
🔗02

Filter

kind video

Rule
📦03

Empty

[] ok

Edge
📄04

MDN

ImageCapture

Pattern
🎯05

Baseline

since Sep 2017

Status

❓ Frequently Asked Questions

An array of MediaStreamTrack objects whose kind property is "video". If the stream has no video tracks, the array is empty.
No. MDN marks MediaStream.getVideoTracks() as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
No. MDN documents getVideoTracks() with no parameters. Call it on a MediaStream: stream.getVideoTracks().
No. MDN says the order is not defined by the specification and may change from one call to the next.
getTracks() returns every track in the stream. getVideoTracks() returns only tracks where track.kind === "video".
After getUserMedia with video, call getVideoTracks()[0] and pass the track to the ImageCapture constructor for photo capabilities.
Did you know?

MDN’s getVideoTracks() example passes getVideoTracks()[0] to new ImageCapture(track) so you can read photo capabilities from the live camera feed.

Detach tracks

Learn removeTrack()—remove a MediaStreamTrack from a stream.

removeTrack() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful