Skip to content

Commit eab456a

Browse files
committed
docs: update new api details
1 parent 31a7175 commit eab456a

File tree

2 files changed

+338
-233
lines changed

2 files changed

+338
-233
lines changed

README.md

Lines changed: 169 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,48 @@
11
<h4 align="right"><strong>English</strong> | <a href="https://github.com/ForeverSc/web-demuxer/blob/main/README_CN.md">简体中文</a></h4>
22
<h1 align="center">Web-Demuxer</h1>
33
<p align="center">Demux media files in the browser using WebAssembly, designed for WebCodecs.</p>
4+
45
<div align="center">
56
<a href="https://www.npmjs.com/package/web-demuxer"><img src="https://img.shields.io/npm/v/web-demuxer" alt="version"></a>
67
<a href="https://www.npmjs.com/package/web-demuxer"><img src="https://img.shields.io/npm/dm/web-demuxer" alt="downloads"></a>
78
<a href="https://www.jsdelivr.com/package/npm/web-demuxer"><img src="https://data.jsdelivr.com/v1/package/npm/web-demuxer/badge" alt="hits"></a>
89
</div>
910

10-
## Purpose
11-
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.
11+
## Overview
12+
13+
WebCodecs provides decoding capabilities but lacks demuxing functionality. While mp4box.js is excellent for MP4 files, it only supports MP4 format. **Web-Demuxer** aims to support a wide range of multimedia formats in one package, specifically designed for seamless WebCodecs integration.
1214

1315
## Features
14-
- 🪄 Specifically designed for WebCodecs, the API is very friendly for WebCodecs development, you can easily realize the media file demux.
15-
- 📦 One-time support for a variety of media formats, such as mov/mp4/mkv/webm/flv/m4v/wmv/avi/ts, etc.
16-
- 🧩 Support for customized packaging, you can adjust the configuration, packaged in a specified format demuxer
1716

18-
## Install
19-
### NPM
17+
- 🪄 **WebCodecs-First Design** - API optimized for WebCodecs development with intuitive methods
18+
- 📦 **Multi-Format Support** - Handle mov/mp4/mkv/webm/flv/m4v/wmv/avi/ts and more formats
19+
- 🧩 **Customizable Build** - Configure and build demuxers for specific formats only
20+
- 🔧 **Rich Media Info** - Extract detailed metadata similar to ffprobe output
21+
22+
## Quick Start
23+
2024
```bash
2125
npm install web-demuxer
2226
```
2327

24-
### CDN
25-
```html
26-
<script type="module">
27-
import { WebDemuxer } from 'https://cdn.jsdelivr.net/npm/web-demuxer/+esm';
28-
</script>
29-
```
30-
31-
## Usage
3228
```typescript
33-
import { WebDemuxer } from "web-demuxer"
29+
import { WebDemuxer } from "web-demuxer";
3430

35-
const demuxer = new WebDemuxer({
36-
// ⚠️ Optional parameter for custom WASM file location
37-
// When omitted, it defaults to looking for WASM files in the same directory as the script file.
38-
// We recommend placing the WASM file (web-demuxer.wasm from npm package's dist/wasm-files/)
39-
// in your project's static directory (e.g., public folder).
40-
// Alternatively, you can specify a CDN path like this:
41-
wasmFilePath: "https://cdn.jsdelivr.net/npm/web-demuxer@latest/dist/wasm-files/web-demuxer.wasm",
42-
})
31+
const demuxer = new WebDemuxer();
4332

44-
// Take the example of seeking a video frame at a specified point in time
33+
// Example: Get video frame at specific time
4534
async function seek(file, time) {
46-
// 1. load video file
35+
// 1. Load video file
4736
await demuxer.load(file);
4837

49-
// 2. demux video file and generate WebCodecs needed VideoDecoderConfig and EncodedVideoChunk
38+
// 2. Demux video file and generate VideoDecoderConfig and EncodedVideoChunk required by WebCodecs
5039
const videoDecoderConfig = await demuxer.getDecoderConfig('video');
5140
const videoEncodedChunk = await demuxer.seek('video', time);
5241

53-
// 3. use WebCodecs to decode frame
42+
// 3. Decode video frame through WebCodecs
5443
const decoder = new VideoDecoder({
5544
output: (frame) => {
56-
// draw frame...
45+
// Render frame, e.g., using canvas drawImage
5746
frame.close();
5847
},
5948
error: (e) => {
@@ -67,61 +56,106 @@ async function seek(file, time) {
6756
}
6857
```
6958

70-
## Examples
71-
- [Seek Video Frame](https://foreversc.github.io/web-demuxer/#example-seek)[code](https://github.com/ForeverSc/web-demuxer/blob/main/index.html#L96)
72-
- [Play Video](https://foreversc.github.io/web-demuxer/#example-play)[code](https://github.com/ForeverSc/web-demuxer/blob/main/index.html#L123)
59+
## Installation
7360

74-
## API
75-
```typescript
76-
new WebDemuxer(options?: WebDemuxerOptions)
61+
### NPM
62+
```bash
63+
npm install web-demuxer
7764
```
78-
Creates a new instance of `WebDemuxer`.
79-
80-
Parameters:
81-
- `options`: Optional, configuration options.
82-
- `wasmFilePath`: Optional, customize the WASM file path, it defaults to looking for WASM files in the same directory as the script file.
8365

84-
```typescript
85-
load(source: File | string): Promise<void>
66+
### CDN
67+
```html
68+
<script type="module">
69+
import { WebDemuxer } from 'https://cdn.jsdelivr.net/npm/web-demuxer/+esm';
70+
</script>
8671
```
87-
Loads a file and waits for the wasm worker to finish loading. The subsequent methods can only be called after the `load` method has been successfully executed.
8872

89-
Parameters:
90-
- `source`: Required, support the `File` object or file URL to be processed.
73+
### WASM File Setup
74+
75+
**‼️ Important:** Place the WASM file in your static directory (e.g., `public/`) for proper loading.
9176

9277
```typescript
93-
getDecoderConfig<T extends MediaType>(type: T): Promise<MediaTypeToConfig[T]>
78+
const demuxer = new WebDemuxer({
79+
// Option 1: Use CDN
80+
wasmFilePath: "https://cdn.jsdelivr.net/npm/web-demuxer@latest/dist/wasm-files/web-demuxer.wasm",
81+
82+
// Option 2: Use local file
83+
// Copy dist/wasm-files/web-demuxer.wasm from npm package to public directory
84+
// You can use plugins like vite-plugin-static-copy to sync automatically
85+
// If JS and WASM are in the same public directory, wasmFilePath can be omitted
86+
// wasmFilePath: "/path/to/your/public/web-demuxer.wasm"
87+
});
9488
```
95-
Get decoder configuration for WebCodecs based on media type ('video' or 'audio'). Returns `VideoDecoderConfig` or `AudioDecoderConfig` that can be used directly with WebCodecs.
9689

97-
Parameters:
98-
- `type`: Required, media type ('video' or 'audio')
90+
## Live Examples
91+
- [Seek Video Frame](https://foreversc.github.io/web-demuxer/#example-seek) | [Source Code](https://github.com/bilibili/web-demuxer/blob/main/index.html#L131-L157)
92+
- [Play Video](https://foreversc.github.io/web-demuxer/#example-play) | [Source Code](https://github.com/bilibili/web-demuxer/blob/main/index.html#L159-L197)
9993

100-
```typescript
101-
seek<T extends MediaType>(type: T, time: number, seekFlag?: AVSeekFlag): Promise<MediaTypeToChunk[T]>
102-
```
103-
Seek and return encoded chunk for WebCodecs. Returns `EncodedVideoChunk` or `EncodedAudioChunk` based on media type.
94+
## API
10495

105-
Parameters:
106-
- `type`: Required, media type ('video' or 'audio')
107-
- `time`: Required, unit is s
108-
- `seekFlag`: Seek flag, default value is 1 (backward seek). See `AVSeekFlag` for details.
96+
### Constructor
10997

110-
```typescript
111-
read<T extends MediaType>(type: T, start?: number, end?: number, seekFlag?: AVSeekFlag): ReadableStream<MediaTypeToChunk[T]>
112-
```
113-
Read encoded chunks as a stream for WebCodecs. Returns `ReadableStream` of `EncodedVideoChunk` or `EncodedAudioChunk`.
98+
#### `new WebDemuxer(options?: WebDemuxerOptions)`
11499

115-
Parameters:
116-
- `type`: Required, media type ('video' or 'audio')
117-
- `start`: Optional, start time in seconds (default: 0)
118-
- `end`: Optional, end time in seconds (default: 0, read till end)
119-
- `seekFlag`: Optional, seek flag (default: AVSEEK_FLAG_BACKWARD)
100+
Creates a new WebDemuxer instance.
101+
102+
**Parameters:**
103+
- `options.wasmFilePath` (optional): Custom WASM file path. Defaults to looking for `web-demuxer.wasm` in the script directory.
104+
105+
### Core Methods
106+
107+
#### `load(source: File | string): Promise<void>`
108+
109+
Loads a media file and initializes the WASM worker.
110+
111+
**Parameters:**
112+
- `source`: File object or URL string
113+
114+
**Note:** All subsequent methods require successful `load()` execution.
115+
116+
#### `getDecoderConfig<T extends MediaType>(type: T): Promise<MediaTypeToConfig[T]>`
117+
118+
Gets WebCodecs decoder configuration.
119+
120+
**Parameters:**
121+
- `type`: `'video'` or `'audio'`
122+
123+
**Returns:** `VideoDecoderConfig` or `AudioDecoderConfig`
124+
125+
#### `seek<T extends MediaType>(type: T, time: number, seekFlag?: AVSeekFlag): Promise<MediaTypeToChunk[T]>`
126+
127+
Seeks to specific time and returns encoded chunk.
128+
129+
**Parameters:**
130+
- `type`: `'video'` or `'audio'`
131+
- `time`: Time in seconds
132+
- `seekFlag`: Seek direction (default: backward)
133+
134+
**Returns:** `EncodedVideoChunk` or `EncodedAudioChunk`
135+
136+
#### `read<T extends MediaType>(type: T, start?: number, end?: number, seekFlag?: AVSeekFlag): ReadableStream<MediaTypeToChunk[T]>`
137+
138+
Creates a stream of encoded chunks.
139+
140+
**Parameters:**
141+
- `type`: `'video'` or `'audio'`
142+
- `start`: Start time in seconds (default: 0)
143+
- `end`: End time in seconds (default: end of file)
144+
- `seekFlag`: Seek direction (default: backward)
145+
146+
**Returns:** `ReadableStream` of encoded chunks
147+
148+
### Media Information
149+
150+
#### `getMediaInfo(): Promise<WebMediaInfo>`
151+
152+
Extracts comprehensive media metadata (similar to ffprobe output).
153+
154+
**Returns:**
155+
156+
<details>
157+
<summary>📋 Example Response (Click to expand)</summary>
120158

121-
```typescript
122-
getMediaInfo(): Promise<WebMediaInfo> // 2.0 New
123-
```
124-
Get the media information of a file, the output is referenced from `ffprobe`
125159
```json
126160
{
127161
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
@@ -151,7 +185,7 @@ Get the media information of a file, the output is referenced from `ffprobe`
151185
"sample_fmt": "u8",
152186
"bit_rate": "6385079",
153187
"extradata_size": 36,
154-
"extradata": Uint8Array,
188+
"extradata": "Uint8Array",
155189
"r_frame_rate": "30/1",
156190
"avg_frame_rate": "30/1",
157191
"sample_aspect_ratio": "N/A",
@@ -184,7 +218,7 @@ Get the media information of a file, the output is referenced from `ffprobe`
184218
"sample_fmt": "",
185219
"bit_rate": "124878",
186220
"extradata_size": 2,
187-
"extradata": Uint8Array,
221+
"extradata": "Uint8Array",
188222
"r_frame_rate": "0/0",
189223
"avg_frame_rate": "0/0",
190224
"sample_aspect_ratio": "N/A",
@@ -203,67 +237,85 @@ Get the media information of a file, the output is referenced from `ffprobe`
203237
]
204238
}
205239
```
240+
</details>
206241

207-
```typescript
208-
getMediaStream(type: MediaType, streamIndex?: number): Promise<WebAVStream>
209-
```
210-
Get information about a media stream (video or audio) in the media file.
242+
#### `getMediaStream(type: MediaType, streamIndex?: number): Promise<WebAVStream>`
211243

212-
Parameters:
213-
- `type`: Required, the type of media stream ('video' or 'audio')
214-
- `streamIndex`: Optional, the index of the media stream
244+
Gets information about a specific media stream.
215245

216-
```typescript
217-
seekMediaPacket(type: MediaType, time: number, seekFlag?: AVSeekFlag): Promise<WebAVPacket>
218-
```
219-
Retrieves the media data at the specified time point.
246+
**Parameters:**
247+
- `type`: `'video'` or `'audio'`
248+
- `streamIndex`: Stream index (optional)
220249

221-
Parameters:
222-
- `type`: Required, the type of media ('video' or 'audio')
223-
- `time`: Required, in seconds
224-
- `seekFlag`: Optional, seek flag, defaults to 1 (backward seek). See `AVSeekFlag` for details.
250+
### Low-Level Packet Access
225251

226-
```typescript
227-
readMediaPacket(type: MediaType, start?: number, end?: number, seekFlag?: AVSeekFlag): ReadableStream<WebAVPacket>
228-
```
229-
Returns a `ReadableStream` for streaming media packet data.
252+
#### `seekMediaPacket(type: MediaType, time: number, seekFlag?: AVSeekFlag): Promise<WebAVPacket>`
230253

231-
Parameters:
232-
- `type`: Required, the type of media ('video' or 'audio')
233-
- `start`: Optional, start time in seconds (default: 0)
234-
- `end`: Optional, end time in seconds (default: 0, read till end)
235-
- `seekFlag`: Optional, seek flag (default: AVSEEK_FLAG_BACKWARD)
254+
Gets raw media packet at specific time.
236255

256+
**Parameters:**
257+
- `type`: Media type (`'video'` or `'audio'`)
258+
- `time`: Time in seconds
259+
- `seekFlag`: Seek direction (default: backward seek)
237260

238-
```typescript
239-
setLogLevel(level: AVLogLevel) // 2.0 New
240-
```
241-
Parameters:
242-
- `level`: Required, output log level, see `AVLogLevel` for details.
261+
#### `readMediaPacket(type: MediaType, start?: number, end?: number, seekFlag?: AVSeekFlag): ReadableStream<WebAVPacket>`
243262

244-
```typescript
245-
destroy(): void
246-
```
247-
Destroys the instance and releases the worker.
263+
Returns a `ReadableStream` for streaming raw media packet data.
264+
265+
**Parameters:**
266+
- `type`: Media type (`'video'` or `'audio'`)
267+
- `start`: Start time in seconds (default: 0)
268+
- `end`: End time in seconds (default: 0, read till end)
269+
- `seekFlag`: Seek direction (default: backward seek)
270+
271+
### Utility Methods
272+
273+
#### `setLogLevel(level: AVLogLevel): void`
274+
275+
Sets logging verbosity level for debugging purposes.
276+
277+
**Parameters:**
278+
- `level`: Log level (see `AVLogLevel` for available options)
279+
280+
#### `destroy(): void`
281+
282+
Cleans up resources and terminates worker.
248283

249284
## Custom Demuxer
250-
Currently, two versions of the demuxer are provided by default to support different formats:
251-
- `dist/wasm-files/web-demuxer.wasm`: Full version (gzipped: 1131 kB), larger in size, supports mov, mp4, m4a, 3gp, 3g2, mj2, avi, flv, matroska, webm, m4v, mpeg, asf, mpegts
252-
- `dist/wasm-files/web-demuxer-mini.wasm`: Minimalist version (gzipped: 493 kB), smaller in size, only supports mov, mp4, m4a, 3gp, 3g2, matroska, webm, m4v
253-
> If you want to use a smaller size version, you can use version 1.0 of web-demuxer, the lite version is only 115KB
254-
> Version 1.0 is written in C, focuses on WebCodecs, and is small in size, while version 2.0 uses C++ Embind, which provides richer media information output, is easier to maintain, and is large in size
255285

256-
You can also implement a demuxer for specific formats through custom configuration:
286+
Web-Demuxer provides two pre-built versions:
287+
288+
| Version | Size (gzipped) | Supported Formats |
289+
|---------|----------------|-------------------|
290+
| **Full** (`web-demuxer.wasm`) | 1131 kB | mov, mp4, avi, flv, mkv, webm, mpeg, asf, mpegts, etc. |
291+
| **Mini** (`web-demuxer-mini.wasm`) | 493 kB | mov, mp4, mkv, webm, m4v |
257292

258-
First, modify the `enable-demuxer` configuration in the `Makefile`
293+
294+
### Building Custom Version
295+
296+
For specific format support, customize the build:
297+
298+
1. **Configure formats** in `Makefile`:
259299
```makefile
260300
DEMUX_ARGS = \
261-
--enable-demuxer=mov,mp4,m4a,3gp,3g2,mj2,avi,flv,matroska,webm,m4v,mpeg,asf
301+
--enable-demuxer=mov,mp4,m4a,3gp,3g2,mj2
302+
```
303+
304+
2. **Start Docker environment**:
305+
```bash
306+
# For ARM64 (Apple Silicon)
307+
npm run dev:docker:arm64
308+
309+
# For x86_64 (Intel/AMD)
310+
npm run dev:docker:x86_64
262311
```
263-
Then execute `npm run dev:docker:arm64` (if on Windows, please execute `npm run dev:docker:x86_64`) to start the Docker environment.
264312

265-
Finally, execute `npm run build:wasm` to build the demuxer for the specified formats.
313+
3. **Build custom WASM**:
314+
```bash
315+
npm run build:wasm
316+
```
266317

267318
## License
268-
This project is primarily licensed under the MIT License, covering most of the codebase.
269-
The `lib/` directory includes code derived from FFmpeg, which is licensed under the LGPL License.
319+
320+
This project is licensed under the MIT License for the main codebase.
321+
The `lib/` directory contains FFmpeg-derived code under the LGPL License.

0 commit comments

Comments
 (0)