【问题标题】:Librosa cannot load from BytesIOLibrosa 无法从 BytesIO 加载
【发布时间】:2020-06-26 19:08:28
【问题描述】:

我目前正在尝试创建一个用于深度学习的大型数据集,其中包含许多存储在一起的压缩 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: 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

【问题讨论】:

  • 由很多压缩的 mp3 文件存储在一起 你确定你可以像这样连接所有的文件吗?结果是有效的 MP3 文件还是其他可识别的格式?
  • 我认为这无关紧要。我不是试图将连接文件加载为有效的 mp3 文件,而只是加载前 N 个字节,这与第一个文件中的完全相同。 (准确地说是 362861 字节)

标签: python python-3.x librosa


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多