我是靠谱客的博主 畅快饼干,这篇文章主要介绍python读取视频文件时长_Python实现统计一个文件夹里所有视频总时长,现在分享给大家,希望可以做个参考。

以下为转载内容:

一个文件夹里有很多子文件夹,每个子文件夹里有一些视频,如何知道这个根目录下所有视频一共有多少分钟呢?

我用python写了一个小程序完成这个工作,用os.walk遍历文件,再用moviepy获得视频时长,用datatime把秒数转换成易读格式,代码在GitHub,欢迎大家Star!

https://github.com/aihy/my-tricks/blob/master/compute_duration.py

演示一下

文件夹是这样的:

➜ 吴恩达deeplearning.ai ls

01.神经网络和深度学习

02.改善深层神经网络:超参数调试、正则化以及优化

03.结构化机器学习项目

04.卷积神经网络

05.序列模型

课件_课后作业_其他资料1

2

3

4

5

6

7

指令:

python compute_duration.py --path /Users/phx/Downloads/吴恩达deeplearning.ai --type .mkv1

输出:

1 day, 3:32:43.3300001

moviepy需要用pip装一下

import os

import datetime

import sys

import argparse

from moviepy.editor import VideoFileClip

if __name__ == "__main__":

parser = argparse.ArgumentParser(

description='Compute Total Time of a Series of Videos')

parser.add_argument("--path", metavar="PATH", default=".",

help="the root path of the videos(default: .)")

parser.add_argument("--type", metavar="TYPE", default=".mkv",

help="the type of the videos(default: .mkv)")

args = parser.parse_args()

filelist = []

for a, b, c in os.walk(args.path):

for name in c:

fname = os.path.join(a, name)

if fname.endswith(args.type):

filelist.append(fname)

ftime = 0.0

for item in filelist:

clip = VideoFileClip(item)

ftime += clip.duration

print("%d seconds: " % ftime,str(datetime.timedelta(seconds=ftime)))

分割线中间是原作者github中的代码

最后

以上就是畅快饼干最近收集整理的关于python读取视频文件时长_Python实现统计一个文件夹里所有视频总时长的全部内容,更多相关python读取视频文件时长_Python实现统计一个文件夹里所有视频总时长内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部