I have this code that shows the duration of videos in path.
import os
import cv2
import datetime
path = 'H:\path\course'
array = []
for directory, subdirectory, files in os.walk(path):
for file in files:
merge = os.path.join(directory,file)
if file.lower().endswith('.mov') or file.lower().endswith('.flv') or file.lower().endswith('.mp4'):
data = cv2.VideoCapture(merge)
frames = data.get(cv2.CAP_PROP_FRAME_COUNT)
fps = int(data.get(cv2.CAP_PROP_FPS))
seconds = int(frames / fps)
video_time = str(datetime.timedelta(seconds=seconds))
# print("duration in seconds:", seconds)
# print("video time:", video_time)
# print(merge,' == ', video_time)
array += str(video_time)
print('Total: ', sum(array))
I tried putting it on a list to do the sum of all the hours at once and it didn't work.
It came out like this:
Total: ['0', ':', '0', '1', ':', '0', '0', '0', ':', '2', '5', ':', '0', '1', '0', ':', '1', '2', ':', '3', '5', '0', ':', '1', '2', ':', '2', '3', '0', ':', '0', '1', ':', '2', '9', '0', ':', '1', '5', ':', '1', '5', '0', ':', '3', '4', ':', '1', '3', '0', ':', '2', '4', ':', '5', '4', '0', ':', '1', '6', ':', '2', '6', '0', ':', '1', '4', ':', '1', '4', '0', ':', '1', '6', ':', '3', '6', '0', ':', '0', '2', ':', '5', '4', '0', ':', '0', '2', ':', '4', '4', '0', ':', '2', '6']
How do I add the total duration of all outputs?
PSEdit:
The colleague's answer helped me to solve the problem. But another problem came up that might help other beginners like me in the future.
When adding the var video_time to array other than str, it returned the full value.
So I had to send the seconds to the array as int and then convert to hours.