我建议使用FFprobe(附带FFmpeg)。
查马斯给出的答案非常接近,但最终对我来说失败了。
如前所述,我使用的是Python3.5和3.6,这对我很有用。import subprocess
def get_duration(file):
"""Get the duration of a video using ffprobe."""
cmd = 'ffprobe -i {} -show_entries format=duration -v quiet -of csv="p=0"'.format(file)
output = subprocess.check_output(
cmd,
shell=True, # Let this run in the shell
stderr=subprocess.STDOUT
)
# return round(float(output)) # ugly, but rounds your seconds up or down
return float(output)
如果你想把这个函数放到一个类中,并在Django(1.8-1.11)中使用它,只需改变一行,然后把这个函数放到你的类中,如下所示:def get_duration(file):
致:def get_duration(self, file):
注意:使用相对路径在本地对我有效,但生产服务器需要绝对路径。您可以使用os.path.abspath(os.path.dirname(file))获取视频或音频文件的路径。
最后
以上就是无聊小笼包最近收集整理的关于python获取网页播放视频时长_用ffmpeg在python中获取视频时长的全部内容,更多相关python获取网页播放视频时长_用ffmpeg在python中获取视频时长内容请搜索靠谱客的其他文章。
发表评论 取消回复