открытие сырой версии
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import subprocess
|
||||
import speech_recognition as sr
|
||||
import os
|
||||
from functions.loging import loging
|
||||
|
||||
|
||||
class Converter:
|
||||
def __init__(self, path_to_file: str, message, language: str = "ru-RU"):
|
||||
self.language = language
|
||||
self.path_to_file = path_to_file
|
||||
self.service = None
|
||||
self.message = message
|
||||
|
||||
def audio_to_text(self) -> str:
|
||||
import mutagen
|
||||
f = mutagen.File(self.path_to_file)
|
||||
if float(f.info.length) >= 30.0:
|
||||
self.service = "google"
|
||||
return self.google()
|
||||
else:
|
||||
self.service = "yandex"
|
||||
return self.yandex()
|
||||
|
||||
def google(self) -> str:
|
||||
subprocess.run(['ffmpeg', '-v', 'quiet', '-i', self.path_to_file, self.path_to_file.replace(".ogg", ".wav")])
|
||||
os.remove(self.path_to_file)
|
||||
wav_file = self.path_to_file.replace(".ogg", ".wav")
|
||||
r = sr.Recognizer()
|
||||
with sr.AudioFile(wav_file) as source:
|
||||
audio = r.record(source)
|
||||
r.adjust_for_ambient_noise(source)
|
||||
response = "google\n\n" + r.recognize_google(audio, language=self.language)
|
||||
loging(self.message, "google", wav_file, response)
|
||||
os.remove(wav_file)
|
||||
return response
|
||||
|
||||
def yandex(self) -> str:
|
||||
from speechkit import ShortAudioRecognition, Session
|
||||
reg = ShortAudioRecognition(Session.from_api_key("AQVN3xNJamAFP4_FS6Gis0Uud0vONFk24umBSXvh"))
|
||||
response = "yandex\n\n" + reg.recognize(open(str(self.path_to_file), str('rb')).read())
|
||||
loging(self.message, "yandex", self.path_to_file, response)
|
||||
os.remove(self.path_to_file)
|
||||
return response
|
||||
Reference in New Issue
Block a user