- EdgeTTS(https://github.com/rany2/edge-tts) 是一个非常实用的开源工具,它调用 微软 Edge 浏览器背后的 Azure TTS(Text-to-Speech)接口,可以免费合成高质量的语音,并支持多语言(包括自然中文发音),无须 API 密钥,即装即用,非常适合开发者和批处理任务。
- EdgeTTS 是一个基于 Python 的命令行工具和库
- 使用 Microsoft Edge 的 在线语音合成服务(等同 Azure TTS,但不需要登录或 key,大规模需求(如商业音频、播客、导航等)建议购买正式的 Azure TTS 服务或下载微软语音包离线使用)
- 支持 中文(普通话/粤语),支持不同语音角色、语速、音调
- 可将文本直接转换为音频文件(如
.mp3
/ .wav
)
快速使用 EdgeTTS
pip install edge-tts
edge-tts --text "你好,这是Edge TTS语音合成测试。" --voice zh-CN-XiaoxiaoNeural --write-media output.mp3
可选参数说明:
参数 | 说明 |
---|
--text | 你要朗读的文字(可传入多段) |
--voice | 语音角色(如 zh-CN-XiaoxiaoNeural ) |
--write-media | 输出文件名(如 output.mp3 ) |
--rate | 语速(如 +20% ,-10% ) |
--volume | 音量(如 +5dB ) |
--output-file | 也可以输出成 WAV |
、
支持语音列表查询
edge-tts --list-voices > voices.json
语音名 | 说明 |
---|
zh-CN-XiaoxiaoNeural | 女声(自然流畅) |
zh-CN-YunxiNeural | 男声(中气十足) |
zh-CN-XiaoyiNeural | 女声(温柔型) |
zh-HK-HiuGaaiNeural | 粤语(香港)女声 |
zh-TW-HsiaoChenNeural | 台湾普通话女声 |
CODE
Python 脚本调用 EdgeTTS 合成中文语音
import asyncio
from edge_tts import Communicate
async def main():
communicate = Communicate(text="你好,欢迎使用 Edge TTS 中文语音合成。", voice="zh-CN-XiaoxiaoNeural")
await communicate.save("demo.mp3")
asyncio.run(main())
txt转音频
import os
import asyncio
import edge_tts
VOICE = "zh-CN-XiaoxiaoNeural"
RATE = "+0%"
FOLDER = "texts"
async def text_to_mp3(file_path, output_path):
with open(file_path, "r", encoding="utf-8") as f:
text = f.read().replace('\n', '')
communicate = edge_tts.Communicate(text, voice=VOICE, rate=RATE)
await communicate.save(output_path)
print(f"✅ 已生成:{output_path}")
async def main():
for filename in os.listdir(FOLDER):
if filename.endswith(".txt"):
txt_path = os.path.join(FOLDER, filename)
mp3_name = os.path.splitext(filename)[0] + ".mp3"
mp3_path = os.path.join(FOLDER, mp3_name)
await text_to_mp3(txt_path, mp3_path)
asyncio.run(main())
PDF转音频
import asyncio
import os
from PyPDF2 import PdfReader
from edge_tts import Communicate
# === 设置参数 ===
PDF_PATH = "input.pdf" # ← 替换为你的 PDF 文件路径
OUTPUT_MP3 = "output.mp3" # ← 输出音频文件名
VOICE = "zh-CN-XiaoxiaoNeural" # 中文女声(可改为男声等)
# === 提取 PDF 文本 ===
def extract_text_from_pdf(pdf_path):
reader = PdfReader(pdf_path)
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
return text.strip()
# === 异步 TTS 合成 ===
async def convert_text_to_speech(text, output_path, voice):
communicate = Communicate(text=text, voice=voice)
await communicate.save(output_path)
# === 主流程 ===
async def main():
if not os.path.exists(PDF_PATH):
print(f"❌ PDF 文件不存在: {PDF_PATH}")
return
print("📄 正在提取 PDF 文本...")
text = extract_text_from_pdf(PDF_PATH)
if not text:
print("⚠️ 没有提取到文本内容。请检查 PDF 是否为扫描图像。")
return
print("🎙️ 正在合成语音,请稍候...")
await convert_text_to_speech(text, OUTPUT_MP3, VOICE)
print(f"✅ 已保存为音频文件: {OUTPUT_MP3}")
# === 执行 ===
asyncio.run(main())