Thought leadership from the most innovative tech companies, all in one place.

Build a Video, Audio + Screen Recorder Web App with JavaScript

image

In this pandemic, a lot has changed and so the work culture with something we can't get rid of, yes “Remote Meetings”. While at one of those meetings, I got curious about the meeting platform and after some search came to know what our browsers are capable of (inbuilt browser APIs).

So, I planned to develop a simple recorder that supports Audio, Video, and Screen Recording available at http://recorder.dhruv479.dev

And if you like this article, don't miss to clap and follow.

To proceed further, you should be acquainted with some terms:

MediaRecorder: The MediaRecorder interface of the MediaStream API provides functionality to easily record media ref: MDN

MediaDevices: The MediaDevices interface provides access to connected media input devices like cameras and microphones, as well as screen-sharing ref: MDN

Ok, ready to roll? let's add some code!

Recording Handler: At first, we will write a function that intakes the stream and mimeType, collect the data chunks and then enable downloading the file on the local machine from the browser.

handle-stream-record.js

First, create a MediaRecorder instance with the input stream. Then, add events for ondataavailable (when data comes in stream) and onstop (when the stream stops).

After, chunk is available, it is pushed in the array and after the stop event, all the chunks are put together to form a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) specifying the mimeType.

Later, URL is created for the blob object and can be downloaded from the browser.

Audio Recorder: Going forward, we will write a function to invoke the audio recording that further calls handleRecord with the required parameters.

audio-stream-record.js

Here, we will use mediaDevices.getUserMedia to get access to resources on the user's machine. After calling this, we will get a prompt asking for permission in the browser and after confirmation, we can access those resources. Also, we can pass additional parameters to the constraints like audio: { echoCancellation: true } which reduces the sound echo.

Video Recorder: Similar to Audio Recording, we need to add video in the parameter of the getUserMedia function.

video-stream-record.js

After adding video constraint in getUserMedia , you will get an additional prompt to camera permission and after the confirmation, guess what, we can access the camera stream. Also, additional params can be passed to video such as: video: { width: {min: 640}, height: {min: 480}}

Screen Recorder: Amongst all, this is the most interesting, as the display stream is provided by getDisplayMedia, and the audio is provided by getUserMedia . So, we need to merge different streams here if need an audio background for the screen record.

screen-stream-record.js

We merged the different streams to create a new stream and passed it to the handleRecord function. Here also, we can add more specific constraints as per the need.

Constraints passed to capture media can be more specific like resolution, echo, cursor instead of true/false.

Conclusion

Voila, well the JavaScript part is done 😁. We just need to add some basic HTML to invoke those functions, stop recording and of course, download the file.

Here is a reference to the complete code:

dhruv479/recorder

Hope you liked the article. Clap and follow for more content.




Continue Learning