I have successfully managed to extract the video from the canvas, but now I need to combine it with an audio stream. After researching a bit, I discovered that I need to use the AudioDestinationNode object in some way. I attempted a couple of methods, but so far I have been unsuccessful. Below is the code snippet that only captures the video. I now need to add the audio merging functionality.
var canvas = document.querySelector("canvas");
var ctx = canvas.getContext("2d");
function download(content) {
element.click();
document.body.removeChild(element);
}
function roll(){
var colors = ["red", "blue", "yellow", "orange", "black", "white", "green"];
function draw (){
ctx.fillStyle = colors[Math.floor(Math.random() * colors.length)];
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
draw();
var videoStream = canvas.captureStream(30);
var mediaRecorder = new MediaRecorder(videoStream);
var chunks = [];
mediaRecorder.ondataavailable = function(e) {
chunks.push(e.data);
};
mediaRecorder.onstop = function(e) {
var blob = new Blob(chunks, { 'type' : 'video/mp4' });
chunks = [];
var videoURL = URL.createObjectURL(blob);
var tag = document.createElement('a');
tag.href = videoURL;
tag.download = 'sample.mp4';
document.body.appendChild(tag);
tag.click();
document.body.removeChild(tag);
};
mediaRecorder.ondataavailable = function(e) {
chunks.push(e.data);
};
mediaRecorder.start();
setInterval(draw, 300);
setTimeout(function (){ mediaRecorder.stop(); }, 5000);
}
<html>
<canvas width="300" height="300"></canvas>
<button onclick="roll()">Get</button>
</html>