Потребуется
ffmpeg установить из
ffmpeg
Распаковать и прописать в PATH
from subprocess import Popen
from speech_recognition import (Recognizer, AudioFile)
from speech_recognition import (UnknownValueError, RequestError)
class SpeechOggAudioFileToText:
def __init__(self):
self.recognizer = Recognizer()
def ogg_to_wav(self, file):
args = ['ffmpeg','-i', file, 'test.wav']
process = Popen(args)
process.wait()
@property
def text(self):
AUDIO_FILE = 'test.wav'
with AudioFile(AUDIO_FILE) as source:
audio = self.recognizer.record(source)
try:
text = self.recognizer.recognize_google(audio, language='RU')
return text
except UnknownValueError:
print("Не удаётся распознать аудио файл")
except RequestError as error:
print("Не удалось запросить результаты: {0}".format(error))
def main():
speech_ogg = SpeechOggAudioFileToText()
speech_ogg.ogg_to_wav('test.ogg')
print(speech_ogg.text)
if __name__ == '__main__':
main()