forked from TeamUltroid/UltroidAddons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeechtool.py
More file actions
82 lines (72 loc) · 2.1 KB
/
speechtool.py
File metadata and controls
82 lines (72 loc) · 2.1 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#
# Ultroid - UserBot
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
#
# Ported from Telebot
"""
✘ Commands Available -
• {i}tts LanguageCode <reply to a message>
• {i}tts LangaugeCode | text to speak
"""
import asyncio
import os
import subprocess
from datetime import datetime
from gtts import gTTS
from . import *
@ultroid_cmd(
pattern="tts ?(.*)",
)
async def _(event):
input_str = event.pattern_match.group(1)
start = datetime.now()
if event.reply_to_msg_id:
previous_message = await event.get_reply_message()
text = previous_message.message
lan = input_str
elif "|" in input_str:
lan, text = input_str.split("|")
else:
await eor(event, "Invalid Syntax. Module stopping.")
return
text = text.strip()
lan = lan.strip()
if not os.path.isdir("downloads/"):
os.makedirs("downloads/")
required_file_name = "downloads/voice.ogg"
try:
tts = gTTS(text, lang=lan)
tts.save(required_file_name)
command_to_execute = [
"ffmpeg",
"-i",
required_file_name,
"-map",
"0:a",
"-codec:a",
"libopus",
"-b:a",
"100k",
"-vbr",
"on",
required_file_name + ".opus",
]
try:
subprocess.check_output(command_to_execute, stderr=subprocess.STDOUT)
except (subprocess.CalledProcessError, NameError, FileNotFoundError) as exc:
await eor(event, str(exc))
else:
os.remove(required_file_name)
required_file_name = required_file_name + ".opus"
end = datetime.now()
ms = (end - start).seconds
await event.reply(
file=required_file_name,
)
os.remove(required_file_name)
await eod(event, "Processed {} ({}) in {} seconds!".format(text[0:97], lan, ms))
except Exception as e:
await eor(event, str(e))