Librosa cannot load from BytesIO

Viewed 1272

I am currently trying to create a large dataset for deep learning consisting of a lot of compressed mp3 files stored together so I dont have 100k files that I have to load individually.

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)

Now I want to load this one by one:

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

It breaks with the following error:

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

For testing I tried to load the original file, which works fine:

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

Note that this "dummy"-load of the original mp3 also throws a warning:

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

Why is this happening? Is there maybe a better way to store a lot of seperate-files together and load them at once?

Here are the versions that I am using:

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
2 Answers

If you are using torchaudio, do: !pip install torch==1.11.0 torchaudio==0.11.0 -f https://download.pytorch.org/whl/cu113/torch_stable.html

Related