Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pydub accessing the sampling rate(Hz) and the audio signal from an mp3 file

Just found out this interesting python package pydub which converts any audio file to mp3, wav, etc.

As far as I have read its documentation, the process is as follows:

  1. read the mp3 audio file using from_mp3()
  2. creates a wav file using export().

Just curious if there is a way to access the sampling rate and the audio signal(of 1-dimensional array, supposing it is a mono) directly from the mp3 file without converting it to a wav file. I am working on thousands of audio files and it might be expensive to convert all of them to wav file.

like image 753
bninopaul Avatar asked Jul 14 '15 07:07

bninopaul


2 Answers

If you aren't interested in the actual audio content of the file, you may be able to use pydub.utils.mediainfo():

>>> from pydub.utils import mediainfo
>>> info = mediainfo("/path/to/file.mp3")
>>> print info['sample_rate']
44100
>>> print info['channels']
1

This uses avlib's avprobe utility, and returns all kinds of info. I suggest giving it a try :)

Should be much faster than opening each mp3 using AudioSegment.from_mp3(…)

like image 55
Jiaaro Avatar answered Oct 31 '22 00:10

Jiaaro


frame_rate means sample_rate, so you can get like below;

from pydub import AudioSegment

filename = "hoge.wav"
myaudio = AudioSegment.from_file(filename)
print(myaudio.frame_rate)
like image 25
LittleWat Avatar answered Oct 30 '22 23:10

LittleWat