Skip to content

Instantly share code, notes, and snippets.

@karx
Created September 29, 2019 15:13
Show Gist options
  • Save karx/8afab00e44ffbe543e362fb43e39cfbe to your computer and use it in GitHub Desktop.
Save karx/8afab00e44ffbe543e362fb43e39cfbe to your computer and use it in GitHub Desktop.
HTML Canvas - Record and download video
var canvas = document.querySelector("canvas");
// Optional frames per second argument.
var stream = canvas.captureStream(25);
var recordedChunks = [];
console.log(stream);
var options = { mimeType: "video/webm; codecs=vp9" };
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.start();
function handleDataAvailable(event) {
console.log("data-available");
if (event.data.size > 0) {
recordedChunks.push(event.data);
console.log(recordedChunks);
download();
} else {
// ...
}
}
function download() {
var blob = new Blob(recordedChunks, {
type: 'video/webm'
});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
document.body.appendChild(a);
a.style = 'display: none';
a.href = url;
a.download = 'test.webm';
a.click();
window.URL.revokeObjectURL(url);
}
//for logging status
setInterval( (event) => {
console.log(mediaRecorder);
},1500);
// demo: to download after 9sec
setTimeout( (event) => {
console.log("stopping");
mediaRecorder.stop();
}, 9000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment