Skip to content

Instantly share code, notes, and snippets.

@laygond
Created September 12, 2019 18:02
Show Gist options
  • Save laygond/c856b0e5f7d5160cc2cdbc3ca60b91b4 to your computer and use it in GitHub Desktop.
Save laygond/c856b0e5f7d5160cc2cdbc3ca60b91b4 to your computer and use it in GitHub Desktop.
Split a video into small sections (.5 seconds each) and then randomize them.
# https://github.com/antiboredom/automating-video/blob/master/moviepy-tutorial.md
import random
import moviepy.editor as mp
original_video = mp.VideoFileClip(videofile)
duration = original_video.duration
segment_length = .5
clips = []
# the first segment starts at 0 seconds
clip_start = 0
# make new segments as long as clip_start is
# less than the duration of the video
while clip_start < duration:
clip_end = clip_start + segment_length
# make sure the the end of the clip doesn't exceed the length of the original video
if clip_end > duration:
clip_end = duration
# create a new moviepy videoclip, and add it to our clips list
clip = original_video.subclip(clip_start, clip_end)
clips.append(clip)
clip_start = clip_end
# randomize the clips
random.shuffle(clips)
# stick em all together and save
final_video = mp.concatenate_videoclips(clips)
final_video.write_videofile('random.mp4')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment