在Python中将音频转换为文本时出错

2024-05-20 22:53:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将音频文件转换为文本。音频保存为桌面上的Wel.wav。在

代码如下:

import speech_recognition as sr

r = sr.Recognizer()

audio = 'Wel.wav'

with sr.AudioFile(audio) as source:
audio = r.record(source)
print ('Done!')

try:
text = r.recognize_google(audio)
print (text)

except Exception as e:
print (e)

我得到以下错误:

^{pr2}$

现在当我试着告诉它目录位置时

我把音频命令改成

audio = 'C:\\Users\\user\\Desktop\\Wel.wav'

它抛出以下错误:

Error                                     Traceback (most recent call last)
C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py in 
__enter__(self)
202             # attempt to read the file as WAV
--> 203             self.audio_reader = 
wave.open(self.filename_or_fileobject, "rb")
204             self.little_endian = True  # RIFF WAV is a little-endian 
format (most ``audioop`` operations assume that the frames are stored in 
little-endian form)

C:\Users\user\Anaconda3\lib\wave.py in open(f, mode)
498     if mode in ('r', 'rb'):
 --> 499         return Wave_read(f)
500     elif mode in ('w', 'wb'):

C:\Users\user\Anaconda3\lib\wave.py in __init__(self, f)
162         try:
--> 163             self.initfp(f)
164         except:

C:\Users\user\Anaconda3\lib\wave.py in initfp(self, file)
129         if self._file.getname() != b'RIFF':
--> 130             raise Error('file does not start with RIFF id')
131         if self._file.read(4) != b'WAVE':

Error: file does not start with RIFF id

During handling of the above exception, another exception occurred:

Error                                     Traceback (most recent call last)
C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py in 
   __enter__(self)
207                 # attempt to read the file as AIFF
--> 208                 self.audio_reader = 
aifc.open(self.filename_or_fileobject, "rb")
209                 self.little_endian = False  # AIFF is a big-endian 
format

 C:\Users\user\Anaconda3\lib\aifc.py in open(f, mode)
889     if mode in ('r', 'rb'):
--> 890         return Aifc_read(f)
891     elif mode in ('w', 'wb'):

C:\Users\user\Anaconda3\lib\aifc.py in __init__(self, f)
339         # else, assume it is an open file object already
--> 340         self.initfp(f)
341 

C:\Users\user\Anaconda3\lib\aifc.py in initfp(self, file)
304         if chunk.getname() != b'FORM':
--> 305             raise Error('file does not start with FORM id')
306         formdata = chunk.read(4)

Error: file does not start with FORM id

During handling of the above exception, another exception occurred:

EOFError                                  Traceback (most recent call last)
C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py in 
__enter__(self)
233                 try:
--> 234                     self.audio_reader = aifc.open(aiff_file, "rb")
235                 except (aifc.Error, EOFError):

C:\Users\user\Anaconda3\lib\aifc.py in open(f, mode)
889     if mode in ('r', 'rb'):
 --> 890         return Aifc_read(f)
891     elif mode in ('w', 'wb'):

C:\Users\user\Anaconda3\lib\aifc.py in __init__(self, f)
339         # else, assume it is an open file object already
 --> 340         self.initfp(f)
341 

C:\Users\user\Anaconda3\lib\aifc.py in initfp(self, file)
302         self._file = file
 --> 303         chunk = Chunk(file)
304         if chunk.getname() != b'FORM':

 C:\Users\user\Anaconda3\lib\chunk.py in __init__(self, file, align, 
 bigendian, inclheader)
 62         if len(self.chunkname) < 4:
 ---> 63             raise EOFError
 64         try:

 EOFError: 

 During handling of the above exception, another exception occurred:

 ValueError                                Traceback (most recent call last)
 <ipython-input-29-5bee547ee15e> in <module>()
  5 audio = 'C:\\Users\\user\\Desktop\\Wel.wav'
  6 
   ----> 7 with sr.AudioFile(audio) as source:
  8     audio = r.record(source)
  9     print ('Done!')

  C:\Users\user\Anaconda3\lib\site-packages\speech_recognition\__init__.py 
  in __enter__(self)
234                     self.audio_reader = aifc.open(aiff_file, "rb")
235                 except (aifc.Error, EOFError):
 --> 236                     raise ValueError("Audio file could not be read 
as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in 
another format")
237                 self.little_endian = False  # AIFF is a big-endian 
format
238         assert 1 <= self.audio_reader.getnchannels() <= 2, "Audio must 
be mono or stereo"

ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native 
FLAC; check if file is corrupted or in another format

有谁能帮我一下吗。在


Tags: inpyselfreadifmodelibas