python存储mp3信息,使用python从mp3音频文件中获取振幅数据

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")

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

解决方案

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.

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值