Librosa 无法从 BytesIO 加载

Jon*_*n R 6 python python-3.x librosa

我目前正在尝试创建一个用于深度学习的大型数据集,其中包含存储在一起的大量压缩 mp3 文件,因此我没有必须单独加载的 100k 文件。

x = b''
with open("file1.mp3", "rb") as f:
    x += f.read()
print(len(x)) # 362861
with open("file2.mp3", "rb") as f:
    x += f.read()
print(len(x)) # 725722
with open("testdataset", 'wb+') as f:
    f.write(x)
Run Code Online (Sandbox Code Playgroud)

现在我想一一加载:

with open("testdataset", 'rb') as f:
    bs = f.read(362861)
    y, sr = librosa.core.load(io.BytesIO(bs), mono=True, sr=44100, dtype=np.float32) # crahes
Run Code Online (Sandbox Code Playgroud)

它因以下错误而中断:

RuntimeError: Error opening <_io.BytesIO object at 0x7f509ed1cf90>: File contains data in an unknown format.

为了进行测试,我尝试加载原始文件,效果很好:

y, sr = librosa.core.load("file1.mp3", mono=True, sr=44100, dtype=np.float32) # works fine

请注意,原始 mp3 的“虚拟”加载也会引发警告:

UserWarning: PySoundFile failed. Trying audioread instead. warnings.warn('PySoundFile failed. Trying audioread instead.')

为什么会发生这种情况?是否有更好的方法将大量单独的文件存储在一起并立即加载它们?

以下是我正在使用的版本:

python: 3.8.3 (default, May 14 2020, 20:11:43) 
[GCC 7.5.0]
librosa: 0.7.2
audioread: 2.1.8
numpy: 1.19.0
scipy: 1.5.0
sklearn: 0.23.1
joblib: 0.15.1
decorator: 4.4.2
six: 1.15.0
soundfile: 0.10.3
resampy: 0.2.2
numba: 0.48.0
Run Code Online (Sandbox Code Playgroud)

Net*_*nel -1

librosa使用soundfile,不支持 mp3 文件(/encoding)

https://librosa.org/doc/main/ generated/librosa.load.html