JSFiddle - React, Tailwind, and code Playground
HTML
<video id="vid"></video>
JavaScript
navigator.mediaDevices.getUserMedia({video: true}).then(stream => {
window.stream = stream;
vid.srcObject = stream;
vid.play();
recordSegments(stream);
recordFull(stream);
});
const segments = [];
function recordSegments(stream){
let int = setInterval(()=>{
if(segments.length >= 10){
clearInterval(int);
stream.getTracks().forEach(t=>t.stop());
return;
}
const chunks = [];
const rec = new MediaRecorder(stream);
rec.ondataavailable = e => chunks.push(e.data);
rec.onstop = e => segments.push(new Blob(chunks));
rec.start();
setTimeout(()=>rec.stop(), 1000);
}, 1000);
}
function recordFull(stream){
const chunks = [];
const rec = new MediaRecorder(stream);
rec.ondataavailable = e => chunks.push(e.data);
rec.onstop = e => exportAll(new Blob(chunks));
rec.start();
setTimeout(()=>rec.stop(), 10000);
}
function exportAll(full){
vid.remove();
segments.unshift(full)
segments.forEach(blob=>{
const vid = document.createElement('video');
vid.src = URL.createObjectURL(blob);
vid.controls = true;
document.body.appendChild(vid);
});
}