• Software Degeneracy
  • Posts
  • Automating the editing of TikTok brainrot clips in Python using the Moviepy library

Automating the editing of TikTok brainrot clips in Python using the Moviepy library

veryone knows exactly what I mean when I say brainrot clips. Game footage in the background. Original video clip overlayed over. Designed for stimulating dopamine drones to keep em watching.

Automating the editing of TikTok brainrot clips in Python using the Moviepy library

Everyone knows exactly what I mean when I say brainrot clips.

Game footage in the background.

Original video clip overlayed over.

Designed for stimulating dopamine drones to keep em watching.

What’s wild is people are making a shit ton of money from these types of clips when they’re over a minute long.

If you’re here from Instagram, I’m gonna break down the entire script I showed in the video so you can get a better understanding of how it works so you can take my code and tweak it how you want.

If you want to skip the explanation and just get the repo here’s the code:

import os from moviepy.editor import VideoFileClip, CompositeVideoClip, concatenate_videoclips , clips_array, vfx, AudioFileClip, ImageClip from moviepy.video.fx import resize timesRan = 0 clipIndex = 0 backgroundClip = input("What Video File Clip for Background? ") directoryName = input("What directory name? ") parent_dir = "C:\\Open Source\\Tiktok Clip Editing" clipFolder = "C:\\Open Source\\Tiktok Clip Editing\\clips" folderPath = os.path.join(parent_dir,directoryName) os.makedirs(folderPath, exist_ok= True) clipList = os.listdir(clipFolder) clipCount = len(clipList) def videoGen(list, minecraftClip, timesRan, folderPath, directoryName, clipFolder, clipIndex, listLength): while timesRan < listLength: clipFilePath = os.path.join(clipFolder, list[clipIndex]) overlayClip = VideoFileClip(clipFilePath) bgClip = VideoFileClip(minecraftClip) overlayResize = overlayClip.resize(width=800, height=519) overlayFinal = overlayResize.set_position("top") clipDuration = (int(overlayClip.duration)) overlayFinal = overlayFinal.margin(top=100, opacity=0) clips = CompositeVideoClip([bgClip, overlayFinal]) \ .set_duration(clipDuration) \ outputFilePath = os.path.join(folderPath, f'{directoryName}{timesRan}.mp4') clips.write_videofile(outputFilePath, audio_codec='aac') timesRan = timesRan + 1 clipIndex = clipIndex + 1 print("Complete!") videoGen(clipList, backgroundClip, timesRan, folderPath, directoryName, clipFolder, clipIndex, clipCount)

The libraries you’ll want to use are MoviePy and OS. MoviePy is a video editing library and OS is just for settings file paths/creating folders.

timesRan = 0 clipIndex = 0 backgroundClip = input("What Video File Clip for Background? ") directoryName = input("What directory name? ") parent_dir = "C:\\Open Source\\Tiktok Clip Editing" clipFolder = "C:\\Open Source\\Tiktok Clip Editing\\clips" folderPath = os.path.join(parent_dir,directoryName) os.makedirs(folderPath, exist_ok= True) clipList = os.listdir(clipFolder) clipCount = len(clipList)

These are the variables we pass to our videoGen function.

timesRan is set to 0 initially because the code hasn’t been run yet.

clipIndex is the index of the clip list we will start with (0 because first item).

background clip is an input. it’s the video file you’ll want to use for for the background, you’ll just enter its name here.

directoryName is the input for what you want the directory for holding the generated clips to be.

parent_dir is your parent directory, replace mine with yours.

clipFolder is the folder holding all of your clips that you want edited, replace mine with the one you are using.

folderPath is the new path for the folder you just created (os.makedirs creates this).

clipList is every item in your clip folder, and clipCount will give you the total # of items in that directory.

Now for the function itself:

while timesRan < listLength: clipFilePath = os.path.join(clipFolder, list[clipIndex]) overlayClip = VideoFileClip(clipFilePath) bgClip = VideoFileClip(minecraftClip) overlayResize = overlayClip.resize(width=800, height=519) overlayFinal = overlayResize.set_position("top") clipDuration = (int(overlayClip.duration)) overlayFinal = overlayFinal.margin(top=100, opacity=0) clips = CompositeVideoClip([bgClip, overlayFinal]) \ .set_duration(clipDuration) \ outputFilePath = os.path.join(folderPath, f'{directoryName}{timesRan}.mp4') clips.write_videofile(outputFilePath, audio_codec='aac') timesRan = timesRan + 1 clipIndex = clipIndex + 1 print("Complete!")

The while loops means that as long as timesRan is less than listLength, it will keep running this code. At the end we add one to timesRan and clipIndex to track this.

clipFilePath = os.path.join(clipFolder, list[clipIndex]) overlayClip = VideoFileClip(clipFilePath) bgClip = VideoFileClip(minecraftClip)

clipFilePath is the path of the current clip being overlayed with the videoFileClip function from moviePy. bgClip is the same, except it’s the background clip passed.

overlayResize = overlayClip.resize(width=800, height=519) overlayFinal = overlayResize.set_position("top") clipDuration = (int(overlayClip.duration)) overlayFinal = overlayFinal.margin(top=100, opacity=0)

This code is where you can add customization to it.

the overlayResize is how big or small you want the overlay clip to be, but try to keep the aspect ratio the same.

the overlayResize.set_position is setting it to the top of the clip you don’t need this if you don’t want it.

clipDuration takes the length of hte overlayClip and sets that to how long the clip needs to be to make sure your background clip length doesn’t screw up the total clip length.

overlayFinal.margin is the margin for the clip. in this example, it’s 100px away fropm the top. if you don’t have 0 opacity the margin at the top will be black bars,.

clips = CompositeVideoClip([bgClip, overlayFinal]) \ .set_duration(clipDuration) \ outputFilePath = os.path.join(folderPath, f'{directoryName}{timesRan}.mp4') clips.write_videofile(outputFilePath, audio_codec='aac')

This is the last part of our code.

CompositeVideoClip edits the 2 clips together, and .set_duration sets the total duration of the clip to clipDuration which was initialized above.

outputFilePath is where we will be writing the video to, with out folderPath/directory/timesRan number to identify it.mp4

and clips.write_videofile will write that clip to the specified path.

use the ‘aac’ audio_codec because otherwise the audio will not play on a phone.

Hopefully you found this script helpful. If you want to learn more about how to monetize this check out Max Jones on Tiktok