Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from moviepy.editor import VideoFileClip, concatenate_videoclips
- import glob, os, re
- import time , random
- def list_files(dir):
- r = []
- for root, dirs, files in os.walk(dir):
- for name in files:
- r.append(os.path.join(root, name))
- return r
- def make_clips(vid,clip_dur,i,x):
- dur = vid.duration
- if dur < 900:
- const = 0
- else:
- const = 60
- if i == 0:
- t1 = random.randint(0, int((dur - 60) / x))
- t1 = t1 + const
- t2 = t1 + clip_dur
- elif i + 1 == x:
- t1 = dur - random.randint(20,60)
- t2 = t1 + clip_dur
- else:
- t1 = random.randint((int(i * (dur - 60) / x)) , (int((i + 1) * (dur - 60) / x)))
- t1 = t1 + const
- t2 = t1 + clip_dur
- print(t1, t2)
- clip = (vid.subclip(t1, t2))
- return clip
- def main(total_dur = 15.0):
- folder = 'D:\\EGDownloads\\'
- files = list_files(folder)
- for file in files:
- for iteration in range(1,3):
- cur_dur = 0
- if ".ini" in file or "GIFS" in file:
- continue
- gif_files = []
- file_name = file.split("\\")
- file_name = file_name[-1]
- new_file_name = file_name + "-" + str(int(total_dur)) + "seconds(" + str(iteration) + ").mp4"
- gif_folder = "D:\\EGDownloads\\GIFS\\"
- gif_files = list_files(gif_folder)
- if any(new_file_name == gif_file.split('\\')[-1] for gif_file in gif_files):
- continue
- vid = VideoFileClip(file)
- vid_dur = float(vid.duration)
- min = int(vid_dur / 60)
- secs = int(vid_dur % 60)
- print(str(min) + " Minutes " + str(secs) + " Seconds")
- if vid_dur<600:
- continue
- print(file_name)
- list = []
- clip_parts = []
- parts = 1
- while cur_dur<12.5:
- time = random.randint(5,9)
- time = time/10
- cur_dur+=time
- clip_parts.append(time)
- parts+=1
- clip_parts.append(total_dur-cur_dur)
- for i, clip_dur in enumerate(clip_parts):
- short_clip = make_clips(vid,clip_dur,i,parts)
- list.append(short_clip)
- final_clip = concatenate_videoclips(list)
- final_clip.write_videofile("D:\\GIFS\\" + new_file_name)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment