使用python从mp3音频文件中获取振幅数据 [英] get the amplitude data from an mp3 audio files using python

查看:999
本文介绍了使用python从mp3音频文件中获取振幅数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mp3文件,我想基本上绘制出该音频样本中存在的振幅频谱. 我知道如果我们有一个wav文件,我们可以很容易地做到这一点.有很多python软件包可用于处理wav文件格式.但是,我不想将文件转换为wav格式,然后存储然后使用. 我要实现的目标是直接获取mp3文件的幅度,即使我必须将其转换为wav格式,脚本也应该在运行时进行广播,而无需实际将文件存储在数据库中. 我知道我们可以像下面那样转换文件:

I have an mp3 file and I want to basically plot the amplitude spectrum present in that audio sample. I know that we can do this very easily if we have a wav file. There are lot of python packages available for handling wav file format. However, I do not want to convert the file into wav format then store it and then use it. What I am trying to achieve is to get the amplitude of an mp3 file directly and even if I have to convert it into wav format, the script should do it on air during runtime without actually storing the file in the database. I know we can convert the file like follows:

from pydub import AudioSegment
sound = AudioSegment.from_mp3("test.mp3")
sound.export("temp.wav", format="wav")

它会创建它原本应该的temp.wav,但是我们可以只使用内容而不存储实际文件吗?

and it creates the temp.wav which it supposed to but can we just use the content without storing the actual file?

推荐答案

MP3是经过编码的wave(+标签和其他内容).您需要做的就是使用MP3解码器对其进行解码.解码器将为您提供需要进一步处理的完整音频数据.

MP3 is encoded wave (+ tags and other stuff). All you need to do is decode it using MP3 decoder. Decoder will give you whole audio data you need for further processing.

如何解码mp3?令我震惊的是,几乎没有可用的Python工具.尽管我在问题中找到了一个不错的人.它被称为 pydub ,我希望我可以使用作者提供的示例代码段(我将其的更多信息更新为Wiki):

How to decode mp3? I am shocked there are so few available tools for Python. Although I found a good one in this question. It's called pydub and I hope I can use a sample snippet from author (I updated it with more info from wiki):

from pydub import AudioSegment

sound = AudioSegment.from_mp3("test.mp3")

# get raw audio data as a bytestring
raw_data = sound.raw_data
# get the frame rate
sample_rate = sound.frame_rate
# get amount of bytes contained in one sample
sample_size = sound.sample_width
# get channels
channels = sound.channels

请注意,此时raw_data处于播放中";).现在由您决定如何使用收集的数据,但是此模块似乎可以为您提供所需的一切.

Note that raw_data is 'on air' at this point ;). Now it's up to you how do you want to use gathered data, but this module seems to give you everything you need.

这篇关于使用python从mp3音频文件中获取振幅数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆