Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to create a screen recorder using javascript only

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>VScoder How to create screen recorder</title>
    <style>
        button{
            color:black;
            background-color:white;
            border: 2px solid black;
        }
        button:hover{
            color: white;
            background-color: black;
            border: 1px solid white;
        }
    </style>
</head>
<body>
   <button id="start">Start</button>
    <button id="stop">Stop</button>
    <script>
let start = document.getElementById('start'),
    stop  = document.getElementById('stop'),
    mediaRecorder;

start.addEventListener('click', async function(){
    let stream = await recordScreen();
    let mimeType = 'video/webm';
    mediaRecorder = createRecorder(stream, mimeType);
  
})

stop.addEventListener('click', function(){
    mediaRecorder.stop();
    
})

async function recordScreen() {
    return await navigator.mediaDevices.getDisplayMedia({
        audio: true, 
        video: { mediaSource: "screen"}
    });
}

function createRecorder (stream, mimeType) {
  
  let recordedChunks = []; 

  const mediaRecorder = new MediaRecorder(stream);

  mediaRecorder.ondataavailable = function (e) {
    if (e.data.size > 0) {
      recordedChunks.push(e.data);
    }  
  };
  mediaRecorder.onstop = function () {
     saveFile(recordedChunks);
     recordedChunks = [];
  };
  mediaRecorder.start(200); 
  return mediaRecorder;
}

function saveFile(recordedChunks){

   const blob = new Blob(recordedChunks, {
      type: 'video/webm'
    });
    let filename = window.prompt('Enter file name'),
        downloadLink = document.createElement('a');
    downloadLink.href = URL.createObjectURL(blob);
    downloadLink.download = `${filename}.webm`;

    document.body.appendChild(downloadLink);
    downloadLink.click();
    URL.revokeObjectURL(blob);
    document.body.removeChild(downloadLink);
}
</script>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: favicon in next js not working 
Javascript :: Unable to resolve module `@react-native-community/toolbar-android 
Javascript :: jquery if screen size 
Javascript :: html get selected option javascript 
Javascript :: get unique values from array of objects javascript 
Javascript :: install node js lts ubuntu 18.04 
Javascript :: how to get domain name in react 
Javascript :: how to see if a web site is useing react 
Javascript :: vue js footer copyright date automatically 
Javascript :: jquery remove all tr from table 
Javascript :: jmeter mac 
Javascript :: string to int javascript 
Javascript :: js get alphabet as array 
Javascript :: check if a string contains another string js 
Javascript :: how to check element is in viewport 
Javascript :: random number generator in hjs 
Javascript :: prepend element jquery 
Javascript :: javascript remove last character from string 
Javascript :: neffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 
Javascript :: jquery serialize form data and submit 
Javascript :: requirenativecomponent rnsscreen was not found in the uimanager 
Javascript :: express send raw html 
Javascript :: js array to comma separated list 
Javascript :: discord js delete message after time 
Javascript :: create slug in javascript 
Javascript :: regex cpf javascript 
Javascript :: datatable loading 
Javascript :: javascript initiate array all zero 
Javascript :: FlatList Warning: Each child in a list should have a unique "key" prop. 
Javascript :: remove attribute javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =