1
import speech_recognition as sr
import subprocess
import urllib
from urllib.request import urlopen
if update.message.body.attachments and update.message.body.attachments[0].type == 'audio':
url = update.message.body.attachments[0].payload.url
urllib.request.urlretrieve(url, 'temp.mp3')
subprocess.call(['ffmpeg', '-y', '-i', 'temp.mp3', 'temp.wav'])
r = sr.Recognizer()
with sr.AudioFile('temp.wav') as source:
audio = r.record(source)
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print("Google Speech Recognition thinks you said " + r.recognize_google(audio, language='ru'))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))