Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to open file file.wav as a WAV due to: file does not start with RIFF id

I am getting this error when trying to open a RIFF file (which as I understand it is a type of WAV) in python.

Failed to open file file.wav as a WAV due to: file does not start with RIFF id

When I inspect it with various tools which leads me to believe that it is really a WAV / RIFF file.

$ file file.wav 
file.wav: MBWF/RF64 audio, stereo 96000 Hz


$ file -i file.wav 
file.wav: audio/x-wav; charset=binary




$ mediainfo file.wav 
General
Complete name                            : file.wav
Format                                   : Wave
Format profile                           : RF64
File size                                : 4.10 GiB
Duration                                 : 2h 7mn
Overall bit rate mode                    : Constant
Overall bit rate                         : 4 608 Kbps

Audio
Format                                   : PCM
Format settings, Endianness              : Little
Format settings, Sign                    : Signed
Codec ID                                 : 1
Duration                                 : 2h 7mn
Bit rate mode                            : Constant
Bit rate                                 : 4 608 Kbps
Channel(s)                               : 2 channels
Sampling rate                            : 96.0 KHz
Bit depth                                : 24 bits
Stream size                              : 4.10 GiB (100%)
like image 337
Alex Avatar asked Sep 04 '14 18:09

Alex


People also ask

What is a riff ID?

January 2019. The Resource Interchange File Format (RIFF) is a generic file container format for storing data in tagged chunks. It is primarily used to store multimedia such as sound and video, though it may also be used to store any arbitrary data.

How do I open a .WAV file?

How to open a WAV file. Since WAV is quite a popular format, almost all devices today support it using built-in media players. On Windows, the Windows Media Player is capable of playing WAV files. On MacOS, iTunes or QuickTime can play WAV files.

Can windows open WAV files?

Similar to the AVI and ASF format, WAV is only a file container. Audio content that is compressed with a wide variety of codecs and that is stored in a . wav file can be played back in Windows Media Player if the appropriate codecs are installed on the computer. The most common audio codecs that are used in .

How do I read a WAV file in Python?

This function opens a file to read/write audio data. The function needs two parameters - first the file name and second the mode. The mode can be 'wb' for writing audio data or 'rb' for reading. A mode of 'rb' returns a Wave_read object, while a mode of 'wb' returns a Wave_write object.


3 Answers

What you have is a 64-bit RIFF. wave does not support 64-bit RIFF files.

like image 180
Ignacio Vazquez-Abrams Avatar answered Oct 05 '22 23:10

Ignacio Vazquez-Abrams


If your audio is okay, and you are able to read the file with librosa or scipy.io, we can simply read the file, write it back to a temporary wav file and then read it with the wave package again.

Example. Below, we get the RIFF id error.

>>> import wave
>>> wave.open('./SA1.WAV')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pytorch/anaconda3/lib/python3.6/wave.py", line 499, in open
    return Wave_read(f)
  File "/home/pytorch/anaconda3/lib/python3.6/wave.py", line 163, in __init__
    self.initfp(f)
  File "/home/pytorch/anaconda3/lib/python3.6/wave.py", line 130, in initfp
    raise Error('file does not start with RIFF id')
wave.Error: file does not start with RIFF id

We read into numpy with librosa, write back with soundfile.

import librosa
import soundfile as sf
>>> x,_ = librosa.load('./SA1.WAV', sr=16000)
>>> sf.write('tmp.wav', x, 16000)
>>> wave.open('tmp.wav','r')
<wave.Wave_read object at 0x7fbcb4c8cf28>
like image 27
kakrafoon Avatar answered Oct 05 '22 23:10

kakrafoon


Similar to @kakrafoon 's answer but using soundfile to both read and write (in case you care about limiting the number of external dependencies):

import soundfile
import wave

file_path = "your_file.wav"

# Read and rewrite the file with soundfile
data, samplerate = soundfile.read(file_path)
soundfile.write(file_path, data, samplerate)

# Now try to open the file with wave
with wave.open(file_path) as file:
    print('File opened!')
like image 32
skedwards88 Avatar answered Oct 06 '22 00:10

skedwards88