Encode PCM audio samples to AAC and HE-AAC (ADTS): WebCodecs, else the Fraunhofer FDK encoder (WASM). Opt-in alternative to @audio/encode-aac
Encode PCM audio samples to AAC (ADTS) format, HE-AAC and HE-AACv2 included, in browsers and Node.
Where the browser's WebCodecs AudioEncoder supports the configuration (Chromium 94+, Safari 16+) it encodes there: zero bundle cost, hardware-accelerated. Everywhere else (Node, Firefox) it runs the Fraunhofer FDK AAC encoder (v2.0.3) compiled to WebAssembly, loaded on first use.
Opt-in: @audio/encode-aac, the default that @audio/encode and @audio/encode-mp4 install, runs FFmpeg's AAC-LC encoder (LGPL) in its place and has no HE-AAC outside WebCodecs. This package has the same API and adds HE-AAC under the Fraunhofer FDK license; neither of those packages depends on it.
import aac from '@audio/encode-aac-fdk';
const encoder = await aac({ sampleRate: 44100, channels: 2, bitrate: 128 });
const a = await encoder.encode(chunk1); // → Uint8Array (ADTS frames)
const b = await encoder.encode(chunk2);
const c = await encoder.flush(); // → Uint8Array (remaining frames); frees the encoder
// complete ADTS AAC = concat(a, b, c)
Options
| Option | Default | Description |
|---|---|---|
sampleRate |
— | Input sample rate in Hz (required) |
channels |
1 |
1 or 2; FDK also 3–6 and 8 (WAV channel order) |
bitrate |
128 |
Target bitrate in kbps |
profile |
'lc' |
'lc' AAC-LC, 'he' HE-AAC (SBR), 'hev2' HE-AACv2 (SBR + PS, stereo only) |
The encoder reports priming (its delay in samples: FDK 2048 for AAC-LC) and frameLength (1024; 2048 for HE-AAC), which a container needs for gapless playback. @audio/encode-mp4 uses them.
Output is raw ADTS-framed AAC: each chunk is a sequence of whole ADTS frames, playable as .aac. HE-AAC signals SBR implicitly (an LC header at the core rate), as ADTS must.
In place of @audio/encode-aac
To have @audio/encode and @audio/encode-mp4 encode through this package (HE-AAC in Node, encode.aac(..., { profile: 'he' }), encode.m4a(..., { profile: 'he' })), map the default to it in your project's package.json and reinstall:
"overrides": { "@audio/encode-aac": "npm:@audio/encode-aac-fdk@^1.0.0" }
npm's overrides field takes another package by the npm: prefix. The FDK license then applies to what you install.
License
MIT. The FDK encoder inside (src/fdk.wasm.js) is the Fraunhofer FDK AAC Codec Library, distributed under its own license (LICENSE.fdk-aac): free to redistribute with that notice, and granting no patent license. AAC is covered by patents; commercial distributors may need a license from the patent pool (Via LA).