Find the Length of a Song with Pygame

Viewed 11379

I'm building an Radio Automation Program, but I can't figure out how to have a timer countdown the number of seconds left in the song. I'm currently using Pygame and don't really want to load another toolkit just for this. So far I can make a timer count up using this:

import pygame

#setup music
track = "Music/Track02.wav"
pygame.mixer.music.load(track)
pygame.mixer.music.play()
print("Playing Music")
while(pygame.mixer.music.get_busy()):
    print "\r"+str(pygame.mixer.music.get_pos()),

But I have no idea how to get the total length of the song and countdown without having played the song already.

3 Answers

The mutagen.mp3 can be used to find the length of music.

Firstly: pip install mutagen

-> İmport:

from pygame import *
from mutagen.mp3 import MP3

-> Use:

mixer.music.load('example.mp3')
song = MP3('example.mp3')
songLength = song.info.length
Related