Skip to content

Commit 195c939

Browse files
Added a script to find video duration inside a directory
1 parent 53d2903 commit 195c939

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import moviepy.editor # pip install moviepy, need it for movie duration
2+
import mimetypes # to guess file type, if it's a video file or not
3+
import os # path, file and directory
4+
import sys # to get the argument
5+
6+
# Converts duration into human readable format
7+
def get_duration(seconds):
8+
hours = seconds // 3600
9+
seconds %= 3600
10+
minutes = seconds // 60
11+
seconds %= 60
12+
13+
hours = "0"+str(hours) if(hours<10) else str(hours)
14+
minutes = "0"+str(minutes) if(minutes<10) else str(minutes)
15+
seconds = "0"+str(seconds) if(seconds<10) else str(seconds)
16+
return hours, minutes, seconds
17+
18+
# Display video(s) duration for given directory
19+
def display_video_duration(videos_list):
20+
for video_file in videos_list:
21+
video_file_path = dir_path+"/"+video_file
22+
23+
# check if video_file is of video type
24+
try:
25+
if mimetypes.guess_type(video_file_path)[0].startswith('video'):
26+
# Create an object by passing the location of a video file
27+
video = moviepy.editor.VideoFileClip(video_file_path)
28+
29+
# Contains the duration of the video in terms of seconds
30+
video_duration = int(video.duration) # video length output is in seconds
31+
hours, minutes, seconds = get_duration(video_duration) #convert into human readable duration format
32+
33+
print(f"{video_file}: {hours}:{minutes}:{seconds}")
34+
except AttributeError:
35+
pass
36+
37+
if (len(sys.argv) != 2):
38+
exit("It needs video file list to work")
39+
40+
dir_path = ""
41+
if(os.path.isdir(sys.argv[1])):
42+
dir_path = sys.argv[1] # first argument as video list directory
43+
44+
# list to store vodeo files
45+
videos = []
46+
47+
# Iterate directory
48+
for path in os.listdir(dir_path):
49+
# check if current path is a file
50+
if os.path.isfile(os.path.join(dir_path, path)):
51+
videos.append(path)
52+
53+
display_video_duration(videos)
54+
55+
56+
# If you want to check the given file is a video file
57+
#if mimetypes.guess_type(path)[0].startswith('video'):
58+
# print('It is a video')

0 commit comments

Comments
 (0)