python edge_tts(文本转音频)

1. 安装插件edge-tts

pip install edge-tts

2.文本转音频

"""
    :param voice: 指定声音名称
    :param content: 文本内容
    :param audioFile: 音频输出文件
    :param vttFile: 字幕输出文件
"""
   
async def create_tts_mp3(voice: str, content: str, audioFile: str, vttFile:str) -> None:  
    communicate = edge_tts.Communicate(content, voice)  
    submaker = edge_tts.SubMaker()  
    with open(audioFile, "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(vttFile, "w", encoding="utf-8") as file:  
        file.write(submaker.generate_subs())

3.voice选择说明

edge-tts --list-voices

python edge_tts(文本转音频)_第1张图片

你可能感兴趣的:(python,音视频)