Ever wanted to watch all your favorite 300 scenes in a random order? Now you can with Python! First, get the moviepy package. Then we can proceed to learn by example:
Say we have a video file called ’10s count.avi’ that shows a counter incrementing from 0 to 9 every second.
We are interested in shuffling the video clip so that:
- The scene with number 5 is omitted
- 9 shows at the very end
- the rest is shuffled randomly
In Python create a list of two-element tuples where the first and second element indicates the start and end times (in seconds) of the scene:
startstop = [(0, 1), (1, 2), (2, 4), (6, 7), (8, 9), (9, 10)]
Create a new variable omitting the last tuple of the list startstop_s = startstop[:-1], import the random package then shuffle the new list random.shuffle(startstop_s), and finally append back the omitted tuple startstop_s.append(startstop[-1]). After these operations we get the following list of tuples in startstop_s defining our new movie file:
[(6, 7), (0, 1), (8, 9), (2, 4), (1, 2), (9, 10)]
Next we need to use moviepy to make our new clip using the subclip method, which extracts a portion of a video given the start and ending times, and concatenate_videoclips method, which does what it says. The following three lines of codes does the job:
clip = moviepy.editor.VideoFileClip(filepath) #Load video from filepath into Python
output=[clip.subclip(*ss) for ss in startstop_s] #Make new video clip object with shuffled order
moviepy.editor.concatenate_videoclips(output).write_videofile(filepath.split('.')[0] + '_shuffle.mp4',preset='ultrafast') #Save video clip to file
… and it’s done! Now we have a file called ’10s count_shuffle.mp4′ as shown above (yes, it was encoded from lossless to h264). The Python script can be accessed here.

Also my YouTube channel for AI related projects.
