Search code examples
pythonffmpegspeech-recognitionspeech-to-text

Unable to read .wav file in python


I am using speech_recognition to read a .wav file using the following code :

r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
        audio = r.record(source)

However, I get the following error: file does not start with RIFF id

I tried the following solutions and use the below code, but I end up with new errors:

Solution 1 Code:

import librosa
import soundfile as sf
x,_ = librosa.load('sample_wav.WAV', sr=16000)

Error : Error opening 'C:\\Users\\biswankar.das\\Downloads\\sample_wav.WAV': File contains data in an unknown format

Solution 2 Code:

from scipy.io import wavfile
samplerate, data = wavfile.read(file_path)

error : File format b'\xff\xe3\x18\xc4' not understood. Only 'RIFF' and 'RIFX' supported.

I tried analyzing this file online, the format is MPEG, following are the details: ANALYSIS DETAILS:

General
Format : MPEG Audio
File size : 246 KiB
Duration : 4 min 11 s
Overall bit rate mode : Constant
Overall bit rate : 8 000 b/s
FileExtension_Invalid : m1a mpa mpa1 mp1 m2a mpa2 mp2 mp3

Audio
Format : MPEG Audio
Format version : Version 2.5
Format profile : Layer 3
Duration : 4 min 11 s
Bit rate mode : Constant
Bit rate : 8 000 b/s
Channel(s) : 1 channel
Sampling rate : 8 000 Hz
Frame rate : 13.889 FPS (576 SPF)
Compression mode : Lossy
Stream size : 246 KiB (100%)

I tried using ffmpeg as well using the below code, but I get an error while trying the same :

import pydub as pydub
from pydub import AudioSegment
AudioSegment.ffmpeg = "\\ffmpeg.exe"
pydub.AudioSegment.converter = r"\\ffmpeg.exe"
data = AudioSegment.from_wav("sample_wav.wav")

Error: The system cannot find the file specified - Altho I can read the same file location


Solution

  • The error file does not start with RIFF id means that your file is not supported by wave so you have to use a different file altogether. It might be because the file might not be a .wav file. Look at this post for more details- Failed to open file file.wav as a WAV due to: file does not start with RIFF id.