Advertisement
shakhawatt

total_lengths_of_videos_in_a_folder

Jul 24th, 2021
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1.  
  2. """"""" Get total time of list of videos """""""
  3. import cv2
  4. import os
  5. def min_to_min_seconds(total_mins):
  6.  
  7.     total_mins, base_1OO_seconds = divmod(total_mins, 1)
  8.     seconds = int(base_1OO_seconds *60)
  9.  
  10.     return (total_mins, seconds)
  11.  
  12. def get_total_length_of_video(video_path,verbose = False):
  13.     video = cv2.VideoCapture(video_path)
  14.     frame_rate = video.get(cv2.CAP_PROP_FPS)
  15.     total_frames  = video.get(cv2.CAP_PROP_FRAME_COUNT)
  16.     total_seconds = total_frames/frame_rate
  17.     total_mins = total_seconds/60
  18.    
  19.     if verbose:
  20.         total_minutes,total_seconds = min_to_min_seconds(total_mins)
  21.         print(video_path,':','time:',int(total_minutes),':',total_seconds)
  22.  
  23.     return total_mins
  24.  
  25. def total_playlist_time(video_list):
  26.     total_time=0.0
  27.     for video_path in video_list:
  28.         total_time += get_total_length_of_video(video_path)
  29.  
  30.     total_min,total_seconds=min_to_min_seconds(total_time)
  31.     print('total time:',int(total_min),':',total_seconds)
  32.  
  33. def create_video_list(video_dir):
  34.     videos=os.listdir(video_dir)
  35.     videos=[os.path.join(video_dir,video) for
  36.             video in videos if video.endswith(('.mp4'))]
  37.     return videos
  38.  
  39.  
  40. if __name__ == "__main__":
  41.     VIDEO_PATH='.'
  42.     videos=create_video_list(VIDEO_PATH)
  43.     total_playlist_time(videos)
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement