# -*- coding: utf-8 -*- import asyncio from _md5 import md5 from pathlib import Path from typing import Tuple, Union import edge_tts from mutagen.mp3 import MP3 class EdgeTTS: """ 封装EdgeTTS,支持: 1、根据文本合成语音并将语音文件保存至指定文件夹内 """ def __init__( self, folder_path: Union[str, Path], ): """ 初始化 :param folder_path: 语音文件保存文件夹路径 """ # 指定文件夹路径(path对象) self.folder_path = ( folder_path if isinstance(folder_path, Path) else Path(folder_path) ) def synthetize( self, text: str, timbre: str, rate: str, volume: str, ) -> Tuple[Path, int]: """ 根据文本合成语音并将语音文件保存至指定文件夹内 :param text: 文本 :param timbre: 音色名称 :param rate: 语速 :param volume: 音量 :return 语音文件路径(path对象)和持续时长(单位为微秒) """ # noinspection PyBroadException try: # 异步处理方法 async def _async_synthetize(): # 构造语音文件名称 file_name = f"{md5((text + timbre + rate + volume).encode("utf-8")) .hexdigest() .upper()}.mp3" # 构造语音文件路径 file_path = self.folder_path / file_name communicator = edge_tts.Communicate( text=text.replace("\n", ""), voice=timbre, rate=rate, volume=volume, ) await communicator.save(file_path.as_posix()) # 持续时长(单位为微秒) duration = int(round(MP3(file_path.as_posix()).info.length * 1000000)) return file_path, duration return asyncio.run(_async_synthetize()) except Exception as exception: raise RuntimeError( f"根据文本合成语音并将语音文件保存至指定文件夹内发生异常:{str(exception)}" )