go to all blogs

Ever wondered how you can stream your own webcam recording live on HTML page?

Well, in this quick and easy post, we will have a look at how you can live stream your webcam straight to your HTML page.

Let's get started.

Setup

HTML required for this short demo is one liner. Create a file index.html and paste the following:

1<video id="myVidPlayer" controls muted autoplay></video>

This embeds a Simple HTML5 video player to your page. controls muted autoplay are optional parameters to video tag, which clearly describe their purposes.

Now, it's time to add some life to the video player. This code will Stream your Webcam recording to the created video player.

1<script type="text/javascript">
2 //Selector for your <video> element
3 const video = document.querySelector('#myVidPlayer');
4
5 //Core
6 window.navigator.mediaDevices.getUserMedia({ video: true })
7 .then(stream => {
8 video.srcObject = stream;
9 video.onloadedmetadata = (e) => {
10 video.play();
11 };
12 })
13 .catch( () => {
14 alert('You have give browser the permission to run Webcam and mic ;( ');
15 });
16
17</script>

Here, getUserMedia() is a function which needs an object as a parameter with either of audio/video as a compulsory property set to true. Else, you will get an error. This function then returns a Promise.

Upon running this code, browser will ask for our permission for the usage of Microphone and Camera. If you "allow" it to use, then our Promise resolves with "stream" as a parameter which we assign it to video.srcObject. This sets the stage up for playing the video. Upon successful loading of metadata of our video player (video.onloadmetadata), we start the actual streaming with video.play() and you will see your "beautiful" face right there on your HTML page, else it is rejected with an alert box popup

Bonus : Snapshot of current video frame

Now, we will add a button which will get the snapshot of the current video frame on click and will display it to you in a canvas element.

1<button onclick="snapshot()">Snapshot</button>
2
3<div class="mycanvas">
4 <h6>Captured snapshot</h6>
5 <canvas></canvas>
6</div>

Let's rewrite our complete JavaScript

1<script type="text/javascript">
2 var canvas = document.querySelector("canvas");
3 var context = canvas.getContext("2d");
4 const video = document.querySelector('#myVidPlayer');
5
6 //w-width,h-height
7 var w, h;
8 canvas.style.display = "none";
9
10 //new
11 function snapshot(){
12 context.fillRect(0, 0, w, h);
13 context.drawImage(video, 0, 0, w, h);
14 canvas.style.display = "block";
15 }
16
17 window.navigator.mediaDevices.getUserMedia({ video: true, audio: true })
18 .then(stream => {
19 video.srcObject = stream;
20 video.onloadedmetadata = (e) => {
21 video.play();
22
23 //new
24 w = video.videoWidth;
25 h = video.videoHeight
26
27 canvas.width = w;
28 canvas.height = h;
29 };
30 })
31 .catch(error => {
32 alert('You have to enable the mike and the camera');
33 });
34</script>

On clicking the button, canvas context is filled with a 2D rectangle with w=width and h=height. (0,0) is passed to tell that we want to fill the context from left top corner of the canvas. Then, we attach our video frame snapshot to the canvas and make it visible with display:"block" which was hidden by default. More info on Canvas API is here.

Here is the Code pen link Codepen.

Conclusion

This was it for this short and sweet tutorial. See you next time ;)