librosa.load()花费太长时间来加载(采样)mp3文件 [英] librosa.load() takes too long to load(sample) mp3 files
问题描述
我正在尝试使用librosa
库通过以下Python代码对mp3文件进行采样(将模拟转换为数字),但是这会花费太多时间(一个文件大约需要4秒).我怀疑这是因为librosa
不支持mp3
,因此使用较慢的audioread
来采样mp3
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 mp3
代码:
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
我也收到此警告:
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?
为进行比较,对10个相同大小的wav
转化进行采样仅花费了大约1.2
秒的时间
For comparison, it only took around 1.2
seconds total time to sample 10 same-sized wav
conversions
推荐答案
因此,警告类型会提示它. Librosa开发人员在此GitHub问题中解决了类似问题:
So the warning kind of hints it. The Librosa developers addressed a similar question in this GitHub question:
由于libsndfile加载mp3时,始终会出现此警告 目前尚不支持mp3格式.天秤座试图使用 首先是libsndfile,如果失败,它将退回到 audioread程序包,它速度较慢且较脆,但是 支持更多格式.
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.
这已在 Librosa-code :try ... except RuntimeError ...
因此,在这种情况下,您可以执行自己的直接使用audioread
的load()
来避免在librosa.load()
的第一块中浪费时间,或者可以使用其他库,例如 pydub .或者,您可以使用 ffmpeg 来
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.
这篇关于librosa.load()花费太长时间来加载(采样)mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!