@bestcodes/edge-tts

中文文档
3.0.1发布于 2025/12/28MITTypeScriptES Module构建来源已验证近 30 天下载 2210

Free Text-to-Speech using Microsoft Edge TTS

原始使用文档
文档目录13 节
  1. Installation
  2. Quick Start
  3. Get audio buffer
  4. Save to file
  5. With subtitles
  6. Real-time Streaming
  7. Stream audio chunks
  8. Stream directly to file with progress
  9. Stream with subtitles (real-time updates)
  10. Options
  11. List voices
  12. Low-level API
  13. License

@bestcodes/edge-tts

A TypeScript port of edge-tts. Small, fast, and easy text-to-speech using Microsoft Edge's online service.

  • Tiny bundle size (< 50kb)
  • No API keys required
  • Stream audio or save to file
  • Generate SRT subtitles
  • Access to all available Edge voices

Installation

npm install @bestcodes/edge-tts
# or
bun add @bestcodes/edge-tts

Quick Start

Get audio buffer

import { generateSpeech } from "@bestcodes/edge-tts";

const audio = await generateSpeech({
  text: "Hello, world!",
  voice: "en-US-EmmaMultilingualNeural",
});

// Do something with the audio buffer

Save to file

import { generateSpeechToFile } from "@bestcodes/edge-tts";

await generateSpeechToFile({
  text: "Hello, world!",
  outputPath: "./output.mp3",
});

With subtitles

import { generateSpeechWithSubtitlesToFile } from "@bestcodes/edge-tts";

const { audio, subtitles } = await generateSpeechWithSubtitlesToFile({
  text: "This text will have subtitles.",
  subtitlePath: "./subtitles.srt",
});

Real-time Streaming

Stream audio chunks

import { streamSpeech } from "@bestcodes/edge-tts";

for await (const chunk of streamSpeech({
  text: "Hello, world!",
  voice: "en-US-EmmaMultilingualNeural",
})) {
  if (chunk.type === "audio" && chunk.data) {
    // Process audio chunk in real-time
    console.log(`Received ${chunk.data.length} bytes`);
  }
}

Stream directly to file with progress

import { streamSpeechToFile } from "@bestcodes/edge-tts";

for await (const progress of streamSpeechToFile({
  text: "Hello, world!",
  outputPath: "./output.mp3",
})) {
  console.log(
    `Written: ${progress.bytesWritten} bytes (chunk: ${progress.chunkSize})`,
  );
}

Stream with subtitles (real-time updates)

import { streamSpeechWithSubtitlesToFile } from "@bestcodes/edge-tts";

for await (const chunk of streamSpeechWithSubtitlesToFile({
  text: "This text will have subtitles.",
  subtitlePath: "./subtitles.srt",
})) {
  if (chunk.type === "audio" && chunk.data) {
    // Process audio chunk
  } else if (chunk.subtitles) {
    // Subtitles file updated in real-time
    console.log("Subtitles updated:", chunk.subtitles);
  }
}

Options

{
  text: string;                    // Required: text to convert
  voice?: string;                  // Default: "en-US-EmmaMultilingualNeural"
  rate?: string;                   // e.g. "+10%" or "-20%" (default: "+0%")
  volume?: string;                 // e.g. "+50%" or "-10%" (default: "+0%")
  pitch?: string;                  // e.g. "+10Hz" or "-5Hz" (default: "+0Hz")
  boundary?: "WordBoundary" | "SentenceBoundary";
  proxy?: string;                  // Optional proxy URL
  connectTimeoutSeconds?: number;  // Default: 10
  receiveTimeoutSeconds?: number;  // Default: 60
  outputPath?: string;             // For generateSpeechToFile/streamSpeechToFile
  subtitlePath?: string;           // For generateSpeechWithSubtitlesToFile/streamSpeechWithSubtitlesToFile
}

List voices

import { getVoices, findVoices } from "@bestcodes/edge-tts";

// Get all voices
const allVoices = await getVoices();

// Find specific voices
const femaleVoices = await findVoices({ Gender: "Female" });
const englishVoices = await findVoices({ Locale: "en-US" });

Low-level API

import { Experimental_Raw } from "@bestcodes/edge-tts";

const communicate = new Experimental_Raw.Communicate(
  "Hello!",
  "en-US-EmmaMultilingualNeural",
);

for await (const chunk of communicate.stream()) {
  if (chunk.type === "audio") {
    // Process audio data (chunk.data is a Buffer)
  } else if (chunk.type === "WordBoundary") {
    // Word boundary metadata
  }
}

License

MIT

版本与发布记录

@bestcodes/edge-tts 共发布 6 个版本,首次发布于 2024/10/26;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.1latest2025/12/2810 个月前
3.0.02025/12/2710 个月前
2.0.02025/12/2710 个月前
1.0.32024/10/262 年前
1.0.22024/10/262 年前
1.0.12024/10/262 年前

关于 @bestcodes/edge-tts 的常见问题

@bestcodes/edge-tts 是什么?

Free Text-to-Speech using Microsoft Edge TTS

如何安装 @bestcodes/edge-tts?

在项目目录运行 npm install @bestcodes/edge-tts,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^5.9.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bestcodes/edge-tts 支持 TypeScript 吗?

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

@bestcodes/edge-tts 支持 ESM 还是 CommonJS?

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

@bestcodes/edge-tts 可以商用吗?

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

@bestcodes/edge-tts 的下载量有多少?

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

@bestcodes/edge-tts 还在维护吗?

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