转换mp3文件时出现Python语音识别错误

问题描述

我第一次尝试将音频转换为文本。

import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile("/path/to/.mp3") as source:
     audio = r.record(source)

当我执行上面的代码时,出现以下错误

<ipython-input-10-72e982ecb706> in <module>()
----> 1 with sr.AudioFile("/home/yogaraj/Documents/Python workouts/Python audio to text/show_me_the_meaning.mp3") as source:
      2     audio = sr.record(source)
      3 

/usr/lib/python2.7/site-packages/speech_recognition/__init__.pyc in __enter__(self)
    197                 aiff_file = io.BytesIO(aiff_data)
    198                 try:
--> 199                     self.audio_reader = aifc.open(aiff_file, "rb")
    200                 except aifc.Error:
    201                     assert False, "Audio file could not be read as WAV, AIFF, or FLAC; check if file is corrupted"

/usr/lib64/python2.7/aifc.pyc in open(f, mode)
    950             mode = 'rb'
    951     if mode in ('r', 'rb'):
--> 952         return Aifc_read(f)
    953     elif mode in ('w', 'wb'):
    954         return Aifc_write(f)

/usr/lib64/python2.7/aifc.pyc in __init__(self, f)
    345             f = __builtin__.open(f, 'rb')
    346         # else, assume it is an open file object already
--> 347         self.initfp(f)
    348 
    349     #

/usr/lib64/python2.7/aifc.pyc in initfp(self, file)
    296         self._soundpos = 0
    297         self._file = file
--> 298         chunk = Chunk(file)
    299         if chunk.getname() != 'FORM':
    300             raise Error, 'file does not start with FORM id'

/usr/lib64/python2.7/chunk.py in __init__(self, file, align, bigendian, inclheader)
     61         self.chunkname = file.read(4)
     62         if len(self.chunkname) < 4:
---> 63             raise EOFError
     64         try:
     65             self.chunksize = struct.unpack(strflag+'L', file.read(4))[0]

我不知道我出了什么问题。有人能告诉我我在上面的代码中哪里错了吗?


解决方案

Speech recognition支持wav文件格式。 以下是使用speech_recognition

的WAV转文本程序示例

示例代码(Python3)

import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile("woman1_wb.wav") as source:
    audio = r.record(source)

try:
    s = r.recognize_google(audio)
    print("Text: "+s)
except Exception as e:
    print("Exception: "+str(e))

输出:

Text: to administer medicine to animals is frequency of very difficult matter and yet sometimes it's necessary to do so

使用的wav文件URL:http://www-mobile.ecs.soton.ac.uk/hth97r/links/Database/woman1_wb.wav

相关文章