import cv2,os
def count_video_duration(file_path):
cap = cv2.VideoCapture(file_path)
if cap.isOpened():
rate = cap.get(5)
FrameNumber = cap.get(7)
duration = FrameNumber / rate
return int(duration)
else:
return 0
def count_video_duration_recursion(file_path):
global duration_video
if os.path.isdir(file_path):
for file in os.listdir(file_path):
count_video_duration_recursion(os.path.join(file_path, file))
else:
if file_path.endswith('.mp4') or file_path.endswith('.avi')or file_path.endswith('.3gp'):
duration = count_video_duration(file_path)
duration_video+=duration
print(file_path, duration)
duration_video = 0
path = "路径"
count_video_duration_recursion(path)
print(duration_video)