Decode WebM / Matroska audio (Opus, Vorbis, AAC, MP3, FLAC, ALAC, PCM) to PCM samples
- Status
- calibrated
- Updated
- Family
- decode, 24 packages under it
- Category
- codecs
- Licence
- MIT AND BSD-3-Clause AND LGPL-3.0-or-later AND Zlib
- Bundles
- codec-parser, gpl-3.0, libopus, libvorbis, simple-yenc, wasm-audio-decoders
- Dependencies
- 12 packages; 7 not permissive:
@audio/decode-aac LGPL-2.1-or-later AND Apache-2.0
@audio/decode-dts LGPL-2.1-or-later
@audio/decode-eac3 LGPL-2.1-or-later
@audio/decode-mp3 MIT AND LGPL-2.1-only AND Zlib
@audio/decode-opus MIT AND BSD-3-Clause AND LGPL-3.0-or-later
@audio/decode-vorbis MIT AND BSD-3-Clause AND LGPL-3.0-or-later AND Zlib
codec-parser LGPL-3.0-or-later - Downloads
- 715,704 last month
- Tests
- at least 88 in audiojs/decode's CI, passed at 58c36d8,
Decode WebM and Matroska (MKV) audio to PCM float samples — the audio track of video files included (Opus, Vorbis, AAC, ALAC, MP3, FLAC, AC-3, DTS, PCM).
Install
Usage
import decode, { decoder } from '@audio/decode-webm'
let { channelData, sampleRate } = await decode(webmBytes)
let dec = await decoder() // initialize Opus and Vorbis WASM
let result = dec.decode(chunk)
let tail = dec.flush()
dec.free()
Video tracks are skipped; the first audio track is decoded. Laced blocks (Xiph, EBML, fixed) are split into frames.
API
decode(src): Promise<AudioData>
Decode a complete Uint8Array or ArrayBuffer.
decoder(): Promise<WebmDecoder>
Initialize the codec runtimes and return a streaming decoder. For the WebM codecs (Opus, Vorbis) decode() and flush() are synchronous. For Matroska-only codecs the call that completes the track header returns a Promise while the codec package loads; later calls are synchronous. flush() ends the stream.
The factory prepares the Opus and Vorbis runtimes, then releases what the track does not need once the header is read.
Codecs
| CodecID | Decoded by |
|---|---|
A_OPUS |
bundled libopus WASM core from @audio/decode-opus |
A_VORBIS |
bundled @audio/decode-vorbis |
A_AAC (all profiles) |
@audio/decode-aac, loaded on demand |
A_ALAC |
@audio/decode-aac, loaded on demand |
A_MPEG/L3 |
@audio/decode-mp3, loaded on demand |
A_FLAC |
@audio/decode-flac, loaded on demand |
A_PCM/INT/LIT, A_PCM/INT/BIG, A_PCM/FLOAT/IEEE |
built in |
A_AC3 |
@audio/decode-eac3, loaded on demand |
A_DTS (core of DTS-HD too) |
@audio/decode-dts, loaded on demand |
A_EAC3 |
@audio/decode-eac3, loaded on demand |
TrueHD, MPEG Layer I/II, WavPack, TTA and A_MS/ACM tracks throw an error naming the codec. DiscardPadding on Opus blocks is honoured, so files from @audio/encode-webm decode sample-exact.
On-demand codec packages are imported dynamically, which is unavailable inside an AudioWorklet — Opus and Vorbis work there, the others need the main thread or a Worker.
License
ॐ · MIT. Bundled libopus is BSD 3-Clause. decode-webm.js also bundles @audio/decode-vorbis: libvorbis, BSD-3-Clause (LICENSE.libvorbis); codec-parser, LGPL-3.0-or-later (LICENSE.codec-parser); @wasm-audio-decoders, MIT, with puff, zlib (LICENSE.wasm-audio-decoders); simple-yenc, MIT (LICENSE.simple-yenc). The bundled codec-parser is version 2.5.0, taken from decode-vorbis.js: rebuild that with a modified codec-parser by decode-vorbis's build.sh, then decode-webm.js by this repository's build.sh; the GNU GPL that the LGPL-3.0 supplements is in LICENSE.gpl-3.0. It installs the codec packages it loads on demand, and with them FFmpeg, LGPL-2.1-or-later (@audio/decode-aac, -dts, -eac3), libmpg123, LGPL-2.1-only (-mp3), codec-parser, LGPL-3.0-or-later (-flac, -opus, -vorbis), and BSD-3-Clause, Apache-2.0 (decode-aac's ALAC port) and Zlib code.