-
Notifications
You must be signed in to change notification settings - Fork 11
/
transcode.js
51 lines (44 loc) · 1.26 KB
/
transcode.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
const CSS_CENTERED = 'position:fixed;top:50%;left:50%;transform:translate(-50%, -50%);';
let spinner = null;
let player = null;
const { createFFmpeg, fetchFile } = FFmpeg;
const ffmpeg = createFFmpeg({
log: true,
corePath: chrome.runtime.getURL('vendor/ffmpeg-core.js'),
});
const showPlayer = (data) => {
player = document.createElement('video');
player.style = CSS_CENTERED;
player.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
player.controls = true;
document.body.append(player);
}
const hidePlayer = () => {
if (player !== null) {
palyer.remove();
}
}
const showSpinner = () => {
spinner = document.createElement('img');
spinner.src = chrome.runtime.getURL('assets/spinner.gif');
spinner.style = CSS_CENTERED;
document.body.append(spinner);
}
const hideSpinner = () => {
if (spinner !== null) {
spinner.remove();
}
}
const transcode = async (file) => {
hidePlayer();
showSpinner();
const name = 'video';
if (! ffmpeg.isLoaded()){
await ffmpeg.load();
}
ffmpeg.FS('writeFile', name, await fetchFile(file));
await ffmpeg.run('-i', name, 'output.mp4');
const data = ffmpeg.FS('readFile', 'output.mp4');
hideSpinner();
showPlayer(data);
}