How to get sample rate of mp3 file using python

Viewed 6598

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

2 Answers

The pydub answer is pretty good, until you use it with pyinstaller. Here is how to do what you are looking for with mutagen.

# Specifically MP3 file
from mutagen.mp3 import MP3
audio_info = MP3('FILENAME.mp3').info

# Generic audio file
import mutagen
audio_info = mutagen.File('FILENAME.ext').info

print(audio_info.sample_rate)
Related