Search code examples
pythonaudioraspberry-pisample-rate

How to get sample rate of mp3 file using python


I need to play this mp3 file using pygame but I dont know what the sample rate of the file is. I need some way to programaticaly get the sample rate of the audio file so that I can play it at the correct rate cuz if I dont then it just distorts the sound. Thanks for any help


Solution

  • Using pydub:

    >>> from pydub import AudioSegment
    >>> song = AudioSegment.from_mp3("file.mp3")
    >>> song.frame_rate
    44100
    

    Or use pydub.utils.mediainfo():

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