How to get sample rate of mp3 file using python
Asked Answered
I

2

5

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

Insurgent answered 4/6, 2017 at 5:1 Comment(0)
A
11

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
Adjacency answered 4/6, 2017 at 5:7 Comment(5)
sorry I should have said this in the original post but I have tried that already but I cant seem to install the ffmpeg dependancy for that :/Insurgent
try to install the wheel versionAdjacency
You can also install the deps. readmeAdjacency
Installing the wheel version worked for me thank you so much!Insurgent
This isn't a very efficient method. AudioSegment parses the entire MP3 file, which isn't necessary if all you want to get is meta data, like frame rate.Aristocratic
C
2

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)
Ceramic answered 23/7, 2019 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.