Python/短视频合成自动化/edgetts.py

75 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
合成语音模块
"""
import asyncio
from hashlib import md5
from pathlib import Path
from typing import Tuple, Union
import edge_tts
from mutagen.mp3 import MP3
# pylint: disable=too-few-public-methods
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对象和持续时长单位为微秒
"""
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)}"
) from exception