@ audio/ encode- mp3 (英语)
将PCM音频样本编码为MP3格式.
WASM (libmp3lame by wasm-media-encodeers)——在节点和浏览器中同时工作.
import mp3 from '@audio/encode-mp3';
const encoder = await mp3({ sampleRate: 44100, channels: 1, bitrate: 128 });
const chunk = encoder.encode(channelData); // → Uint8Array (MP3 frames)
const tail = encoder.flush(); // → Uint8Array (remaining)
// concatenate chunk + tail, then write encoder.head() over the start
无缺口
输出会以一个载有LAME标记的Info(CBR)或Xing(VBR)帧来开启:帧和字节计数,一个求取TOC,和编码器延迟(576个样本)并编出解码器trim(ffmpeg, mpg123, Apple's AudioToollbox, LAME's own),所以一个解码文件有源的确切长度和时间. 总数列于末尾: head() 之后 flush() 返回完成的框架(当一个导出时在 ID3 标记后),以便在开始时写入; 完整文件 encode.mp3(...) 联 合 国 @audio/encode 为了你 未喷射(管子),占位符仍然带有延迟,并带有 frames (准确的长度前面) 帧数和被垫也.
选项
| 选项 | 默认 | 说明 |
|---|---|---|
sampleRate | —— 说 | 抽样率(所需) |
channels | 2 | 1 (莫诺语)或 2 (语法) |
bitrate | 128 | Kbps 中的 CBR 比特率 |
quality | —— 说 | VBR质量 0–9 (0=最佳). 如果设定了, 将覆盖比特率 . |
流线
const encoder = await mp3({ sampleRate: 44100, channels: 1, bitrate: 192 });
const a = encoder.encode(chunk1); // → Uint8Array
const b = encoder.encode(chunk2); // → Uint8Array
const c = encoder.flush(); // → Uint8Array
// complete MP3 = concat(a, b, c), encoder.head() written over its start
encoder.free();
流程输出
chapters: [{ time, title }] (秒)成为ID3v2 CTOC/CHAP帧(ID3v2 章节框架增编),单位 writeMeta 我也一样. stream: true 放置标签( n)meta, (中文(简体) ), chapters)在第一帧前;上一章的结尾直到 head() 返回完成的标记 flush(),除非 frames 给出了精确的长度前面.
