11

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.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
JRJurman
  • 1,615
  • 2
  • 19
  • 30
  • This is on linux with python2.7 if that helps... – JRJurman Aug 04 '11 at 05:01
  • `mixer.music.get_pos` seems to return the total play time for the mixer - i.e. it doesn't reset with each sound played. How do you use that to show the per track progress? – Peter Gibson Jan 05 '17 at 03:53

3 Answers3

9

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())
Shan
  • 21
  • 9
  • a = pygame.mixer.Sound('path/to/music.mp3') pygame.error: Unable to open file 'path/to/music.mp3' – zyc Jul 15 '18 at 19:46
  • 1
    I looked it up elsewhere. a.get_length() only works for .ogg and .wav – zyc Jul 15 '18 at 19:52
3

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
HyopeR
  • 387
  • 2
  • 11
1

Check the Documentation. According to the site, this function returns the length of a sound object in seconds. So, time remaining is simply (pygame.mixer.music.get_length() - pygame.mixer.music.get_pos())*-1.

That way, it displays as a negative number like most time remaining counters do in a music player. Note, I don't have pygame on this computer, so I can't test it. So, check it just to make sure.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
Jonathanb
  • 1,224
  • 1
  • 11
  • 16
  • 3
    .get_length is only for the pygame Sounds, not the pygame.mixer.music – JRJurman Aug 04 '11 at 05:34
  • 1
    Right, sorry I missed that. Checking through the documentation furhter, it looks like the reason that won't work is because it streams Music objects in rather than loading them entirely into memory. Much like some MP3 players won't display the song length until the song is fully loaded, only the song never fully loads because pygame doesn't buffer like that. Any reason you can't use a Sound object instead? – Jonathanb Aug 05 '11 at 15:37
  • Just figured I should be using the Music object instead of the sound object... Although, I guess I could load the songs for the sake of getting the length... – JRJurman Aug 06 '11 at 04:44
  • pygame.mixer.Sounds().get_length() – Shubham Kumar Gupta Dec 18 '21 at 21:19