pythonmp3duration

Finding the length of an mp3 file


So i have the code:

import glob,os
import random


path = 'C:\\Music\\'
aw=[]
for infile in glob.glob( os.path.join(path,'*.mp3') ):
    libr = infile.split('Downloaded',1)



    aw.append(infile)
aww = -1
while 1:
    aww += 1
    print len(aw),aww

    random.shuffle(aw)
    awww = aw[aww]
    os.startfile(awww)

but all it does is go through all of the songs without stopping. I thought if I could find the length of the song that is currently playing, I could use the "time" module to keep going after the song is done with the (sleep) attribute. However, I couldn't find how to get the length of the song on windows. Does anyone know a solution to my probleme?


Solution

  • You can use mutagen to get the length of the song (see the tutorial):

    from mutagen.mp3 import MP3
    audio = MP3("example.mp3")
    print(audio.info.length)