我目前正在尝试为深度学习创建一个大型数据集,其中包含许多压缩的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)
现在我想逐个加载这些内容:
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
遇到以下错误:
RuntimeError: 无法打开 <_io.BytesIO object at 0x7f509ed1cf90>: 文件包含未知格式的数据。
为了测试,我尝试加载原始文件,结果可以正常工作:
y、sr = librosa.core.load("file1.mp3", mono=True, sr=44100, dtype=np.float32) # 可以正常工作
请注意,这个对原始mp3进行虚拟加载时还会引发警告:
UserWarning: PySoundFile 失败。尝试使用audioread。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
audioread
可能会发出需要文件名的 shell 命令。 - bfontaine