查找 mp3 文件的长度

发布于 2024-11-07 13:01:59 字数 455 浏览 7 评论 0原文

所以我有代码:

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)

但它所做的只是不停地浏览所有歌曲。我想如果我能找到当前正在播放的歌曲的长度,我可以使用“时间”模块在歌曲播放完(睡眠)属性后继续播放。但是,我找不到如何在 Windows 上获取歌曲的长度。有谁知道我的问题的解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘东忘西忘不掉你 2024-11-14 13:01:59

您可以使用 mutagen 来获取歌曲的长度(请参阅教程):

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

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)
两相知 2024-11-14 13:01:59

您可以使用 FFMPEG 库:

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

并且输出将是:

[FORMAT]
duration=228.200515
[/FORMAT]

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]
平定天下 2024-11-14 13:01:59

较新版本的 ffmpeg-python 具有 ffprobe 的包装函数。获取持续时间的示例如下:

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

位于: https:/ /github.com/kkroening/ffmpeg-python/issues/57#issuecomment-361039924

Newer versions of ffmpeg-python 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

弥枳 2024-11-14 13:01:59

如果您喜欢的话,您也可以使用 eyes3 来获得此值:

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

但请注意,这使用采样来确定轨道的长度。因此,如果使用可变比特率,样本可能无法代表整体,并且估计值可能会有很大偏差(我在法庭录音中看到这些估计值偏差超过 30%)。

我不确定这比其他选项差多少,但如果您有可变比特率,请记住这一点。

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.

笙痞 2024-11-14 13:01:59

也许也在Python中进行播放,即不使用os.startfile,使用一些Python库来播放文件。

我最近编写了这样一个库/模块,即 musicplayer 模块(在 PyPI< /a>)。 这里是一个简单的演示您可以轻松扩展您的随机播放代码的播放器。

只需执行easy_install musicplayer即可。然后,这里是一些获取长度的示例代码:

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]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文