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

audiomp3pydubpythonwav

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.

Best Answer

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(…)

Related Topic