# =======================================
# _ __ __ __ _
# | |/ /___ | \/ | ___ __| |___
# | ' // _ \ | |\/| |/ _ \ / _` / __|
# | . \ __/ | | | | (_) | (_| \__ \
# |_|\_\___| |_| |_|\___/ \__,_|___/
# @ke_mods
# =======================================
# meta developer: @ke_mods
# scope: ffmpeg
# requires: pydub SpeechRecognition
from .. import loader, utils
import os
import speech_recognition as sr
from pydub import AudioSegment
@loader.tds
class VoiceToTextMod(loader.Module):
strings = {
"name": "VoiceToText",
"process_text": "✨ Recognizing the message text...",
"vtt_success": "🔥 Recognized text:\n
{}
",
"vtt_failure": "🚫 Failed to recognize the message.",
"vtt_request_error": "🚫 Error when contacting the recognition service:\n{}",
"vtt_invalid": "🚫 Please reply to a voice or video message with the command {}vtt",
"vtt_successful": "✅ Text recognized successfully",
}
strings_ru = {
"process_text": "✨ Распознаю текст сообщения...",
"vtt_success": "🔥 Распознанный текст:\n{}
",
"vtt_failure": "🚫 Не удалось распознать сообщение.",
"vtt_request_error": "🚫 Ошибка при обращении к сервису распознавания:\n{}",
"vtt_invalid": "🚫 Пожалуйста, ответьте на голосовое или видеосообщение командой {}vtt",
"vtt_successful": "✅ Текст успешно распознан",
}
@loader.command(
ru_doc="- распознает текст из голосового или видеосообщения.",
)
async def vttcmd(self, message):
"""- recognizes text from voice or video messages."""
reply = await message.get_reply_message()
if not reply or not (reply.voice or reply.video_note):
await utils.answer(message, self.strings["vtt_invalid"].format(self.get_prefix()))
return
msg = await utils.answer(
message, self.strings["process_text"], reply_to=message.id
)
media_file = await reply.download_media()
wav_file = media_file.replace('.mp4', '.wav') if reply.video_note else media_file.replace('.oga', '.wav')
try:
AudioSegment.from_file(media_file).export(wav_file, format='wav')
recognizer = sr.Recognizer()
with sr.AudioFile(wav_file) as source:
audio_data = recognizer.record(source)
try:
text = recognizer.recognize_google(audio_data, language='ru-RU')
await utils.answer(msg, self.strings["vtt_success"].format(text))
except sr.UnknownValueError:
await utils.answer(msg, self.strings["vtt_failure"])
except sr.RequestError as e:
await utils.answer(msg, self.strings["vtt_request_error"].format(e))
finally:
os.remove(media_file)
os.remove(wav_file)