Skip to content

Instantly share code, notes, and snippets.

@skortchmark9
Created December 23, 2018 03:56
Show Gist options
  • Save skortchmark9/5a44c49d22a4b0402b3a247d2cfb93b4 to your computer and use it in GitHub Desktop.
Save skortchmark9/5a44c49d22a4b0402b3a247d2cfb93b4 to your computer and use it in GitHub Desktop.
async function screenCap(duration) {
function _startScreenCapture() {
if (navigator.getDisplayMedia) {
return navigator.getDisplayMedia({video: true});
} else {
return navigator.mediaDevices.getUserMedia({video: {mediaSource: 'screen'}});
}
}
const stream = await _startScreenCapture();
const downloadLink = document.createElement('a');
downloadLink.textContent = 'Screen Recording';
downloadLink.style = 'position: absolute; bottom: 0; left: 0;';
document.body.appendChild(downloadLink);
const videoChunks = [];
const mediaRecorder = new MediaRecorder(stream, {mimeType: 'video/webm'});
mediaRecorder.addEventListener('dataavailable', event => {
if (event.data && event.data.size > 0) {
videoChunks.push(event.data);
}
});
mediaRecorder.start(10);
const stopRecording = () => {
mediaRecorder.stop();
stream.getTracks().forEach(track => track.stop());
const recordingUrl = window.URL.createObjectURL(new Blob(videoChunks, {type: 'video/webm'}));
downloadLink.download = 'screen-recording.webm';
downloadLink.href = recordingUrl;
};
setTimeout(stopRecording, duration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment