Search mp3 file length

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 he does is go through all the songs without stopping. I thought that if I could find the length of the song that is playing now, I could use the "time" module to continue working after the song is completed with the (sleep) attribute. However, I could not find how to get the length of the song in the windows. Does anyone know a solution to my problem?

+12
source share
4 answers

You can use mutagen to get the length of a song (see the Tutorial ):

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

You can use the FFMPEG libraries:

  args=("ffprobe","-show_entries", "format=duration","-i",filename) popen = subprocess.Popen(args, stdout = subprocess.PIPE) popen.wait() output = popen.stdout.read() 

and the output will be:

 [FORMAT] duration=228.200515 [/FORMAT] 
+6
source

You can also get this with eyed3 if that is your taste by doing:

 import eyed3 duration = eyed3.load('path_to_your_file.mp3').info.time_secs 

Note that this uses sampling to determine the length of the track. As a result, if it uses a variable data rate, the samples may not be representative for the whole, and the score can be deleted to a large extent (I saw that these scores exceed 30% in court hearings).

I'm not sure if this is much worse than the other options, but it is something to remember if you have variable bit rates.

+3
source

Perhaps the game is also in Python, that is, do not use os.startfile , use some Python library to play the file.

I recently wrote such a library / module, the musicplayer module ( in PyPI ). Here is a simple demo player that you can easily extend for your shuffle code.

Just do easy_install musicplayer . Then, here is a sample code to get the length:

 class Song: def __init__(self, fn): self.f = open(fn) def readPacket(self, bufSize): return self.f.read(bufSize) def seekRaw(self, offset, whence): self.f.seek(offset, whence) return self.f.tell() import musicplayer as mp songLenViaMetadata = mp.getMetadata(Song(filename)).get("duration", None) songLenViaAnalyzing = mp.calcReplayGain(Song(filename))[0] 
0
source

Source: https://habr.com/ru/post/888368/


All Articles