检测视频时长
可以检测一个文件夹内所有视频的时长,进行课时统计
'''
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")