-
Notifications
You must be signed in to change notification settings - Fork 3
/
remux.html
102 lines (94 loc) · 2.31 KB
/
remux.html
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
<html>
<head></head>
<body>
<div>
<input style="width:500px" placeholder="ts url" /> <button>load</button>
</div>
<div>
<input type="file" id="file" />
</div>
</body>
<script>
const input = document.querySelector('input');
const button = document.querySelector('button');
const fileEle = document.querySelector('#file');
const worker = new Worker('./remux-worker.js');
let url;
input.addEventListener('change', e => {
url = e.target.value;
});
button.addEventListener('click', e => {
if (!url) return;
load(url);
});
fileEle.addEventListener('change', e => {
let file = e.target.files[0];
let reader = new FileReader();
reader.onload = () => {
worker.postMessage(
{
type: 'remux',
buffer: reader.result
},
[reader.result]
);
};
reader.readAsArrayBuffer(file);
e.target.value = '';
});
worker.addEventListener('message', e => {
const { data, type } = e.data;
switch (type) {
case 'wasmInit':
worker.postMessage({ type: 'malloc' });
break;
case 'mp4':
play(data);
break;
case 'error':
alert('Error: ' + data);
throw new Error(data);
break;
}
});
function play(buffer) {
let video = document.querySelector('video');
if (!video) {
video = document.createElement('video');
video.style.width = '600px';
document.body.appendChild(video);
}
if (video.src) {
URL.revokeObjectURL(video.src);
video.src = null;
video.load();
}
video.src = URL.createObjectURL(
new Blob([buffer], { type: 'video/mp4' })
);
video.play();
}
function load(url) {
fetch(url)
.then(res => {
if (res.status >= 200 && res.status < 300) {
return res.arrayBuffer();
}
throw res.statusText;
})
.then(buffer => {
worker.postMessage(
{
type: 'remux',
buffer
},
[buffer]
);
})
.catch(e => {
console.log('Error:', e);
});
}
window.load = load;
</script>
</html>