Librosa read MP3 audio from URL

Viewed 2988

I am trying to read Mp3 audio from URL using Librosa. I understand that Librosa first uses PySoundFile to load the audio and if that fails it then uses audioread.

I have the following code so far:

import librosa
import io
from six.moves.urllib.request import urlopen

url = "https://sneezoramafunction.blob.core.windows.net/usersounds/00bd9b8c914947d48724fd7e0c88041b.mp3"

data, samplerate = librosa.load(io.BytesIO(urlopen(url).read()))

But this gives me the following error:

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

Additionally, I would be deploying this on Azure ML service and would not have any local storage available.

Thank you all for any help/advice

2 Answers

librosa uses soundfile and audioread to load audio files. Note that soundfile does not currently support MP3, which will cause librosa to fall back on the audioread library.

I don't think Librosa can take an object of type BytesIO and process it by itself. You may need to save it locally (wav/mp3) and then load using Librosa.

z = io.BytesIO(urlopen(url).read())
import pathlib
pathlib.Path(('/home/<name>/sneeze.wav').write_bytes(z.getbuffer())
data, sr = librosa.load('/home/<name>/sneeze.wav')
Related