Finding the length of an mp3 file

前端 未结 5 1349
孤城傲影
孤城傲影 2020-12-09 09:02

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 = infil         


        
相关标签:
5条回答
  • 2020-12-09 09:31

    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)
    
    0 讨论(0)
  • 2020-12-09 09:37

    You can also get this using eyed3, if that's your flavor by doing:

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

    Note however that this uses sampling to determine the length of the track. As a result, if it uses variable bit rate, the samples may not be representative of the whole, and the estimate may be off by a good degree (I've seen these estimates be off by more than 30% on court recordings).

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

    0 讨论(0)
  • 2020-12-09 09:44

    Newer versions of python-ffmpeg have a wrapper function for ffprobe. An example of getting the duration is like this:

    import ffmpeg
    print(ffmpeg.probe('in.mp4')['format']['duration']))
    

    Found at: https://github.com/kkroening/ffmpeg-python/issues/57#issuecomment-361039924

    0 讨论(0)
  • 2020-12-09 09:47

    You can use 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]
    
    0 讨论(0)
  • 2020-12-09 09:48

    Maybe do the playing also within Python, i.e. don't use os.startfile, use some Python library to play the file.

    I have recently written such a library/module, the musicplayer module (on PyPI). Here is a simple demo player which you can easily extend for your shuffle code.

    Just do easy_install musicplayer. Then, here is some example 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 讨论(0)
提交回复
热议问题