import cv2 import os def video_duration(dir_name): sum_duration = 0 for root, dirs, files in os.walk(dir_name, topdown=False): for filename in files: cap = cv2.VideoCapture(dir_name + "\\" + filename) if cap.isOpened(): rate = cap.get(5) frame_num = cap.get(7) duration = frame_num / rate sum_duration += duration return sum_duration
if __name__ == '__main__': file = r"D:\Movie1\神话" total_video_time = video_duration(file) print("{file} 目录下全部视频总时长为:秒"+total_video_time)
# coding=utf8
__author__ = 'Administrator'
import cv2
import os
import time
def video_duration(dir_name):
sum_duration = 0
for root, dirs, files in os.walk(dir_name, topdown=False):
for filename in files:
cap = cv2.VideoCapture(dir_name + "\\" + filename)
if cap.isOpened():
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num / rate
sum_duration += duration
return sum_duration
def test(path):
mydict={}
items=[]
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
full_path=os.path.join(dirpath, filename)
cap = cv2.VideoCapture(full_path)
if cap.isOpened():
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num / rate
item = int(duration)
if item not in items:
items.append(item)
mydict[full_path]=item
return mydict,items
if __name__ == '__main__':
start =time.time()
file = r"D:\Movie1\神话"
# total_video_time = video_duration(file)
# print("{file} 目录下全部视频总时长为:秒"+str(total_video_time))
end = time.time()
cost =end-start
print("cost:",cost)
mydict, items= test(file)
print(items)
totaldict={}
for key,v in mydict.items():
print("key: "+str(key)+"\tv: "+str(v))
for j in items:
if v==j:
if j not in totaldict:
totaldict[j]=[key]
else:
totaldict[j]=totaldict[j]+[key]
for key,v in totaldict.items():
print("key: "+str(key)+"\tv: "+str(v))
#最后把相同长度的电影分成了一组
结果如下所示,一个长度对应几个电影的路径
key: 2765 v: ['D:\\Movie1\\神话\\神话-01.mp4']
key: 2492 v: ['D:\\Movie1\\神话\\神话-02.mp4', 'D:\\Movie1\\神话\\神话-04.mp4', 'D:\\Movie1\\神话\\神话-08.mp4', 'D:\\Movie1\\神话\\神话-11.mp4']
key: 2491 v: ['D:\\Movie1\\神话\\神话-03.mp4', 'D:\\Movie1\\神话\\神话-05.mp4', 'D:\\Movie1\\神话\\神话-10.mp4', 'D:\\Movie1\\神话\\神话-12.mp4']
key: 2493 v: ['D:\\Movie1\\神话\\神话-06.mp4']
key: 2490 v: ['D:\\Movie1\\神话\\神话-07.mp4', 'D:\\Movie1\\神话\\神话-09.mp4', 'D:\\Movie1\\神话\\神话-14.mp4', 'D:\\Movie1\\神话\\神话-17.mp4', 'D:\\Movie1\\神话\\神话-18.mp4', 'D:\\Movie1\\神话\\神话-20.mp4', 'D:\\Movie1\\神话\\神话-32.mp4', 'D:\\Movie1\\神话\\神话-33.mp4', 'D:\\Movie1\\神话\\神话-40.mp4']
key: 2488 v: ['D:\\Movie1\\神话\\神话-13.mp4', 'D:\\Movie1\\神话\\神话-25.mp4', 'D:\\Movie1\\神话\\神话-44.mp4']
key: 2489 v: ['D:\\Movie1\\神话\\神话-15.mp4', 'D:\\Movie1\\神话\\神话-16.mp4', 'D:\\Movie1\\神话\\神话-22.mp4', 'D:\\Movie1\\神话\\神话-24.mp4', 'D:\\Movie1\\神话\\神话-26.mp4', 'D:\\Movie1\\神话\\神话-28.mp4', 'D:\\Movie1\\神话\\神话-29.mp4', 'D:\\Movie1\\神话\\神话-30.mp4', 'D:\\Movie1\\神话\\神话-31.mp4']
key: 2487 v: ['D:\\Movie1\\神话\\神话-19.mp4', 'D:\\Movie1\\神话\\神话-23.mp4', 'D:\\Movie1\\神话\\神话-36.mp4', 'D:\\Movie1\\神话\\神话-48.mp4', 'D:\\Movie1\\神话\\神话-49.mp4']
key: 1232 v: ['D:\\Movie1\\神话\\神话-21.mp4']
key: 2486 v: ['D:\\Movie1\\神话\\神话-27.mp4', 'D:\\Movie1\\神话\\神话-37.mp4', 'D:\\Movie1\\神话\\神话-38.mp4', 'D:\\Movie1\\神话\\神话-42.mp4', 'D:\\Movie1\\神话\\神话-43.mp4', 'D:\\Movie1\\神话\\神话-45.mp4', 'D:\\Movie1\\神话\\神话-46.mp4']
key: 2485 v: ['D:\\Movie1\\神话\\神话-34.mp4', 'D:\\Movie1\\神话\\神话-35.mp4', 'D:\\Movie1\\神话\\神话-41.mp4']
key: 2484 v: ['D:\\Movie1\\神话\\神话-39.mp4', 'D:\\Movie1\\神话\\神话-47.mp4']
key: 2673 v: ['D:\\Movie1\\神话\\神话-50end.mp4']