簡體   English   中英

無法將文件 file.wav 作為 WAV 打開,原因是:文件不以 RIFF id 開頭

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

嘗試在 python 中打開 RIFF 文件(據我所知它是一種 WAV)時出現此錯誤。

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

當我用各種工具檢查它這使我相信這一個真正的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%)

您擁有的是64 位 RIFF wave不支持 64 位 RIFF 文件。

如果您的音頻沒問題,並且您可以使用 librosa 或 scipy.io 讀取文件,我們可以簡單地讀取文件,將其寫回臨時 wav 文件,然后再次使用 wave 包讀取它。

例子。 下面,我們得到 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

我們用 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>

類似於@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM