@audio/decode

中文文档
4.2.0发布于 2026/10/18MITTypeScriptES Module近 30 天下载 60.2万

Decode audio data in node or browser

原始使用文档
文档目录10 节
  1. Whole-file
  2. Chunked
  3. Streaming
  4. Video files
  5. Browser
  6. Synchronous codecs
  7. Metadata
  8. WebWorker
  9. See also
  10. Licensing

@audio/decode test

Try it in the browser: Extract audio from video, Audio converter.

Decode any audio format to raw samples.
JS / WASM with no ffmpeg or native bindings; works in Node.js and browsers.
Small API, minimal size, near-native performance, lazy-loading, chunked decoding.

npm install @audio/decode

import decode from '@audio/decode';

const { channelData, sampleRate } = await decode(anyAudioBuffer);

Supported formats

FormatPackageSizeEngine
MP3@audio/decode-mp392 KBWASM
WAV@audio/decode-wav11 KBJS
OGG Vorbis@audio/decode-vorbis166 KBWASM
FLAC@audio/decode-flac135 KBWASM
Opus@audio/decode-opus166 KBWASM
M4A / AAC / ALAC@audio/decode-aac368 KBWASM + JS
MP4 / MOV / M4V / 3GP video@audio/decode-mp412 KB + codecJS demux
QOA@audio/decode-qoa8 KBJS
AIFF@audio/decode-aiff20 KBJS
CAF@audio/decode-caf9 KBJS
WebM / MKV video@audio/decode-webm250 KBWASM
AVI video@audio/decode-avi8 KB + codecJS demux
AC-3@audio/decode-ac343 KBWASM
DTS@audio/decode-dts200 KBWASM
E-AC-3 (Dolby Digital Plus, + AC-3)@audio/decode-eac3432 KBWASM (FFmpeg libavcodec, LGPL)
APE (Monkey's Audio)@audio/decode-ape262 KBWASM (FFmpeg libavcodec, LGPL)
AMR@audio/decode-amr241 KBWASM
GSM 06.10 (raw frames, by name)@audio/decode-gsm23 KBWASM (libgsm)
WMA (v1, v2, Pro, Lossless)@audio/decode-wma463 KBWASM (FFmpeg libavcodec, LGPL)
WavPack@audio/decode-wavpack51 KBWASM
TTA (True Audio)@audio/decode-tta11 KBJS
Musepack SV7 / SV8@audio/decode-mpc51 KBWASM
MOD / XM / S3M / IT tracker modules@audio/decode-mod1.4 MBWASM (libopenmpt)
DSF / DFF (DSD64–512 → PCM)@audio/decode-dsd18 KBJS

Whole-file

Auto-detects format. Input can be ArrayBuffer, Uint8Array, Buffer, or anything that materializes to bytes, including a Blob/File or fetch Response.

import decode from '@audio/decode'

let { channelData, sampleRate } = await decode(buf)
let fromFile = await decode(fileInput.files[0])   // File
let fromUrl  = await decode(await fetch(url))      // Response

Chunked

let dec = await decode.mp3()
let a = await dec(chunk1)    // { channelData, sampleRate }
let b = await dec(chunk2)
await dec()                  // close

Streaming

import decode from '@audio/decode'

for await (let { channelData, sampleRate } of decode.mp3(response.body)) {
  // process chunks
}

Works with ReadableStream, fetch body, Node stream, or any async iterable.

Formats: mp3, flac, opus, oga, m4a, mp4, mov, wav, qoa, aac, aiff, caf, webm, mkv, avi, ac3, dts, amr, gsm, wma, eac3, ape, wv, tta, mpc, dsf, dff, mod, xm, s3m, it.

Video files

Video containers decode straight to their audio track — the video stream is skipped, no ffmpeg involved:

let { channelData, sampleRate } = await decode(await fetch('trailer.mp4'))
ContainerPackageAudio codecs
MP4, MOV, M4V, 3GP@audio/decode-mp4AAC, ALAC, MP3, FLAC, Opus, AC-3, DTS, AMR, PCM, G.711
WebM, MKV@audio/decode-webmOpus, Vorbis, AAC, ALAC, MP3, FLAC, AC-3, DTS, PCM
AVI@audio/decode-aviPCM, MP3, AAC, AC-3, DTS, G.711

Surround tracks keep their layout (up to 5.1, WAV channel order). E-AC-3 and TrueHD tracks throw an error naming the codec.

Browser

Works from a CDN without a bundler. Codecs load on demand via dynamic import, only for formats you decode:

<script type="module">
  import decode from 'https://esm.sh/@audio/decode'
  let { channelData, sampleRate } = await decode(buf)
</script>

For self-hosting, use an import map to point @audio/decode and each needed @audio/decode-* package to local files. Codec-internal files load by relative path.

Each codec package's main export works in an AudioWorklet without Blob, TextDecoder, Worker, or fetch. Import codec packages directly because @audio/decode uses dynamic imports.

Initialize WASM before rendering. Decoding runs on the worklet thread and can interrupt audio output.

Synchronous codecs

The umbrella remains async for detection, lazy imports, and Blob/Response inputs. Import a codec package directly for synchronous calls.

wav, qoa, aiff, and caf are synchronous:

import decode from '@audio/decode-wav'
let pcm = decode(wavBytes)

WASM codecs initialize asynchronously, then decode synchronously:

import { decoder } from '@audio/decode-flac'
let dec = await decoder()
let pcm = dec.decode(bytes)
let tail = dec.flush()
dec.free()

Metadata

Read tags, pictures, markers and regions without decoding samples. Available for wav, mp3, flac, oga (Ogg Vorbis), opus, and m4a.

import { wav, mp3, flac, oga, opus, m4a } from '@audio/decode/meta'

let { meta, sampleRate, markers, regions } = mp3(bytes)
// meta: { title, artist, album, year, bpm, key, comment, pictures, raw, ... }
// markers: [{ sample, label }]
// regions: [{ sample, length, label }]

Each codec sub-package also exposes its parser directly:

import { parseMeta } from '@audio/decode-wav/meta'
let info = parseMeta(wavBytes)

WebWorker

Each @audio/decode-* package is a self-contained ESM module that can run in a worker:

// decode-worker.js
import decode from '@audio/decode-mp3'

self.onmessage = async ({ data }) => {
  let pcm = await decode(data)
  self.postMessage(pcm, pcm.channelData.map(ch => ch.buffer))
}

// main.js
let worker = new Worker('./decode-worker.js', { type: 'module' })
worker.postMessage(mp3buf, [mp3buf])
worker.onmessage = ({ data }) => { /* { channelData, sampleRate } */ }

See also

  • audio – edit, measure and save: trim, loudness to spec, delivery checks, a CLI.
  • encode – encode PCM into any audio format.
  • audio-type – detect audio format from buffer.

Licensing

The umbrella and most codec packages are MIT. Codecs built on other libraries carry that library's license: @audio/decode-aac GPL-2.0; @audio/decode-ac3 and @audio/decode-dts GPL-2.0-or-later; @audio/decode-ape, @audio/decode-eac3 and @audio/decode-wma LGPL-2.1-or-later (a slim FFmpeg libavcodec build, no GPL components); @audio/decode-wavpack, @audio/decode-mpc and @audio/decode-mod BSD-3-Clause; @audio/decode-amr Apache-2.0. Install only the codecs whose licenses fit your project. The umbrella loads them on demand.

ॐ · MIT

版本与发布记录

@audio/decode 共发布 16 个版本,首次发布于 2026/07/09;过去 12 个月发布了 16 个版本。

版本发布日期距今
4.2.0latest2026/10/1820 天前
4.1.12026/10/071 个月前
4.1.02026/10/071 个月前
4.0.02026/09/291 个月前
3.16.22026/09/271 个月前
3.16.12026/09/261 个月前

关于 @audio/decode 的常见问题

@audio/decode 是什么?

Decode audio data in node or browser

如何安装 @audio/decode?

在项目目录运行 npm install @audio/decode,或使用 pnpm add / yarn add / bun add。

@audio/decode 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@audio/decode 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@audio/decode 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@audio/decode 可以商用吗?

@audio/decode 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@audio/decode 的下载量有多少?

根据 npm 官方下载统计,@audio/decode 在 2026/10/07 – 2026/11/05 这 30 天内被下载 602,170 次,日均约 20,072 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@audio/decode 还在维护吗?

最新版本 4.2.0 发布于 2026/10/18(20 天前);至今共发布 16 个版本,其中过去 12 个月发布了 16 个;首次发布于 2026/07/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。