语音识别——文本转语音

发布于:2025-05-27 ⋅ 阅读:(45) ⋅ 点赞:(0)

python自带的pytts说话人的声音比较机械,edge-tts提供了更自然的语音合成效果,支持多种语音选择。

项目地址:GitHub - rany2/edge-tts: Use Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API key

1. edge-tts安装

pip install edge-tts

2. MPV安装

同时还需要安装mpv才能正常使用

windows mpv安装:

安装地址:

mpv.io | Installation

安装方法:

windows 安装mpv player-CSDN博客

ubuntu mpv安装:

sudo apt-get update

sudo apt-get install mpv

3. 用法

直接在终端输出声音:

edge-playback --text "你好" --voice zh-CN-YunxiNeural

查看支持的声音列表:

edge-tts --list-voices

保存问mp3文件

edge-tts --text "你好" --write-media C:\output\hello.mp3

 

python使用edge-tts

# 使用edge-tts播放输出,避免保存临时文件
# 直接将edge-tts的输出通过管道传递给音频播放器
# edge-playback --text "你好" --voice zh-CN-YunxiNeural
# 查看支持的语音
# edge-tts --list-voices

import subprocess

def talkContent(text):
    # 创建edge-tts进程,输出音频数据到管道
    tts_process = subprocess.Popen([
                                    'edge-tts',
                                    '--text', text,
                                    '--voice', 'zh-CN-XiaoxiaoNeural', # zh-CN-shaanxi-XiaoniNeural
                                    '--volume', '+100%',
                                    ],
        stdout=subprocess.PIPE
    )
    
    # 创建播放器进程,从管道接收音频数据
    player_process = subprocess.Popen(
        ['mpv', '--no-cache', '--no-terminal', '--', '-'],
        stdin=tts_process.stdout
    )
    
    # 关闭父进程的管道副本,避免死锁
    tts_process.stdout.close()
    
    # 等待播放器进程完成
    player_process.wait()

# if __name__ == "__main__":
#     talkContent("你好")


网站公告

今日签到

点亮在社区的每一天
去签到