libav-wasm 0.8.1 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -40
- package/build/index.d.ts +33 -8
- package/build/index.js +1638 -1564
- package/build/libav.wasm +0 -0
- package/build/utils.d.ts +0 -2
- package/build/worker.d.ts +43 -2
- package/build/worker.js +3472 -3654
- package/package.json +23 -15
- package/build/libav-mt.wasm +0 -0
package/README.md
CHANGED
|
@@ -1,61 +1,98 @@
|
|
|
1
|
-
#
|
|
1
|
+
# libav-wasm
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Remuxes a video file into fragmented MP4 in the browser, a fragment at a time, from a `read(offset, size)`
|
|
4
|
+
you supply. Nothing is downloaded up front and nothing is written to disk, so it plays a file over HTTP
|
|
5
|
+
range requests, out of a torrent, or out of anything else that can answer for a byte range.
|
|
4
6
|
|
|
5
|
-
[src/test.ts](
|
|
6
|
-
|
|
7
|
+
ffmpeg 7.1 compiled to wasm with emscripten. [src/test.ts](./src/test.ts) is a working player.
|
|
8
|
+
|
|
9
|
+
## What it accepts
|
|
10
|
+
|
|
11
|
+
Any container ffmpeg can demux: mkv, mp4, mov, webm, avi, mpegts, flv, 3gp and the rest. What decides
|
|
12
|
+
whether a file plays is the **codec**, not the extension.
|
|
13
|
+
|
|
14
|
+
| | passes through | re-encoded to aac | not playable |
|
|
15
|
+
| --- | --- | --- | --- |
|
|
16
|
+
| video | h264, hevc, vp9, av1 | | anything else |
|
|
17
|
+
| audio | aac, opus, flac | ac3, eac3, truehd, dts, mp3, vorbis, wma, pcm, … | codecs with no decoder |
|
|
18
|
+
|
|
19
|
+
Video is never re-encoded, so a codec no browser can decode (mpeg2, mpeg4 part 2, theora, vc1) cannot be
|
|
20
|
+
rescued and `init()` rejects saying so. Audio always can be, and is, so a track the mp4 muxer will not
|
|
21
|
+
carry never costs you the file. Thumbnails have none of these limits: they decode in wasm and hand back
|
|
22
|
+
pixels, so they work on files that cannot be remuxed at all.
|
|
23
|
+
|
|
24
|
+
Timestamps are reported relative to the start of the content, so a format whose clock starts elsewhere
|
|
25
|
+
(mpegts) still lines up with a player's timeline.
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
7
28
|
|
|
8
|
-
Basic usage
|
|
9
29
|
```ts
|
|
30
|
+
import { makeRemuxer } from 'libav-wasm'
|
|
31
|
+
|
|
10
32
|
const remuxer = await makeRemuxer({
|
|
11
|
-
//
|
|
33
|
+
// where libav.wasm is served from
|
|
12
34
|
publicPath: new URL('/dist/', new URL(import.meta.url).origin).toString(),
|
|
13
|
-
// url of the worker file of libavWASM
|
|
14
35
|
workerUrl: new URL('../build/worker.js', import.meta.url).toString(),
|
|
15
|
-
|
|
16
|
-
bufferSize: 2_500_000,
|
|
17
|
-
// byte length of the video file
|
|
36
|
+
workerOptions: { type: 'module' },
|
|
18
37
|
length: contentLength,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
VIDEO_URL,
|
|
23
|
-
{
|
|
24
|
-
headers: {
|
|
25
|
-
Range: `bytes=${offset}-${size ? Math.min(offset + size, contentLength) - 1 : ''}`
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
).then(res => res.body)
|
|
38
|
+
read: (offset, size) =>
|
|
39
|
+
fetch(VIDEO_URL, { headers: { Range: `bytes=${offset}-${offset + size - 1}` } })
|
|
40
|
+
.then(res => res.arrayBuffer()),
|
|
29
41
|
})
|
|
30
42
|
|
|
31
|
-
//
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
// pos: number,
|
|
38
|
-
// pts: number, // timestamp
|
|
39
|
-
// duration: number
|
|
40
|
-
// }
|
|
41
|
-
// buffer can be appended to the MediaSource API(MSE API)
|
|
43
|
+
// the mp4 init segment, plus every piece of metadata the file carries
|
|
44
|
+
const { data, info, indexes, subtitles, attachments, chapters } = await remuxer.init()
|
|
45
|
+
const codecs = [info.output.videoMimeType, info.output.audioMimeType].filter(Boolean).join(',')
|
|
46
|
+
const mime = `video/mp4; codecs="${codecs}"`
|
|
47
|
+
|
|
48
|
+
// { data, pts, duration, offset, finished } — data appends straight to a SourceBuffer
|
|
42
49
|
const chunk = await remuxer.read()
|
|
43
50
|
|
|
44
|
-
//
|
|
45
|
-
const
|
|
46
|
-
|
|
51
|
+
// rebuilds the muxer and starts again from the keyframe at or before this timestamp
|
|
52
|
+
const seeked = await remuxer.seek(90)
|
|
53
|
+
|
|
54
|
+
await remuxer.destroy()
|
|
47
55
|
```
|
|
48
56
|
|
|
57
|
+
`makeThumbnailer` opens the same file with no muxer, no encoder and no stream map, for `readKeyframe(t)`
|
|
58
|
+
alone. Use it rather than a remuxer: `readKeyframe` seeks backward on the input, which an output muxer
|
|
59
|
+
cannot follow, and a thumbnailer has no muxer to damage.
|
|
60
|
+
|
|
61
|
+
## Building
|
|
62
|
+
|
|
63
|
+
`npm run build` compiles `src/main.cpp` in Docker (emsdk plus ffmpeg, cached after the first run), bundles
|
|
64
|
+
the library and the worker, and emits types. Only the last step re-runs when you touch C++, so the loop is
|
|
65
|
+
about 25 seconds.
|
|
66
|
+
|
|
67
|
+
## Tests
|
|
68
|
+
|
|
69
|
+
`npm test` runs a browser suite against fixtures it synthesizes with ffmpeg on first run. Nothing is
|
|
70
|
+
committed and nothing is anyone's content, so there is no copyright question and no multi-megabyte blob in
|
|
71
|
+
git history: what matters for a remuxer is a file's structure, and every fixture exists because a real bug
|
|
72
|
+
needed exactly that structure to show up.
|
|
73
|
+
|
|
74
|
+
Each fixture encodes its own timestamp in the frame's colour, so a decoded thumbnail can be checked back to
|
|
75
|
+
the second it came from. Without that the only available assertion is "some bytes came back", which is how
|
|
76
|
+
hevc thumbnails silently returned frame one for every timestamp.
|
|
77
|
+
|
|
78
|
+
Drop real files in `fixtures/local/` to widen coverage. That directory is gitignored, and those tests
|
|
79
|
+
assert only what has to hold for any file. `npm run test:large` adds a fixture over 4 GB for the byte
|
|
80
|
+
offset tests.
|
|
81
|
+
|
|
82
|
+
Real Chrome is used rather than Playwright's bundled Chromium, because codec support is a property of the
|
|
83
|
+
binary: Chromium reports hevc unsupported where Chrome supports it. Set `LIBAV_CHROME_PATH` if Chrome is
|
|
84
|
+
not on `PATH`.
|
|
49
85
|
|
|
50
86
|
## Intellisense
|
|
51
|
-
To have C++ autocompletion, put a ffmpeg repo clone folder in the root
|
|
52
|
-
`git clone https://github.com/FFmpeg/FFmpeg` & `git clone https://github.com/emscripten-core/emscripten`
|
|
53
87
|
|
|
88
|
+
For C++ autocompletion, clone into the repo root:
|
|
89
|
+
`git clone https://github.com/FFmpeg/FFmpeg` and `git clone https://github.com/emscripten-core/emscripten`
|
|
90
|
+
|
|
91
|
+
## Ideas
|
|
54
92
|
|
|
93
|
+
- `sourceBuffer.timestampOffset` with always-increasing timestamps, to avoid re-initialising on a backwards seek
|
|
94
|
+
- video transcoding, the only thing that would make mpeg2, mpeg4 part 2 and theora playable
|
|
95
|
+
- audio codec reference: https://cconcolato.github.io/media-mime-support/#audio_codecs
|
|
55
96
|
|
|
56
|
-
## Issues to fix / features to implement
|
|
57
|
-
- Try to play around with `sourceBuffer.timestampOffset = res.pts` and always increasing timestamps to not have to re-init on backwards seeks
|
|
58
|
-
- Transcoding
|
|
59
|
-
- Base transcoding off of https://cconcolato.github.io/media-mime-support/#audio_codecs
|
|
60
97
|
<!-- https://www.ffmpeg.org/doxygen/trunk/remuxing_8c-example.html -->
|
|
61
98
|
<!-- https://github.com/leandromoreira/ffmpeg-libav-tutorial -->
|
package/build/index.d.ts
CHANGED
|
@@ -7,11 +7,12 @@ export type MakeTransmuxerOptions = {
|
|
|
7
7
|
workerOptions?: WorkerOptions;
|
|
8
8
|
read: (offset: number, size: number) => Promise<ArrayBuffer>;
|
|
9
9
|
length: number;
|
|
10
|
-
|
|
10
|
+
/** how much the demuxer reads ahead; defaults to 2.5 MB for a remuxer and 1 MB for a thumbnailer */
|
|
11
|
+
bufferSize?: number;
|
|
11
12
|
/** Input stream index of the audio track to mux; defaults to the first audio stream */
|
|
12
13
|
audioStreamIndex?: number;
|
|
13
14
|
};
|
|
14
|
-
export declare const makeRemuxer: (
|
|
15
|
+
export declare const makeRemuxer: (options: MakeTransmuxerOptions) => Promise<{
|
|
15
16
|
worker: Worker;
|
|
16
17
|
init: () => Promise<{
|
|
17
18
|
data: ArrayBuffer;
|
|
@@ -21,14 +22,14 @@ export declare const makeRemuxer: ({ publicPath, workerUrl, workerOptions, read,
|
|
|
21
22
|
data: ArrayBuffer;
|
|
22
23
|
}[];
|
|
23
24
|
subtitles: ({
|
|
24
|
-
type:
|
|
25
|
+
type: 'header';
|
|
25
26
|
streamIndex: number;
|
|
26
27
|
content: string;
|
|
27
28
|
format: string[];
|
|
28
29
|
language: string;
|
|
29
30
|
title: string;
|
|
30
31
|
} | {
|
|
31
|
-
type:
|
|
32
|
+
type: 'dialogue';
|
|
32
33
|
streamIndex: any;
|
|
33
34
|
start: number;
|
|
34
35
|
end: number;
|
|
@@ -77,14 +78,14 @@ export declare const makeRemuxer: ({ publicPath, workerUrl, workerOptions, read,
|
|
|
77
78
|
seek: (timestamp: number) => Promise<{
|
|
78
79
|
data: ArrayBuffer;
|
|
79
80
|
subtitles: ({
|
|
80
|
-
type:
|
|
81
|
+
type: 'header';
|
|
81
82
|
streamIndex: number;
|
|
82
83
|
content: string;
|
|
83
84
|
format: string[];
|
|
84
85
|
language: string;
|
|
85
86
|
title: string;
|
|
86
87
|
} | {
|
|
87
|
-
type:
|
|
88
|
+
type: 'dialogue';
|
|
88
89
|
streamIndex: any;
|
|
89
90
|
start: number;
|
|
90
91
|
end: number;
|
|
@@ -104,14 +105,14 @@ export declare const makeRemuxer: ({ publicPath, workerUrl, workerOptions, read,
|
|
|
104
105
|
read: () => Promise<{
|
|
105
106
|
data: ArrayBuffer;
|
|
106
107
|
subtitles: ({
|
|
107
|
-
type:
|
|
108
|
+
type: 'header';
|
|
108
109
|
streamIndex: number;
|
|
109
110
|
content: string;
|
|
110
111
|
format: string[];
|
|
111
112
|
language: string;
|
|
112
113
|
title: string;
|
|
113
114
|
} | {
|
|
114
|
-
type:
|
|
115
|
+
type: 'dialogue';
|
|
115
116
|
streamIndex: any;
|
|
116
117
|
start: number;
|
|
117
118
|
end: number;
|
|
@@ -129,4 +130,28 @@ export declare const makeRemuxer: ({ publicPath, workerUrl, workerOptions, read,
|
|
|
129
130
|
finished: boolean;
|
|
130
131
|
}>;
|
|
131
132
|
readKeyframe: (timestamp: number) => Promise<ArrayBuffer>;
|
|
133
|
+
/** Takes effect on the next seek */
|
|
134
|
+
setAudioStreamIndex: (index: number) => Promise<void>;
|
|
135
|
+
}>;
|
|
136
|
+
/**
|
|
137
|
+
* Open a file for thumbnails only. No output muxer, no encoder, no stream map, no header.
|
|
138
|
+
*
|
|
139
|
+
* `readKeyframe` seeks BACKWARD on the input, which an output muxer cannot follow, so a remuxer that also
|
|
140
|
+
* served thumbnails could only be kept correct by never doing both on one instance. There is no muxer here
|
|
141
|
+
* to damage. It also opens files whose audio the muxer refuses outright, which a remuxer cannot.
|
|
142
|
+
*/
|
|
143
|
+
export declare const makeThumbnailer: (options: MakeTransmuxerOptions) => Promise<{
|
|
144
|
+
worker: Worker;
|
|
145
|
+
init: () => Promise<{
|
|
146
|
+
duration: number;
|
|
147
|
+
videoMimeType: string;
|
|
148
|
+
videoExtradata: ArrayBuffer;
|
|
149
|
+
indexes: {
|
|
150
|
+
index: number;
|
|
151
|
+
timestamp: number;
|
|
152
|
+
pos: number;
|
|
153
|
+
}[];
|
|
154
|
+
}>;
|
|
155
|
+
readKeyframe: (timestamp: number) => Promise<ArrayBuffer>;
|
|
156
|
+
destroy: () => Promise<void>;
|
|
132
157
|
}>;
|