Create a screen recording function with JS

Create a screen recording function with JS

OBS studio is cool, but JavaScript is cooler. Now, let’s create our own screen recording function with JavaScript .

First, create an HTML file that contains a record button and a play tag.

The content is as follows:

<!DOCTYPE html> 
<html> 
  <head> 
    <title>Parcel Sandbox</title> 
    <meta charset="UTF-8" /> 
  </head> 
  <body> 
    <video class="video" width="600px" controls></video> 
    <button class="record-btn">record</button> 
 
    <script src="./index.js"></script> 
  </body> 
</html> 

Then create index.js ,

Listen for button clicks:

let btn = document.querySelector(".record-btn"); 
 
btn.addEventListener("click", function () { 
  console.log("hello"); 
}); 

Open the html file in the browser and click the button. We can see hello printed in the console.

Now remove the print and replace it with the following:

let btn = document.querySelector(".record-btn"); 
 
btn.addEventListener("click", async function () { 
  let stream = await navigator.mediaDevices.getDisplayMedia({ 
    video: true 
  }); 
}); 

Now click the button and a screen selection box will pop up:

Since I am using two screens now, two options will appear.

Now you might think that selecting a screen and clicking Share will start the recording. No, this is more complicated than we think. We are going to use MediaRecorder to record our video.

let btn = document.querySelector(".record-btn") 
 
btn.addEventListener("click", async function () { 
  let stream = await navigator.mediaDevices.getDisplayMedia({ 
    video: true 
  }) 
 
  // Need better browser support const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")  
             ? "video/webm; codecs=vp9"  
             : "video/webm" 
    let mediaRecorder = new MediaRecorder(stream, { 
        mimeType: mime 
    }) 
    //Must start mediaRecorder.start() manually 
}) 

When our screen is recorded, mediaRecorder will provide us with chunks of data, which we need to store in a variable.

let btn = document.querySelector(".record-btn") 
 
btn.addEventListener("click", async function () { 
  let stream = await navigator.mediaDevices.getDisplayMedia({ 
    video: true 
  }) 
 
  // Need better browser support const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")  
             ? "video/webm; codecs=vp9"  
             : "video/webm" 
    let mediaRecorder = new MediaRecorder(stream, { 
        mimeType: mime 
    }) 
 
    let chunks = [] 
    mediaRecorder.addEventListener('dataavailable', function(e) { 
        chunks.push(e.data) 
    }) 
 
    //Must start mediaRecorder.start() manually 
}) 

Now, when we click the stop sharing button, we want to play the recorded video in our video element, we can do it like this:

let btn = document.querySelector(".record-btn") 
 
btn.addEventListener("click", async function () { 
  let stream = await navigator.mediaDevices.getDisplayMedia({ 
    video: true 
  }) 
 
  // Need better browser support const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")  
             ? "video/webm; codecs=vp9"  
             : "video/webm" 
    let mediaRecorder = new MediaRecorder(stream, { 
        mimeType: mime 
    }) 
 
    let chunks = [] 
    mediaRecorder.addEventListener('dataavailable', function(e) { 
        chunks.push(e.data) 
    }) 
 
     mediaRecorder.addEventListener('stop', function(){ 
          let blob = new Blob(chunks, { 
              type: chunks[0].type 
          }) 
 
          let video = document.querySelector(".video") 
          video.src = URL.createObjectURL(blob) 
      }) 
 
 
    //Must start mediaRecorder.start() manually 
}) 

Now it is basically done and can be polished, such as automatically downloading the recorded video.

You can do this:

let btn = document.querySelector(".record-btn") 
 
btn.addEventListener("click", async function () { 
  let stream = await navigator.mediaDevices.getDisplayMedia({ 
    video: true 
  }) 
 
  // Need better browser support const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")  
             ? "video/webm; codecs=vp9"  
             : "video/webm" 
    let mediaRecorder = new MediaRecorder(stream, { 
        mimeType: mime 
    }) 
 
    let chunks = [] 
    mediaRecorder.addEventListener('dataavailable', function(e) { 
        chunks.push(e.data) 
    }) 
 
   mediaRecorder.addEventListener('stop', function(){ 
      let blob = new Blob(chunks, { 
          type: chunks[0].type 
      }) 
      let url = URL.createObjectURL(blob) 
 
      let video = document.querySelector("video") 
      video.src = url 
 
      let a = document.createElement('a') 
      a.href = url 
      a.download = 'video.webm' 
      a.click() 
  }) 
 
 
    //Must start mediaRecorder.start() manually 
}) 

Now, the most basic recording function is complete, let's try it out!!

This is the end of this article about using JS to create a screen recording function. For more information about using JS to create a screen recording function, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of JS tips for creating or filling arrays of arbitrary length

<<:  How to add configuration options to Discuz! Forum

>>:  A brief discussion on how to set CSS position absolute relative to the parent element

Recommend

How to build lnmp environment in docker

Create a project directory mkdir php Create the f...

How to clear the cache after using keep-alive in vue

What is keepalive? In normal development, some co...

Example of adding music video to HTML page

1. Video tag Supports automatic playback in Firef...

Two solutions for automatically adding 0 to js regular format date and time

Table of contents background Solution 1 Ideas: Co...

Basic ideas for finding errors in Web front-end development

WEB development mainly consists of two interactio...

Share 16 burning flame effect English fonts treasure trove

We live in a visual world and are surrounded by m...

Mysql splits string into array through stored procedure

To split a string into an array, you need to use ...

Native JS to achieve directory scrolling effects

Here is a text scrolling effect implemented with ...

How to deploy Confluence and jira-software in Docker

version: centos==7.2 jdk==1.8 confluence==6.15.4 ...

Conditional comments to determine the browser (IE series)

<!--[if IE 6]> Only IE6 can recognize <![...

...

js returns to the previous page and refreshes the code

1. Javascript returns to the previous page history...

Implementation of single process control of Linux C background service program

introduce Usually a background server program mus...

JavaScript to implement search data display

This article shares the data display code for Jav...

6 solutions to IDEA's inability to connect to the MySQL database

This article mainly introduces 6 solutions to the...