Find the Length of a Song with Pygame

JRJurman picture JRJurman · Aug 4, 2011 · Viewed 8.2k times · Source

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.

Answer

user1509818 picture user1509818 · Jul 9, 2012

You could also load the song as a normal sound and then check the length while using mixer.music for playing it.

a = pygame.mixer.Sound("test.wav")
print("length",a.get_length())