查找 mp3 文件的长度
所以我有代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 mutagen 来获取歌曲的长度(请参阅教程):
You can use mutagen to get the length of the song (see the tutorial):
您可以使用 FFMPEG 库:
并且输出将是:
You can use FFMPEG libraries:
and the output will be:
较新版本的 ffmpeg-python 具有 ffprobe 的包装函数。获取持续时间的示例如下:
位于: https:/ /github.com/kkroening/ffmpeg-python/issues/57#issuecomment-361039924
Newer versions of
ffmpeg-python
have a wrapper function forffprobe
. An example of getting the duration is like this:Found at: https://github.com/kkroening/ffmpeg-python/issues/57#issuecomment-361039924
如果您喜欢的话,您也可以使用 eyes3 来获得此值:
但请注意,这使用采样来确定轨道的长度。因此,如果使用可变比特率,样本可能无法代表整体,并且估计值可能会有很大偏差(我在法庭录音中看到这些估计值偏差超过 30%)。
我不确定这比其他选项差多少,但如果您有可变比特率,请记住这一点。
You can also get this using eyed3, if that's your flavor by doing:
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.
也许也在Python中进行播放,即不使用os.startfile,使用一些Python库来播放文件。
我最近编写了这样一个库/模块,即
musicplayer
模块(在 PyPI< /a>)。 这里是一个简单的演示您可以轻松扩展您的随机播放代码的播放器。只需执行
easy_install musicplayer
即可。然后,这里是一些获取长度的示例代码: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: