Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...