python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python edge-tts语音合成

Python中edge-tts实现便捷语音合成

作者:T0uken

edge-tts是一个功能强大的Python库,支持多种语言和声音选项,本文主要介绍了Python中edge-tts实现便捷语音合成,具有一定的参考价值,感兴趣的可以了解一下

edge-tts 是一个功能强大的 Python 库,利用 Microsoft Azure 的云端文本到语音(TTS)服务,支持多种语言和声音选项,能够生成高质量、自然听感的语音输出。它支持多种音频格式,包括 MP3、WAV 和 OGG,适用于在本地或服务器上进行文本转换为语音的应用程序,可以通过简单的 API 调用进行部署和运行,非常适合语音助手、教育应用和音频内容制作等多种场景。

安装与环境设置

首先,确保您已经安装了 edge-tts 库:

pip install edge-tts

安装完成后,您可以开始进行语音合成相关的功能开发。

文本转语音

在这个章节,我们将展示如何实现一个基础功能:传入文本并生成语音,保存为音频文件。该功能使用固定的语音并将语音保存为 .mp3 文件。执行后会生成 weather.mp3 音频文件,包含了合成的中文语音。

import asyncio
import edge_tts

def generate_audio(text: str, voice: str, output_file: str) -> None:
    """
    传入文本、语音及输出文件名,生成语音并保存为音频文件
    :param text: 需要合成的中文文本
    :param voice: 使用的语音类型,如 'zh-CN-XiaoyiNeural'
    :param output_file: 输出的音频文件名
    """
    async def generate_audio_async() -> None:
        """异步生成语音"""
        communicate = edge_tts.Communicate(text, voice)
        await communicate.save(output_file)

    # 异步执行生成音频
    asyncio.run(generate_audio_async())

# 示例调用
generate_audio("今天天气不错,适合出门玩耍。", "zh-CN-XiaoyiNeural", "weather.mp3")

查找音色

在此章节中,我们将展示如何查找符合特定条件的语音,并将符合条件的语音列表打印给用户,而不进行进一步的操作。此方法仅列出符合条件的语音,并打印出每个语音的名称、性别和语言。

import asyncio
import edge_tts
from edge_tts import VoicesManager

async def print_available_voices(language: str = "zh", gender: str = None) -> None:
    """
    异步查找并打印符合特定条件的语音列表。
    :param language: 语音的语言,如 "zh-CN" 表示中文
    :param gender: 可选参数,选择语音的性别("Male" 或 "Female"),默认不指定
    """
    # 异步获取所有可用语音
    voices = await VoicesManager.create()

    # 根据语言过滤语音
    filtered_voices = voices.find(Language=language)
    if gender:
        filtered_voices = [voice for voice in filtered_voices if voice["Gender"] == gender]
    
    # 打印符合条件的语音
    if filtered_voices:
        print(f"符合条件的语音:")
        for voice in filtered_voices:
            print(f"语音名称: {voice['Name']}, 性别: {voice['Gender']}, 语言: {voice['Language']}")
    else:
        print(f"没有找到符合条件的语音:语言={language}, 性别={gender}")

# 示例调用
async def main():
    await print_available_voices(language="zh", gender="Female")

# 运行异步示例
if __name__ == "__main__":
    asyncio.run(main())

更改语音参数

除了选择不同的音色外,edge-tts 还允许用户在合成时对语音的音量、语速、音调等参数进行调整。通过 Communicate 类中的 ratepitch 和 volume 参数,可以动态控制生成的语音效果。

import edge_tts

def generate_audio_with_custom_params(text: str, output_file: str, rate: str = "+0%", pitch: str = "+0Hz", volume: str = "+0%") -> None:
    """
    生成带有自定义语音参数的音频
    :param text: 需要合成的中文文本
    :param output_file: 输出的音频文件名
    :param rate: 语速调整(默认为 "+0%",表示标准语速)
    :param pitch: 音调调整(默认为 "+0Hz",表示标准音调)
    :param volume: 音量调整(默认为 "+0%",表示标准音量)
    """
    # 选择中文语音,这里使用的是小艺的 Neural 语音
    voice = "zh-CN-XiaoyiNeural"  
    
    # 使用 edge_tts.Communicate 创建语音对象,并传入自定义参数
    communicate = edge_tts.Communicate(text, voice, rate=rate, pitch=pitch, volume=volume)
    
    # 保存生成的音频文件
    communicate.save_sync(output_file)
    print(f"音频已生成,语速: {rate},音调: {pitch},音量: {volume}。")

# 示例调用
generate_audio_with_custom_params(
    "欢迎体验自定义语音合成!", 
    "custom_param_audio.wav", 
    rate="+50%", 
    pitch="+10Hz", 
    volume="-20%"
)

生成音频与字幕

在某些应用场景中,您可能需要同时生成音频和字幕,并根据需要选择同步或异步方式进行处理。这个章节展示了如何通过 edge-tts 实现同步和异步生成音频和字幕文件。执行后,会生成音频文件和对应的字幕文件。

import asyncio
import edge_tts

def process_audio_and_subtitles_sync(text: str, voice: str, output_file: str, srt_file: str) -> None:
    """
    同步生成音频并实时生成字幕
    :param text: 需要合成的中文文本
    :param voice: 使用的语音类型
    :param output_file: 输出的音频文件名
    :param srt_file: 输出的字幕文件名
    """
    communicate = edge_tts.Communicate(text, voice)
    submaker = edge_tts.SubMaker()

    # 同步生成音频并实时生成字幕
    with open(output_file, "wb") as audio_file:
        for chunk in communicate.stream_sync():
            if chunk["type"] == "audio":
                audio_file.write(chunk["data"])  # 写入音频数据
            elif chunk["type"] == "WordBoundary":
                submaker.feed(chunk)  # 处理字幕

    # 保存字幕文件
    with open(srt_file, "w", encoding="utf-8") as subtitle_file:
        subtitle_file.write(submaker.get_srt())

async def process_audio_and_subtitles_async(text: str, voice: str, output_file: str, srt_file: str) -> None:
    """
    异步生成音频并实时生成字幕
    :param text: 需要合成的中文文本
    :param voice: 使用的语音类型
    :param output_file: 输出的音频文件名
    :param srt_file: 输出的字幕文件名
    """
    # 异步调用同步版本的逻辑
    loop = asyncio.get_event_loop()
    await loop.run_in_executor(None, process_audio_and_subtitles_sync, text, voice, output_file, srt_file)

# 示例调用
process_audio_and_subtitles_sync("欢迎使用 Python 进行语音合成!", "zh-CN-XiaoyiNeural", "audio_sync.mp3", "audio_sync.srt")

# 异步调用
asyncio.run(process_audio_and_subtitles_async("这是一段测试语音和字幕生成的示例。", "zh-CN-XiaoyiNeural", "audio_async.mp3", "audio_async.srt"))

总结

通过本教程,您学习了如何使用 edge-tts 库实现文本到语音的转换。您通过不同的函数实现了以下功能:

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

您可能感兴趣的文章:
阅读全文