josefandersson revised this gist . Go to revision
1 file changed, 37 insertions
download_video_element.js(file created)
@@ -0,0 +1,37 @@ | |||
1 | + | // Record a video element and download the recording. Workaround download method for blob videos that cannot be downloaded directly. | |
2 | + | function dl(videoElement) { | |
3 | + | let recordedChunks = []; | |
4 | + | ||
5 | + | const stream = videoElement.captureStream(); | |
6 | + | ||
7 | + | mediaRecorder = new MediaRecorder(stream, { mimeType: "video/mp4" }); | |
8 | + | ||
9 | + | mediaRecorder.ondataavailable = (event) => { | |
10 | + | if (event.data.size > 0) { | |
11 | + | recordedChunks.push(event.data); | |
12 | + | } | |
13 | + | }; | |
14 | + | ||
15 | + | mediaRecorder.onstop = () => { | |
16 | + | const blob = new Blob(recordedChunks, { type: "video/mp4" }); | |
17 | + | const url = URL.createObjectURL(blob); | |
18 | + | const a = document.createElement("a"); | |
19 | + | a.href = url; | |
20 | + | a.download = `${Date.now()}.mp4`; | |
21 | + | document.body.appendChild(a); | |
22 | + | a.click(); | |
23 | + | URL.revokeObjectURL(url); | |
24 | + | }; | |
25 | + | ||
26 | + | videoElement.onplay = () => { | |
27 | + | mediaRecorder.start(); | |
28 | + | }; | |
29 | + | ||
30 | + | videoElement.onended = () => { | |
31 | + | mediaRecorder.stop(); | |
32 | + | }; | |
33 | + | ||
34 | + | videoElement.loop = false; | |
35 | + | videoElement.currentTime = 0; | |
36 | + | videoElement.play(); | |
37 | + | } |
Newer
Older