-
Notifications
You must be signed in to change notification settings - Fork 5
/
mytts.py
46 lines (40 loc) · 1.89 KB
/
mytts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os, re, edge_tts, asyncio, random, string
class Mytts:
def __init__(self):
self.SAVE_DIR = "/var/www/html/defaultwww/fq/mp3/"
self.SAVE_EXT = ".mp3"
if not os.path.exists(self.SAVE_DIR):
os.makedirs(self.SAVE_DIR)
def remove_html_tags(self, text):
"""去除字符串中的所有HTML标签"""
return re.sub(r'<.*?>', '', text)
def generate_random_string(self, length=10):
letters = string.ascii_letters + string.digits
ret = ''
for _ in range(length):
ret += random.choice(letters)
return ret
async def savetofile(self, text, aid=""):
"""Main function"""
if aid == "":
aid = self.generate_random_string()
fnam = aid + self.SAVE_EXT
savepath = self.SAVE_DIR + fnam
if os.path.exists(savepath):
return fnam
txt = self.remove_html_tags(text)
#print(txt)
voice = "zh-CN-XiaoxiaoNeural"
communicate = edge_tts.Communicate(text=txt, voice=voice, rate="+60%")
#communicate.save_sync(savepath)
#await communicate.save(savepath)
with open(savepath, "wb") as file:
async for chunk in communicate.stream():
if chunk["type"] == "audio":
file.write(chunk["data"])
return fnam
def getmp3(self, text, aid=""):
fnam = asyncio.run(self.savetofile(text, aid))
return fnam
#fnam = Mytts().getmp3("""<p class="yinwen">石守信等人这才听出了赵匡胤话中有话,连忙离席叩头说:“陛下何出此言?现在天命已定,谁还敢有异心?”赵匡胤神情开始严肃起来:“朕并非说诸位有什么异心,可是诸位想一想,倘若你们的部下想要富贵,一旦把黄袍加在你身上,你即使不想当皇帝,到时也身不由己了。朕就是先例呀!”</p>""")
#print(fnam)