feat: 添加edge-tts语音生成
This commit is contained in:
parent
494b078a09
commit
7c71eafa41
|
@ -3,6 +3,8 @@ from django.conf import settings
|
||||||
import uuid
|
import uuid
|
||||||
import os
|
import os
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
import edge_tts
|
||||||
|
from asgiref.sync import async_to_sync
|
||||||
|
|
||||||
|
|
||||||
def generate_voice(msg: str, per: int = 0):
|
def generate_voice(msg: str, per: int = 0):
|
||||||
|
@ -17,16 +19,29 @@ def generate_voice(msg: str, per: int = 0):
|
||||||
str: 地址
|
str: 地址
|
||||||
dict: result
|
dict: result
|
||||||
"""
|
"""
|
||||||
client = AipSpeech(settings.BD_SP_ID, settings.BD_SP_KEY, settings.BD_SP_SECRET)
|
|
||||||
result = client.synthesis(msg, 'zh', 1, {'vol': 5, 'spd': 5, 'per': per})
|
|
||||||
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
|
|
||||||
if not isinstance(result, dict):
|
|
||||||
file_name = '{}.mp3'.format(uuid.uuid4())
|
file_name = '{}.mp3'.format(uuid.uuid4())
|
||||||
path = '/media/' + timezone.now().strftime('%Y/%m/%d/')
|
path = '/media/' + timezone.now().strftime('%Y/%m/%d/')
|
||||||
full_path = settings.BASE_DIR + path
|
full_path = settings.BASE_DIR + path
|
||||||
if not os.path.exists(full_path):
|
if not os.path.exists(full_path):
|
||||||
os.makedirs(full_path)
|
os.makedirs(full_path)
|
||||||
|
result = None
|
||||||
|
if getattr(settings, 'BD_SP_ENABLED', False):
|
||||||
|
client = AipSpeech(settings.BD_SP_ID, settings.BD_SP_KEY, settings.BD_SP_SECRET)
|
||||||
|
result = client.synthesis(msg, 'zh', 1, {'vol': 5, 'spd': 5, 'per': per})
|
||||||
|
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
|
||||||
|
if not isinstance(result, dict):
|
||||||
with open(full_path + file_name, 'wb') as f:
|
with open(full_path + file_name, 'wb') as f:
|
||||||
f.write(result)
|
f.write(result)
|
||||||
return True, path + file_name, None
|
return True, path + file_name, None
|
||||||
|
elif getattr(settings, 'EDGE_TTS_ENABLED', False):
|
||||||
|
if per == 0:
|
||||||
|
per = 'zh-CN-XiaoxiaoNeural'
|
||||||
|
else:
|
||||||
|
per = 'zh-CN-YunjianNeural'
|
||||||
|
communicate = edge_tts.Communicate(msg, per)
|
||||||
|
try:
|
||||||
|
async_to_sync(communicate.save(full_path + file_name))
|
||||||
|
return True, path + file_name, None
|
||||||
|
except Exception as e:
|
||||||
|
result = str(e)
|
||||||
return False, None, result
|
return False, None, result
|
||||||
|
|
|
@ -34,3 +34,4 @@ pymysql==1.0.3
|
||||||
docxtpl==0.16.7
|
docxtpl==0.16.7
|
||||||
paho-mqtt==2.0.0
|
paho-mqtt==2.0.0
|
||||||
# deepface==0.0.79
|
# deepface==0.0.79
|
||||||
|
# edge-tts==6.1.12
|
||||||
|
|
Loading…
Reference in New Issue