前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用edge-tts将文字转成语音

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

作者头像
Michael阿明
发布2024-05-24 08:46:44
5260
发布2024-05-24 08:46:44
举报
文章被收录于专栏:Michael阿明学习之路

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

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

安装 pip install edge-tts

可以使用命令行来执行

代码语言:javascript
复制
$ edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt

改变速度、音量、音调

代码语言:javascript
复制
$ 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
代码语言:javascript
复制
# _*_ 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 中显示

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档