Guest User

Untitled

a guest
May 3rd, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from moviepy.editor import VideoFileClip, concatenate_videoclips
  2. import glob, os, re
  3. import time , random
  4.  
  5. def list_files(dir):
  6.     r = []
  7.     for root, dirs, files in os.walk(dir):
  8.         for name in files:
  9.             r.append(os.path.join(root, name))
  10.     return r
  11.  
  12. def make_clips(vid,clip_dur,i,x):
  13.     dur = vid.duration
  14.     if dur < 900:
  15.         const = 0
  16.     else:
  17.         const = 60
  18.     if i == 0:
  19.         t1 = random.randint(0, int((dur - 60) / x))
  20.         t1 = t1 + const
  21.         t2 = t1 + clip_dur
  22.     elif i + 1 == x:
  23.         t1 = dur - random.randint(20,60)
  24.         t2 = t1 + clip_dur
  25.     else:
  26.         t1 = random.randint((int(i * (dur - 60) / x)) , (int((i + 1) * (dur - 60) / x)))
  27.         t1 = t1 + const
  28.         t2 = t1 + clip_dur
  29.     print(t1, t2)
  30.     clip = (vid.subclip(t1, t2))
  31.     return clip
  32.  
  33. def main(total_dur = 15.0):
  34.     folder = 'D:\\EGDownloads\\'
  35.     files = list_files(folder)
  36.     for file in files:
  37.         for iteration in range(1,3):
  38.             cur_dur = 0
  39.             if ".ini" in file or "GIFS" in file:
  40.                 continue
  41.             gif_files = []
  42.             file_name = file.split("\\")
  43.             file_name = file_name[-1]
  44.             new_file_name = file_name + "-" + str(int(total_dur)) + "seconds(" + str(iteration) + ").mp4"
  45.             gif_folder = "D:\\EGDownloads\\GIFS\\"
  46.             gif_files = list_files(gif_folder)
  47.             if any(new_file_name == gif_file.split('\\')[-1] for gif_file in gif_files):
  48.                 continue
  49.             vid = VideoFileClip(file)
  50.             vid_dur = float(vid.duration)
  51.             min = int(vid_dur / 60)
  52.             secs = int(vid_dur % 60)
  53.             print(str(min) + " Minutes " + str(secs) + " Seconds")
  54.             if vid_dur<600:
  55.                 continue
  56.             print(file_name)
  57.             list = []
  58.             clip_parts = []
  59.             parts = 1
  60.             while cur_dur<12.5:
  61.                 time = random.randint(5,9)
  62.                 time = time/10
  63.                 cur_dur+=time
  64.                 clip_parts.append(time)
  65.                 parts+=1
  66.             clip_parts.append(total_dur-cur_dur)
  67.             for i, clip_dur in enumerate(clip_parts):
  68.                 short_clip = make_clips(vid,clip_dur,i,parts)
  69.                 list.append(short_clip)
  70.             final_clip = concatenate_videoclips(list)
  71.             final_clip.write_videofile("D:\\GIFS\\" + new_file_name)
  72.  
  73.  
  74. if __name__ == '__main__':
  75.     main()
Add Comment
Please, Sign In to add comment