0
点赞
收藏
分享

微信扫一扫

Python调用edge-tts实现在线文字转语音

惠特曼 03-14 14:01 阅读 2
python

edge-tts是一个 Python 模块,允许通过Python代码或命令的方式使用 Microsoft Edge 的在线文本转语音服务。

项目源码

GitHub - rany2/edge-tts: Use Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API keyUse Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API key - rany2/edge-ttsicon-default.png?t=N7T8https://github.com/rany2/edge-tts

安装

pip install edge-tts

用法

命令行方式

  • --write-media:输出音频
  • --write-subtitles:输出字幕
edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt

选项检查可用的声音

edge-tts --list-voices

改变声音

  • --voice:指定声音
edge-tts --voice zh-CN-XiaoxiaoNeural --text "君不见黄河之水天上来" --write-media hello.mp3 --write-subtitles hello.vtt

改变速率、音量和音高

edge-tts --rate=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
edge-tts --volume=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
edge-tts --pitch=-50Hz --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt

播放音频

edge-playback

edge-playback 用于播放生成的语音。它采用与 edge-tts 相同的参数。

Python代码方式

文字转音频

import asyncio
import edge_tts

TEXT = "Hello World!"
VOICE = "en-GB-SoniaNeural"
OUTPUT_FILE = "test.mp3"

async def amain() -> None:
    """Main function"""
    communicate = edge_tts.Communicate(TEXT, VOICE)
    await communicate.save(OUTPUT_FILE)


if __name__ == "__main__":
    loop = asyncio.get_event_loop_policy().get_event_loop()
    try:
        loop.run_until_complete(amain())
    finally:
        loop.close()

使用VoicesManager进行动态语音选择的示例

import asyncio
import random

import edge_tts
from edge_tts import VoicesManager

TEXT = "Hoy es un buen día."
OUTPUT_FILE = "spanish.mp3"

async def amain() -> None:
    """Main function"""
    voices = await VoicesManager.create()
    voice = voices.find(Gender="Male", Language="es")
    # Also supports Locales
    # voice = voices.find(Gender="Female", Locale="es-AR")

    communicate = edge_tts.Communicate(TEXT, random.choice(voice)["Name"])
    await communicate.save(OUTPUT_FILE)


if __name__ == "__main__":
    loop = asyncio.get_event_loop_policy().get_event_loop()
    try:
        loop.run_until_complete(amain())
    finally:
        loop.close()

流式传输来自TTS的音频数据

import asyncio
import edge_tts

TEXT = "Hello World!"
VOICE = "en-GB-SoniaNeural"
OUTPUT_FILE = "test.mp3"

async def amain() -> None:
    """Main function"""
    communicate = edge_tts.Communicate(TEXT, VOICE)
    with open(OUTPUT_FILE, "wb") as file:
        async for chunk in communicate.stream():
            if chunk["type"] == "audio":
                file.write(chunk["data"])
            elif chunk["type"] == "WordBoundary":
                print(f"WordBoundary: {chunk}")


if __name__ == "__main__":
    loop = asyncio.get_event_loop_policy().get_event_loop()
    try:
        loop.run_until_complete(amain())
    finally:
        loop.close()
举报

相关推荐

0 条评论