-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgif.js
93 lines (78 loc) · 2.86 KB
/
gif.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import AnimatedGif from 'animated_gif/dist/Animated_GIF'
import toBlob from 'data-uri-to-blob'
export async function videoToGif({ videoElem, numFrames }) {
const frameDurationMs = (videoElem.duration * 1000) / numFrames
const uriData = getSrcUri(videoElem.src)
const gifCreator = new AnimatedGif({ sampleInterval: 1 })
const recordingElem = document.createElement('video')
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
gifCreator.setSize(videoElem.videoWidth, videoElem.videoHeight)
gifCreator.setDelay(frameDurationMs)
canvas.width = videoElem.videoWidth
canvas.height = videoElem.videoHeight
try {
const onLoaded = listenOnce(recordingElem, 'loadeddata')
recordingElem.src = uriData.uri
recordingElem.load()
// Firefox Mobile doesn't like to load videos unless it *really* has to, so we play it to get
// things started there
recordingElem.play()
await onLoaded
for (let frame = 0; frame < numFrames; frame++) {
await seek(recordingElem, frame * (frameDurationMs / 1000))
context.drawImage(recordingElem, 0, 0)
gifCreator.addFrameImageData(context.getImageData(0, 0, canvas.width, canvas.height))
}
const blob = await new Promise(resolve => {
gifCreator.getBlobGIF(resolve)
})
return blob
} finally {
uriData.cleanup()
gifCreator.destroy()
delete recordingElem.src
}
}
function getSrcUri(videoElemSrc) {
if (/^data:/.test(videoElemSrc)) {
// Work around Firefox not considering data URI's "origin-clean" (meaning we can't draw from the
// data URI video to our canvas and still be able to call getImageData). Object URIs count as
// origin-clean correctly, however, so we construct one of those
const srcBlob = toBlob(videoElemSrc)
const srcUri = window.URL.createObjectURL(srcBlob)
return {
uri: srcUri,
cleanup() {
window.URL.revokeObjectURL(srcUri)
},
}
}
return { uri: videoElemSrc, cleanup: () => {} }
}
// Returns a promise that will resolve when an event occurs, removing the
// listener once it does. An error listener will also be attached with similar
// semantics (rejecting instead of resolving).
function listenOnce(elem, event) {
return new Promise((resolve, reject) => {
// Assign a value because ESLint isn't happy otherwise? No idea why
let errorCb = () => {}
const cb = () => {
elem.removeEventListener('seeked', cb)
elem.removeEventListener('error', errorCb)
resolve()
}
errorCb = err => {
elem.removeEventListener(event, cb)
elem.removeEventListener('error', errorCb)
reject(err)
}
elem.addEventListener(event, cb)
elem.addEventListener('error', errorCb)
})
}
async function seek(videoElem, timeSeconds) {
const onSeek = listenOnce(videoElem, 'seeked')
videoElem.currentTime = timeSeconds
await onSeek
}