# Python program spr.py # speech to text from audio file voice.wav import speech_recognition as sr from sys import exit from time import strftime print("\n"+__file__) # use a WAV format audio file with German speech: f = "/home/ahg/audio/voice.wav" # ADAPT THE PATH ! print("transcribe to text, use audio file",f,"\n") # (audio file formats: WAV, AIFF, AIFF-C or FLAC) r = sr.Recognizer() # create Recognizer instance a = sr.AudioFile(f) # speech_recognition.AudioFile # audio file -> speech_recognition.audio.AudioData with a as source: audio = r.record(source) # languages: en-US (US Englisch, default) # de_DE (German), zh-CN (Chinese), ... try: #send AudioData instance to Google Web Speech API text = r.recognize_google( audio_data=audio,language="de-DE") except LookupError: print("Sorry, the speech was not understood.") exit(1) print(text) # show the recognized text on screen timetext = strftime("%y%m%d%H%M%S") # build a name of a text file with a time stamp n = "voice_"+timetext+".txt" # write recognized text to the file named above with open(n, "w") as x: x.write(text) print("\nText file",n,"written.\n")