简体   繁体   English

查找mp3文件的长度

[英]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.但是,我找不到如何在 Windows 上获取歌曲的长度。 Does anyone know a solution to my probleme?有谁知道我的问题的解决方案?

You can use mutagen to get the length of the song (see the tutorial ):您可以使用mutagen来获取歌曲的长度(请参阅教程):

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

You can use FFMPEG libraries:您可以使用 FFMPEG 库:

    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]

You can also get this using eyed3, if that's your flavor by doing:您也可以使用 eyed3 来获得它,如果这是您的口味,请执行以下操作:

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).因此,如果它使用可变比特率,样本可能不能代表整体,并且估计值可能有很大的偏差(我在法庭录音中看到这些估计值偏差超过 30%)。

I'm not sure that's much worse than other options, but it's something to remember if you have variable bit rates.我不确定这比其他选项差多少,但如果您有可变比特率,请记住这一点。

Newer versions of python-ffmpeg have a wrapper function for ffprobe .较新版本的python-ffmpeg具有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发现于: https : //github.com/kkroening/ffmpeg-python/issues/57#issuecomment-361039924

Maybe do the playing also within Python, ie don't use os.startfile , use some Python library to play the file.也许也可以在 Python 中进行播放,即不要使用os.startfile ,使用一些 Python 库来播放文件。

I have recently written such a library/module, the musicplayer module ( on PyPI ).我最近写了这样一个库/模块时, musicplayer模块( PyPI上)。 Here is a simple demo player which you can easily extend for your shuffle code. 是一个简单的演示播放器,您可以轻松扩展您的随机播放代码。

Just do easy_install musicplayer .只需执行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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM