使用edge-tts将文字转成语音

参考:https://github.com/rany2/edge-tts 目前3.1k 🌟

重点:免费,无需 API-KEY 即可使用 tts

安装 pip install edge-tts

可以使用命令行来执行

$ edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt

改变速度、音量、音调

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

也可以使用代码,主要的 api 有

  • edge_tts.Communicate(TEXT, VOICE)
  • Communicate.save、Communicate.stream
# _*_ coding: utf-8 _*_
# @Time : 2024/3/19 21:03
# @Author : Michael
# @File : edgeTTS.py
# @desc :
import asyncio
import random
import edge_tts


async def tts() -> None:
    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}")

async def search_voice_tts() -> None:
    # 根据条件获取语音列表
    voices = await edge_tts.VoicesManager.create()
    # 查找男性、中文、中国大陆的语音
    voice = voices.find(Gender="Male", Language="zh", Locale="zh-CN")
    print(voice)
    # 在查找的结果中随机选择语音
    selected_voice = random.choice(voice)["Name"]
    print(selected_voice)
    communicate = edge_tts.Communicate(TEXT, random.choice(voice)["Name"])
    await communicate.save(OUTPUT_FILE)

async def tts_with_submaker() -> None:
    """输出字幕"""
    communicate = edge_tts.Communicate(TEXT, VOICE)
    submaker = edge_tts.SubMaker()
    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":
                submaker.create_sub((chunk["offset"], chunk["duration"]), chunk["text"])

    with open(WEBVTT_FILE, "w", encoding="utf-8") as file:
        file.write(submaker.generate_subs())

if __name__ == "__main__":
    TEXT = "微软的 edge tts 好棒啊!"
    VOICE = "zh-CN-YunyangNeural"  # ShortName
    OUTPUT_FILE = "test1.mp3"
    WEBVTT_FILE = "test.vtt"
    # 列出相关的voice
    voices_options = asyncio.run(edge_tts.list_voices())
    voices_options = [voice for voice in voices_options if voice["Locale"].startswith("zh-")]
    print(voices_options)
    # 调用 tts
    asyncio.run(tts())
    # 调用 search_voice_tts, 随机选择语音
    asyncio.run(search_voice_tts())
    # 调用 tts_with_submaker, 生成字幕
    asyncio.run(tts_with_submaker())

生成的字幕可以在 plotplayer 中显示
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael阿明

如果可以,请点赞留言支持我哦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值