transcribe.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:audio-tagging-toolkit 作者: hipstas 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号