Created
November 27, 2024 06:20
-
-
Save coryvirok/797c3ed27550e92fec42512014791597 to your computer and use it in GitHub Desktop.
Download local blob data url 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
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