python中使用Torchaudio和libosa的音频不同

发布于 2025-01-21 05:07:19 字数 1183 浏览 6 评论 0原文

我在Python中加载了MP3文件,并使用Torchaudiolibrosa

import torchaudio
import librosa

filename='example.mp3'
array_tor, sample_rate_tor = torchaudio.load(filename,format='mp3')
array_lib, sample_rate_lib = librosa.load(filename, sr=sample_rate_tor)
print( len(array_tor.numpy()[0]) , len(array_lib)) # get different value

两个数组的长度不同,为什么会使它们与众不同,以及如何使它们在同一位置?

如果我将example.mp3转换为wav文件

from pydub import AudioSegment
audSeg = AudioSegment.from_mp3('example.mp3')
audSeg.export('example.wav', format="wav")

,则使用torchaudiolibrosaoundfile

import torchaudio
import librosa
import soundfile as sf
filename='example.wav'
array_tor_w, sample_rate_tor_w = torchaudio.load(filename,format='wav')
array_lib_w, sample_rate_lib_w = librosa.load(filename, sr=sample_rate_tor_w)
array_sfl_w, sample_rate_sfl_w = sf.read(filename)
print( len(array_tor_w.numpy()[0]) , len(array_lib_w), len(array_sfl_w)) # get same value

三个数组长度和内容相同,并且也与mp3文件中的len(array_lib)相同。

似乎Torchaudio.load()在MP3文件中很特别。

I loaded mp3 file in python with torchaudio and librosa

import torchaudio
import librosa

filename='example.mp3'
array_tor, sample_rate_tor = torchaudio.load(filename,format='mp3')
array_lib, sample_rate_lib = librosa.load(filename, sr=sample_rate_tor)
print( len(array_tor.numpy()[0]) , len(array_lib)) # get different value

the length of two arrays are different, why makes them different, and how to make them in same?

if I convert example.mp3 to wav file with

from pydub import AudioSegment
audSeg = AudioSegment.from_mp3('example.mp3')
audSeg.export('example.wav', format="wav")

and load wav file with torchaudio , librosa, soundfile

import torchaudio
import librosa
import soundfile as sf
filename='example.wav'
array_tor_w, sample_rate_tor_w = torchaudio.load(filename,format='wav')
array_lib_w, sample_rate_lib_w = librosa.load(filename, sr=sample_rate_tor_w)
array_sfl_w, sample_rate_sfl_w = sf.read(filename)
print( len(array_tor_w.numpy()[0]) , len(array_lib_w), len(array_sfl_w)) # get same value

the three array length and content are same and also same as len(array_lib) in mp3 file.

it seems the torchaudio.load() is special in mp3 file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-01-28 05:07:19

这是由于底层解码器库Torchaudio用途。

UP UTIL V0.11,Torchaudio使用了Libmad,在解码MP3时不会删除额外的填充。

请参阅 https://github.com/pytorch/audio/audio/issues/1500 细节。

在v0.12中,Torchaudio将MP3解码器切换到FFMPEG,应解决填充问题。

This is due to the underlying decoder library torchaudio uses.

Up util v0.11, torchaudio used libmad, which does not remove the extra padding when decoding MP3.

See https://github.com/pytorch/audio/issues/1500 for the detail.

In v0.12, torchaudio switched MP3 decoder to FFmpeg, and the padding issue should be resolved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文