@ bestcodes/ edge- tts (英语)
类型脚本端口 边点. 使用微软Edge的在线服务,实现小而快,简便的文本对语音.
- 小捆绑大小( < 50kb)
- 不需要 API 密钥
- 流出音频或保存到文件
- 生成 SRT 字幕
- 访问所有可用的边缘声音
安装
npm install @bestcodes/edge-tts
# or
bun add @bestcodes/edge-tts
快速启动
获取音频缓冲器
import { generateSpeech } from "@bestcodes/edge-tts";
const audio = await generateSpeech({
text: "Hello, world!",
voice: "en-US-EmmaMultilingualNeural",
});
// Do something with the audio buffer
保存到文件
import { generateSpeechToFile } from "@bestcodes/edge-tts";
await generateSpeechToFile({
text: "Hello, world!",
outputPath: "./output.mp3",
});
带字幕
import { generateSpeechWithSubtitlesToFile } from "@bestcodes/edge-tts";
const { audio, subtitles } = await generateSpeechWithSubtitlesToFile({
text: "This text will have subtitles.",
subtitlePath: "./subtitles.srt",
});
实时流线
流出音频块
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`);
}
}
直接流到带有进度的文件
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})`,
);
}
有字幕的流( 实时更新)
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);
}
}
选项
{
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
}
列表声音
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" });
低级 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
}
}
许可证
麻省理工学院