Skip to content

Instantly share code, notes, and snippets.

@PaulKinlan
Last active August 9, 2022 11:55
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save PaulKinlan/def79b32a6cfec88f7b61e531523c743 to your computer and use it in GitHub Desktop.
Save PaulKinlan/def79b32a6cfec88f7b61e531523c743 to your computer and use it in GitHub Desktop.
Screen recorder in JS
(function() {
let canvas = document.querySelector('canvas');
// Optional frames per second argument.
let stream = canvas.captureStream(25);
var options = {mimeType: 'video/webm; codecs=vp9'};
let recorder = new MediaRecorder(stream, options);
let blobs = [];
function download(blob) {
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'test.webm';
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
recorder.ondataavailable = e => { console.log(e.data); if (e.data && e.data.size > 0) blobs.push(e.data)};
recorder.onstop = (e) => download(new Blob(blobs, {type: 'video/webm'}));
recorder.start(10); // collect 10ms chunks of data
// Record for 10 seconds.
setTimeout(()=> recorder.stop(), 10000);
})();
@PaulKinlan
Copy link
Author

Scoping was annoying me so I made it an IEF

@PaulKinlan
Copy link
Author

, , < -- what an ejit.

@harkal18
Copy link

harkal18 commented Dec 7, 2020

*edit lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment