0

Hi guys i would like to ask how can i get the length of the mp3/wav file using python codes? Right now i have 1 button connected to GPIO, after i press the button the mp3 file will play. Now i want to get also the length of mp3 file, if i press the button the mp3 file will play and also display the length of the file that play... Here`s my code:

import os
import RPi.GPIO as GPIO

GPIO.setmod(GPIO.BCM)
PGIO.setup(23,GPIO.IN)

while True:

if (GPIO.input(23) == True):
     os.system('mpg321 /media/usb/dropped.mp3 -q &')
     print(" THE LENGTH OF THE MP3 FILE THAT PLAYED ")

sleep(1.0)
2

Based on the accepted answer to this Stack Exchange question and modified for Python 3

Install mutagen:

sudo pip3 install mutagen

Then add this to your code:

from mutagen.mp3 import MP3
audio = MP3("/media/usb/dropped.mp3")
print(audio.info.length)

This will return the length in seconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.