Few days ago, I was working on a project where I have to lookup the duration of video files and compare the length duration on local disk with the corresponding duration of video online.
So, instead of going between video on local disk and online repeated, I thought what if I could use python to extract all the duration lengths into a table (CSV/Excel) and compare the columns.
Here is how I went about doing it. I made use of 'moviepy' which is a Python module for video editing, which can be used for basic operations (like cuts, concatenations, title insertions).
Installing moviepy
Use: pip install moviepy
This will some other dependent packages such as: decorator, tqdm, urllib3, idna, chardet, requests, proglog, numpy, pillow, imageio, imageio-ffmpeg
To be sure you installation was successful, try to import the module as seen above. If you got not error then your installation was successful and you are good to move on.
The poblem
Here I have some set of videos I downloaded from an online course and I listed their durations in a spreadsheet file as seen below. Now I want to compare each video duration/length to be sure it was downloaded correctly.
The code to extract the video duration to be compared withwhat was obtained online is as below:-
import glob import datetime from moviepy.editor import VideoFileClip folder_path = r'C:\Users\Yusuf_08039508010\Desktop\videos_tut' videoFiles = glob.glob(folder_path + str('\\*ts')) # Converts Seconds to: hours, mins, seconds using 'datetime' module def convert_sec(vid_seconds): return str(datetime.timedelta(seconds=vid_seconds)) for v in videoFiles: clip = VideoFileClip(v) # duration in seconds (create custom function to convert to: H:M:S) video_duration = clip.duration print (v.split('\\')[-1], convert_sec(int(video_duration)))
Basically, the code uses the 'glob' module to access all the videos in the folder and used the 'datetime' module to convert the video duration in seconds provided by the 'moviepy' module.
The result is a seen below;-
Thank you for reading.
No comments:
Post a Comment