I am trying to sample (convert analog to digital) mp3 files via the following Python code using the
librosa library, but it takes too much time (around 4 seconds for one file). I suspect this is because
librosa doesn't support
mp3 and hence uses the slower
audioread to sample
import time import librosa s = time.time() for i in mp3_list[:10]: # list of mp3 file paths, doing for 10 files y, sr = librosa.load(i) print('time taken =', time.time() - s)
time taken = 36.55561399459839
I also get this warning:
UserWarning: "PySoundFile failed. Trying audioread instead."
Obviously, this is too much time for any practical application. I want to know if there are better alternatives to this?
For comparison, it only took around
1.2 seconds total time to sample 10 same-sized
So the warning kind of hints it. The Librosa developers addressed a similar question in this GitHub question:<blockquote>
This warning will always occur when loading mp3 because libsndfile does not (yet/currently) support the mp3 format. Librosa tries to use libsndfile first, and if that fails, it will fall back on the audioread package, which is a bit slower and more brittle, but supports more formats.</blockquote>
This is confirmed in the Librosa-code:
try ... except RuntimeError ...
So what you can do in this case is either implement your own
load() that directly uses
audioread to avoid the time wasted in the first block of
librosa.load(), or you can use a different library such as pydub. Alternatively, you can use ffmpeg to convert your mp3 to wave before loading them.