I want to display a list of video cameras attached to the user's computer, and when they select one, display streaming video from that camera in an HTML5 <video>
tag.
How can I get a list of the video cameras attached to the user's computer?
I want to display a list of video cameras attached to the user's computer, and when they select one, display streaming video from that camera in an HTML5 <video>
tag.
How can I get a list of the video cameras attached to the user's computer?
Perhaps Navigator.getUserMedia()
(uses WebRTC under the hood) is what you're looking for, though I don't see anything that will directly tell you what devices are available (the list of devices isn't exposed to your code—it's presented to the user when asking for permission to access available hardware).
Also note the browser support: Chrome 21+, Firefox 20+, Opera 12+, no support for IE and possibly Safari.
getUserMedia
doesn't return the available attached devices, it only prompts a popup to the user asking what device he wants to use. If the user accepts, the second parameter on getUserMedia method (an acceptCallback) is called; otherwise, the third parameter (failedCallback) is called. dev.w3.org/2011/webrtc/editor/… –
Absurdity successCallback
does pass in a MediaStream
object which exposes track information. –
Combustible getUserMedia
already does it for you in the popup... –
Absurdity Navigator.getUserMedia
. –
Frustrated getUserMedia
is for an important security reason. WebDevelopers should never be able to start a local streaming without the user permission. –
Absurdity navigator.mediaDevices.enumerateDevices()
) –
Bordy Only works in chrome and edge
<script>
navigator.mediaDevices.enumerateDevices().then(function (devices) {
for(var i = 0; i < devices.length; i ++){
var device = devices[i];
if (device.kind === 'videoinput') {
var option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || 'camera ' + (i + 1);
document.querySelector('select#videoSource').appendChild(option);
}
};
});
</script>
<select id="videoSource"></select>
Perhaps Navigator.getUserMedia()
(uses WebRTC under the hood) is what you're looking for, though I don't see anything that will directly tell you what devices are available (the list of devices isn't exposed to your code—it's presented to the user when asking for permission to access available hardware).
Also note the browser support: Chrome 21+, Firefox 20+, Opera 12+, no support for IE and possibly Safari.
getUserMedia
doesn't return the available attached devices, it only prompts a popup to the user asking what device he wants to use. If the user accepts, the second parameter on getUserMedia method (an acceptCallback) is called; otherwise, the third parameter (failedCallback) is called. dev.w3.org/2011/webrtc/editor/… –
Absurdity successCallback
does pass in a MediaStream
object which exposes track information. –
Combustible getUserMedia
already does it for you in the popup... –
Absurdity Navigator.getUserMedia
. –
Frustrated getUserMedia
is for an important security reason. WebDevelopers should never be able to start a local streaming without the user permission. –
Absurdity navigator.mediaDevices.enumerateDevices()
) –
Bordy try out this...
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="author" content="Victor Stan">
<meta name="description" content="Get multiple video streams on one page. Adapted from code by Muaz Khan">
<title>Video Camera</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ></script>
<style type="text/css" media="screen">
video {
border:1px solid gray;
}
</style>
</head>
<body>
<script>
if (!MediaStreamTrack) document.body.innerHTML = '<h1>Incompatible Browser Detected. Try <strong style="color:red;">Chrome Canary</strong> instead.</h1>';
var videoSources = [];
MediaStreamTrack.getSources(function(media_sources) {
console.log(media_sources);
alert('media_sources : '+media_sources);
media_sources.forEach(function(media_source){
if (media_source.kind === 'video') {
videoSources.push(media_source);
}
});
getMediaSource(videoSources);
});
var get_and_show_media = function(id) {
var constraints = {};
constraints.video = {
optional: [{ sourceId: id}]
};
navigator.webkitGetUserMedia(constraints, function(stream) {
console.log('webkitGetUserMedia');
console.log(constraints);
console.log(stream);
var mediaElement = document.createElement('video');
mediaElement.src = window.URL.createObjectURL(stream);
document.body.appendChild(mediaElement);
mediaElement.controls = true;
mediaElement.play();
}, function (e)
{
alert('Hii');
document.body.appendChild(document.createElement('hr'));
var strong = document.createElement('strong');
strong.innerHTML = JSON.stringify(e);
alert('strong.innerHTML : '+strong.innerHTML);
document.body.appendChild(strong);
});
};
var getMediaSource = function(media) {
console.log(media);
media.forEach(function(media_source) {
if (!media_source) return;
if (media_source.kind === 'video')
{
// add buttons for each media item
var button = $('<input/>', {id: media_source.id, value:media_source.id, type:'submit'});
$("body").append(button);
// show video on click
$(document).on("click", "#"+media_source.id, function(e){
console.log(e);
console.log(media_source.id);
get_and_show_media(media_source.id);
});
}
});
}
</script>
</body>
</html>
JavaScript cannot access your cameras to return a list. You will need to use a Flash SWF to get the camera information and pass it back to your page's JavaScript.
EDIT: to those who downvoted. These methods will not give him a dropdown list of available cameras. If it does, please post a link or code. At the current date, the only way to get a list of cameras (which is what his questions was) is to use Flash (or possibly silverlight, but Flash has much broader install coverage). I've edited my question to be a little more specific in terms of getting the list versus accessing a camera.
await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
let devices = await navigator.mediaDevices.enumerateDevices();
devices = devices.filter((device) => device.kind == "videoinput");
devices variable is the array of webcam/video input devices.
Getting a list of camera devices
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === 'videoinput');
Example: Setting a video device for playback
// get list of devices
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === 'videoinput');
// for this example, I'll choose the first one
const videoDevice = videoDevices[0];
// request user permission on media stream
// note: normally, you can call this multiple times w/o needing to
// request permission again
mediaStreamRef = await navigator.mediaDevices.getUserMedia({
audio: true,
video: {
deviceId: videoDevice?.deviceId
}
});
// get video node
const videoRef = document.querySelector('video');
// reset everything on the video
videoRef.src = null;
videoRef.srcObject = mediaStreamRef;
videoRef.play();
Notes:
navigator.mediaDevices
API docs https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices© 2022 - 2025 — McMap. All rights reserved.