Created
September 29, 2019 15:13
-
-
Save karx/8afab00e44ffbe543e362fb43e39cfbe to your computer and use it in GitHub Desktop.
HTML Canvas - Record and download video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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