speech_recognition: 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

code

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

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

error

Traceback (most recent call last):
  File "/Users/robiulislam/anaconda3/lib/python3.6/site-packages/speech_recognition/__init__.py", line 203, in __enter__
    self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
  File "/Users/robiulislam/anaconda3/lib/python3.6/wave.py", line 499, in open
    return Wave_read(f)
  File "/Users/robiulislam/anaconda3/lib/python3.6/wave.py", line 163, in __init__
    self.initfp(f)
  File "/Users/robiulislam/anaconda3/lib/python3.6/wave.py", line 143, in initfp
    self._read_fmt_chunk(chunk)
  File "/Users/robiulislam/anaconda3/lib/python3.6/wave.py", line 260, in _read_fmt_chunk
    raise Error('unknown format: %r' % (wFormatTag,))
wave.Error: unknown format: 65534

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/robiulislam/anaconda3/lib/python3.6/site-packages/speech_recognition/__init__.py", line 208, in __enter__
    self.audio_reader = aifc.open(self.filename_or_fileobject, "rb")
  File "/Users/robiulislam/anaconda3/lib/python3.6/aifc.py", line 912, in open
    return Aifc_read(f)
  File "/Users/robiulislam/anaconda3/lib/python3.6/aifc.py", line 351, in __init__
    self.initfp(file_object)
  File "/Users/robiulislam/anaconda3/lib/python3.6/aifc.py", line 316, in initfp
    raise Error('file does not start with FORM id')
aifc.Error: file does not start with FORM id

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/robiulislam/anaconda3/lib/python3.6/site-packages/speech_recognition/__init__.py", line 234, in __enter__
    self.audio_reader = aifc.open(aiff_file, "rb")
  File "/Users/robiulislam/anaconda3/lib/python3.6/aifc.py", line 912, in open
    return Aifc_read(f)
  File "/Users/robiulislam/anaconda3/lib/python3.6/aifc.py", line 357, in __init__
    self.initfp(f)
  File "/Users/robiulislam/anaconda3/lib/python3.6/aifc.py", line 314, in initfp
    chunk = Chunk(file)
  File "/Users/robiulislam/anaconda3/lib/python3.6/chunk.py", line 63, in __init__
    raise EOFError
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 21, in <module>
    with sr.AudioFile("test.wav") as source:
  File "/Users/robiulislam/anaconda3/lib/python3.6/site-packages/speech_recognition/__init__.py", line 236, in __enter__
    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")
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

operating system is Mac & python version 3

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 10
  • Comments: 23

Most upvoted comments

These manipulations helped me to restore the .wav file and the script work!

import soundfile
data, samplerate = soundfile.read('old.wav')
soundfile.write('new.wav', data, samplerate, subtype='PCM_16')

I fixed it You got the same problem if youre using python 3.7 and above if you use python 3.6 you can run the code

I’m an idiot. It was an MP3 file, just used pydub to convert.

I’m not sure this is an issue with the code as much as it is the file you are using. The error states it’s looking for a PCM WAV (or other alternative). PCM is one of many ways to encode a WAV file. The files that aren’t working for you must likely need to be encoded differently.

Check out the section titled “Comparison of coding schemes” on this wiki entry for wav files for additional details:

https://en.m.wikipedia.org/wiki/WAV

Here’s a quick excerpt from that section:

“Audio in WAV files can be encoded in a variety of audio coding formats, such as GSM or MP3, to reduce the file size.

This is a reference to compare the monophonic (not stereophonic) audio quality and compression bitrates of audio coding formats available for WAV files including PCM, ADPCM, Microsoft GSM 06.10, CELP, SBC, Truespeech and MPEG Layer-3.”

As @BrendanCarlin said, this seemed to be a problem with the files that are being used. Maybe they are somehow corrupted. Even if this is not the case, the stack trace shows that the error comes from the wave.py library so it is not really an issue with the speech_recognition’s code. Closing the issue for now but feel free to post solution/suggestion/question if you can’t get it to work.

My python3.8.1 got the same problem