def transcribe(inputfile,outputfile='',to_txt=True):
wav_source=True
if inputfile.lower()[-4:]!='.wav': # Creates a temporary WAV
wav_source=False # if input is MP3
temp_filename=inputfile.split('/')[-1]+'_temp.wav'
wav_path='/var/tmp/'+temp_filename # Pathname for temp WAV
subprocess.call(['ffmpeg', '-y', '-i', inputfile, wav_path]) # '-y' option overwrites existing file if present
else:
wav_path=inputfile
transcript=''
r = sr.Recognizer()
with sr.AudioFile(wav_path) as source:
audio = r.record(source) # read the entire audio file
try: # recognize speech using Sphinx
print('Processing ...')
transcript=r.recognize_sphinx(audio)
except sr.UnknownValueError:
print("Sphinx error: No speech detected.")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
if wav_source==False:
os.remove(wav_path) # deleting temp WAV
if to_txt==True:
if outputfile=='':
outputfile=inputfile[:-4]+'.pocketsphinx.txt'
with open(outputfile, 'w') as fo:
fo.write(transcript)
return transcript
else:
return transcript
评论列表
文章目录