2021-09-18

检测视频时长

可以检测一个文件夹内所有视频的时长,进行课时统计

'''
Greated by Carino Sept 20, 2021

Updated on Sept 27,2021

'''

import os
import re
import logging
import cv2

#path = 'D:\漆鹏程\02 课程开发\01 人工智能\深度学习\刘澈\08 AIOC-DL_4-3_深度学习基础与技巧\AIOC-DL_深度学习基础与技巧_4-3'
path = os.curdir  #获取当前文件路径
path = os.path.realpath(path)
os.walk(path)

def getVideoNames(path=os.path.realpath(__file__)):
    name = []
    count = 0
    for root_file, dirs, files in os.walk(path):
        for file in files:
            if re.match(".+\.mp4",file) or re.match(".+\.avi",file)or re.match(".+\.MOV",file):            #匹配mp4文件
                print(file)
                name.append(os.path.join(root_file, file))
                count+=1
            else:
                logging.warning(file+"不是视频文件")
    print(  '%s'%path+"下共" + str(count)+"个mp4、avi文件")
    return name

def getTotalTime(files):
    total=0
    for file in files:
        cap=cv2.VideoCapture(file)
        if cap.isOpened():
            rate = cap.get(5)       #获取帧率
            fraNum=cap.get(7)       #获取帧数
            duration=fraNum/rate/60
            total+=duration
    return total


files=getVideoNames( path )
total=getTotalTime(files)
print("%s:%.2f min   共 %.1f课时"%(path,total,total/45))
os.system("pause")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值