Skip to content

Instantly share code, notes, and snippets.

@coryvirok
Created November 27, 2024 06:20
Show Gist options
  • Save coryvirok/797c3ed27550e92fec42512014791597 to your computer and use it in GitHub Desktop.
Save coryvirok/797c3ed27550e92fec42512014791597 to your computer and use it in GitHub Desktop.
Download local blob data url video
async function downloadProtectedVideo(videoElement, filename = 'video.mp4') {
try {
// Create a MediaRecorder to capture the video stream
const stream = videoElement.captureStream();
const mediaRecorder = new MediaRecorder(stream, {
mimeType: 'video/webm;codecs=vp9'
});
const chunks = [];
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) {
chunks.push(e.data);
}
};
mediaRecorder.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = filename || 'recorded_video.mp4';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
};
// Start recording
videoElement.currentTime = 0; // Reset to beginning
await videoElement.play();
mediaRecorder.start();
// Stop recording when video ends
videoElement.addEventListener('ended', () => {
mediaRecorder.stop();
});
// Or you can manually stop it after a specific duration:
// setTimeout(() => mediaRecorder.stop(), durationInMilliseconds);
} catch (error) {
console.error('Failed to download video:', error);
}
}
const video = document.querySelector('video')
await downloadProtectedVideo(video)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment