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

 更新时间:2024年03月08日 10:59:33   作者:培根芝士  
edge-tts是一个 Python 模块,允许通过Python代码或命令的方式使用 Microsoft Edge 的在线文本转语音服务,这篇文章主要介绍了Python调用edge-tts实现在线文字转语音效果,需要的朋友可以参考下

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-tts

https://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()

到此这篇关于Python调用edge-tts实现在线文字转语音的文章就介绍到这了,更多相关Python在线文字转语音内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python 通过SMSActivateAPI 获取验证码的步骤

    python 通过SMSActivateAPI 获取验证码的步骤

    这篇文章主要介绍了python 通过SMSActivateAPI 如何获取验证码,本文分步骤给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • Python脚本文件外部传递参数的处理方法

    Python脚本文件外部传递参数的处理方法

    Python 自带的接收外部参数的模块,用好以后也是非常的猴赛雷。文章通过一个MySQL 自动化部署脚本,需要指定的参数有三个,我们均可以使用 --参数名 方式来指定,会非常方便,今天介绍的就是 Python 外部指定参数的几种方法,感兴趣的朋友一起看看吧
    2021-05-05
  • PyQt5 对图片进行缩放的实例

    PyQt5 对图片进行缩放的实例

    今天小编就为大家分享一篇PyQt5 对图片进行缩放的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • Python使用whisper实现语音识别(ASR)的示例代码

    Python使用whisper实现语音识别(ASR)的示例代码

    Whisper是OpenAI的一个强大的语音识别库,支持离线的语音识别,本文主要介绍了Python使用whisper实现语音识别(ASR)的示例代码,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • Python打造智能批量重命名工具的详细指南

    Python打造智能批量重命名工具的详细指南

    因办公要求,经常需要对众多、各类文件(夹)进行重命名,人数少还好说,人数大就是一个稍微复杂的问题了,所以本文我们就来使用Python开发一个智能批量重命名工具吧
    2025-07-07
  • 分析Python字符串拼接+=和join()哪个速度更快

    分析Python字符串拼接+=和join()哪个速度更快

    这篇文章主要分析了Python中字符串拼接+=和join()哪个速度更快,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Python直接赋值、浅拷贝与深度拷贝实例分析

    Python直接赋值、浅拷贝与深度拷贝实例分析

    这篇文章主要介绍了Python直接赋值、浅拷贝与深度拷贝,结合实例形式分析了Python直接赋值、浅拷贝与深度拷贝的概念、原理、用法及相关操作注意事项,需要的朋友可以参考下
    2019-06-06
  • Python报错ImportError: IProgress not found. Please update jupyter and ipywidgets解决

    Python报错ImportError: IProgress not found. Please update

    在使用Jupyter Notebook或JupyterLab进行交互式编程时,我们可能会遇到各种导入错误,本文就来介绍一下Python报错ImportError: IProgress not found. Please update jupyter and ipywidgets解决,感兴趣的可以了解一下
    2024-06-06
  • python神经网络Keras GhostNet模型的实现

    python神经网络Keras GhostNet模型的实现

    这篇文章主要为大家介绍了python神经网络Keras GhostNet模型的复现详解,
    2022-05-05
  • paramiko使用tail实时获取服务器的日志输出详解

    paramiko使用tail实时获取服务器的日志输出详解

    这篇文章主要给大家介绍了关于paramiko使用tail实时获取服务器的日志输出的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12

最新评论