[英]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.尝试在 python 中打开 RIFF 文件(据我所知它是一种 WAV)时出现此错误。
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.当我用各种工具检查它这使我相信这是一个真正的WAV / RIFF文件。
$ 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%)
What you have is a 64-bit RIFF .您拥有的是64 位 RIFF 。 wave
does not support 64-bit RIFF files. wave
不支持 64 位 RIFF 文件。
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.如果您的音频没问题,并且您可以使用 librosa 或 scipy.io 读取文件,我们可以简单地读取文件,将其写回临时 wav 文件,然后再次使用 wave 包读取它。
Example.例子。 Below, we get the RIFF id error.下面,我们得到 RIFF id 错误。
>>> 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.我们用 librosa 读入 numpy,用 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>
Similar to @kakrafoon 's answer but using soundfile
to both read and write (in case you care about limiting the number of external dependencies):类似于@kakrafoon 的答案,但使用soundfile
进行读写(以防您关心限制外部依赖项的数量):
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!')
我有一个单词,我将文件的后缀重命名为“mp3”并将其转换为“wav”,然后我就可以阅读它了。
subprocess.call(['ffmpeg', '-i', 'XXX.mp3', 'XXX.wav'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.