How to Check the Sample Rate of an MP3 File in Python?

Estimated read time 1 min read

To check the sample rate of an MP3 file in Python, you can use the pydub library, which is a powerful audio processing library. Here’s an example of how you can retrieve the sample rate of an MP3 file using pydub:

from pydub import AudioSegment

# Load the MP3 file
audio = AudioSegment.from_file("your_file.mp3", format="mp3")

# Get the sample rate
sample_rate = audio.frame_rate

# Print the sample rate

In the code above, we first import the AudioSegment class from pydub. Then, we use the from_file() method to load the MP3 file, specifying the file path and format. Next, we access the frame_rate attribute of the audio object, which represents the sample rate. Finally, we print the sample rate using the print() function.

Replace "your_file.mp3" with the path to your actual MP3 file. The sample rate will be printed, such as 44100 for a standard sample rate of 44.1 kHz.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply