-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
242 lines (213 loc) · 9.25 KB
/
index.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web-Demuxer: Demux media files in the browser using WebAssembly</title>
<meta name="description" content="Demux media files in the browser using WebAssembly, designed for WebCodecs">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css" />
</head>
<body>
<header class="container">
<hgroup>
<h1>Web-Demuxer</h1>
<p>Demux media files in the browser using WebAssembly, designed for WebCodecs.</p>
</hgroup>
<nav>
<ul>
<li>
<details class="dropdown">
<summary role="button" class="secondary">Examples</summary>
<ul>
<li><a href="#example-seek">Seek Video Frame</a></li>
<li><a href="#example-play">Play Video</a></li>
<li><a href="#example-get-media-info">Get Media Info</a></li>
<li><a href="#example-get-all-video-packets">Get All Video Packets</a></li>
</ul>
</details>
</li>
<li>
<details class="dropdown">
<summary role="button" class="secondary">Docs</summary>
<ul>
<li><a href="https://github.com/ForeverSc/web-demuxer/blob/main/README.md#install"
target="_blank">English</a></li>
<li><a href="https://github.com/ForeverSc/web-demuxer/blob/main/README_CN.md#install"
target="_blank">中文</a></li>
</ul>
</details>
</li>
</ul>
</nav>
</header>
<main class="container">
<section>
<h2>Purpose</h2>
<p>WebCodecs only provide the ability to decode, but not to demux. mp4box.js is cool, but it only supports mp4
demux. Web-Demuxer aims to support as many multimedia formats as possible at once.</p>
</section>
<section>
<h2>Features</h2>
<ul>
<li>🪄 Specifically designed for WebCodecs, the API is very friendly for WebCodecs development, you can easily
realize the media file demux.</li>
<li>📦 One-time support for a variety of media formats, such as mov/mp4/mkv/webm/flv/m4v/wmv/avi, etc.</li>
<li>🧩 Support for customized packaging, you can adjust the configuration, packaged in a specified format
demuxer</li>
</ul>
</section>
<section>
<h2>Examples</h2>
<section id="example-seek">
<hgroup>
<h3>Seek Video Frame</h3>
<p>Select a video file and enter the point in time you want to seek (default is 0)</p>
</hgroup>
<card>
<fieldset role="group">
<input type="file" id="example-seek-file">
<input type="number" id="example-seek-time" placeholder="Enter the seek time point in seconds">
<button id="example-seek-btn">Seek</button>
</fieldset>
<canvas id="example-seek-canvas" width="800px" height="450px"></canvas>
</card>
<section id="example-play">
<hgroup>
<h3>Play Video</h3>
<p>Select a video file and enter the playback start and playback end points (default playback from beginning
to end)</p>
</hgroup>
<card>
<fieldset role="group">
<input type="file" id="example-play-file">
<input type="number" id="example-play-start-time" placeholder="Enter start time point in seconds">
<input type="number" id="example-play-end-time" placeholder="Enter end time point in seconds">
<button id="example-play-btn">Play</button>
</fieldset>
<canvas id="example-play-canvas" width="800px" height="450px"></canvas>
</card>
</section>
<section id="example-get-media-info">
<hgroup>
<h3>Get Media Info</h3>
<p>Select a video file and get media info, open devtools to see console output</p>
</hgroup>
<card>
<fieldset role="group">
<input type="file" id="example-get-media-info-file">
<button id="example-get-media-info-btn">Get</button>
</fieldset>
</card>
</section>
<section id="example-get-all-video-packets">
<hgroup>
<h3>Get All Video Packets</h3>
<p>Select a video file and get all video packets, open devtools to see console output</p>
</hgroup>
<card>
<fieldset role="group">
<input type="file" id="example-get-all-video-packets-file">
<button id="example-get-all-video-packets-btn">Get</button>
</fieldset>
</card>
</section>
</section>
</section>
</main>
<script type="module">
(async () => {
const isDEV = import.meta.env && import.meta.env.DEV
const { WebDemuxer } = isDEV ? await import('./src') : await import('https://cdn.jsdelivr.net/npm/web-demuxer/+esm')
const demuxer = new WebDemuxer({
wasmLoaderPath: isDEV ? `${window.location.origin}/src/lib/ffmpeg.js` : "https://cdn.jsdelivr.net/npm/web-demuxer@latest/dist/wasm-files/ffmpeg.min.js",
})
document.getElementById('example-seek-btn').addEventListener('click', async (e) => {
const file = document.getElementById('example-seek-file').files[0]
const seekTime = document.getElementById('example-seek-time').value || 0
const canvas = document.getElementById("example-seek-canvas");
const ctx = canvas.getContext("2d");
await demuxer.load(file);
const videoDecoderConfig = await demuxer.getVideoDecoderConfig();
const videoChunk = await demuxer.seekEncodedVideoChunk(seekTime);
const decoder = new VideoDecoder({
output: (frame) => {
const scale = Math.min(canvas.width / frame.displayWidth, canvas.height / frame.displayHeight);
ctx.drawImage(frame, 0, 0, frame.displayWidth * scale, frame.displayHeight * scale);
frame.close();
},
error: (e) => {
console.error('video decoder error:', e);
}
});
decoder.configure(videoDecoderConfig);
decoder.decode(videoChunk);
decoder.flush();
})
document.getElementById('example-play-btn').addEventListener('click', async () => {
const file = document.getElementById('example-play-file').files[0];
const startTime = document.getElementById('example-play-start-time').value || 0;
const endTime = document.getElementById('example-play-end-time').value || 0;
const canvas = document.getElementById("example-play-canvas");
const ctx = canvas.getContext("2d");
await demuxer.load(file);
const videoDecoderConfig = await demuxer.getVideoDecoderConfig();
const decoder = new VideoDecoder({
output: (frame) => {
const scale = Math.min(canvas.width / frame.displayWidth, canvas.height / frame.displayHeight);
ctx.drawImage(frame, 0, 0, frame.displayWidth * scale, frame.displayHeight * scale);
frame.close();
},
error: (e) => {
console.error('video decoder error:', e);
}
})
decoder.configure(videoDecoderConfig)
const reader = demuxer.readAVPacket(startTime, endTime).getReader()
reader.read().then(async function processPacket({ done, value }) {
if (done) {
decoder.flush();
console.log('read finished');
return
}
const videoChunk = demuxer.genEncodedVideoChunk(value);
decoder.decode(videoChunk);
await wait(1000 / 30) // 30fps, wait for frame decode and draw
return reader.read().then(processPacket)
});
});
document.getElementById('example-get-all-video-packets-btn').addEventListener('click', async () => {
const file = document.getElementById('example-get-all-video-packets-file').files[0];
async function getAllPackets(file) {
await demuxer.load(file)
return new Promise((resolve) => {
const videoPackets = []
const reader = demuxer.readAVPacket().getReader()
reader.read().then(async function processAVPacket({ done, value }) {
if (done) {
resolve(videoPackets) // all packets read finished
return
}
videoPackets.push(value) // get one video packet
// and you can generate video chunk for video decoder
// const videoChunk = demuxer.genEncodedVideoChunk(value)
return reader.read().then(processAVPacket)
})
})
}
console.log('all video packets:', await getAllPackets(file));
});
document.getElementById('example-get-media-info-btn').addEventListener('click', async () => {
const file = document.getElementById('example-get-media-info-file').files[0];
await demuxer.load(file);
const mediaInfo = await demuxer.getMediaInfo();
console.log('media info: ', mediaInfo);
});
function wait(time) {
return new Promise((resolve) => {
setTimeout(resolve, time)
});
}
})();
</script>
</body>
</html>