Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 357
P'sao Avatar asked May 17 '11 22:05

P'sao


People also ask

How do you calculate the length of an audio file?

To determine the file size of an audio file, we have to multiply the bit rate of the audio by its duration in seconds. As a result, we get file size values in terms of kilobits and megabits.

How long is MP3 frame?

The typical MP3 (an MPEG Layer III, version 1) has 1152 samples per frame and the sample rate is (commonly) 44100 hz. So (1152 / 44100) * 1000 = 26,122449 ms per frame.


1 Answers

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) 
like image 59
Gregg Avatar answered Sep 19 '22 20:09

Gregg