-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
166 lines (146 loc) · 3.54 KB
/
index.mjs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const scale = (sw, sh, dw, dh) => {
let ow, oh;
if (!dw && !dh) {
ow = sw;
oh = sh;
} else if (dw) {
ow = dw;
oh = Math.floor(dw / sw * sh);
} else if (dh) {
oh = dh;
ow = Math.floor(dh / sh * sw);
} else {
oh = dw;
ow = dh;
}
return { width: ow, height: oh };
};
const once = (fn) => {
let flag = false;
let result;
return (...args) => {
if (flag) {
return result;
}
return result = fn(...args);
};
};
/**
* Create thumbnails of the video
* @param {string} videoSrc video source url
* @param {number} n the number of thumbnails
* @param {object} options some options
*/
export const createVideoThumbnails = (videoSrc, options = {}) => {
const video = document.createElement('video');
if (options.width) {
video.width = options.width;
}
if (options.height) {
video.height = options.height;
}
let duration = 0;
let hasError = false;
let loaded = false;
let destoryed = false;
const seek = (time) => {
if (video.fastSeek) {
video.fastSeek(time);
} else {
video.currentTime = time;
}
};
const init = () => {
video.src = videoSrc;
};
const handleInit = () => {
duration = video.duration;
loaded = true;
};
const handleFailed = () => {
hasError = true;
loaded = true;
};
video.addEventListener('loadedmetadata', handleInit);
video.addEventListener('error', handleFailed);
return {
make: (n, cb) => {
if (destoryed) {
return;
}
const callback = once(cb);
const result = [];
result.destory = () => {
result.forEach(({ url }) => {
URL.revokeObjectURL(url);
});
};
let step = 0;
let current = 0;
let iw = 0;
let ih = 0;
let canvas;
let context;
const next = () => {
current += step;
if (current < duration) {
seek(current);
} else {
end();
callback(null, result);
}
};
const handleSeek = () => {
context.drawImage(video, 0, 0, iw, ih);
canvas.convertToBlob().then((blob) => {
result.push({
time: current,
url: URL.createObjectURL(blob),
});
next();
});
};
const handleLoad = () => {
step = video.duration / n;
const s = scale(video.videoWidth, video.videoHeight, options.width, options.height);
iw = s.width;
ih = s.height;
canvas = new OffscreenCanvas(iw, ih);
context = canvas.getContext('2d');
video.removeEventListener('loadedmetadata', handleLoad);
seek(0);
};
const handleError = () => {
video.removeEventListener('error', handleError);
const error = new Error(video.error.message);
error.code = video.error.code;
callback(error);
};
const start = () => {
video.addEventListener('seeked', handleSeek);
if (hasError) {
handleError();
} else {
video.addEventListener('error', handleError);
}
if (loaded) {
handleLoad();
} else {
video.addEventListener('loadedmetadata', handleLoad);
init();
}
};
const end = () => {
video.removeEventListener('seeked', handleSeek);
};
start();
},
destory: () => {
video.removeEventListener('loadedmetadata', handleInit);
video.removeAttribute('src');
video.load();
destoryed = true;
},
};
};
export default createVideoThumbnails;