How To Edit Videos With Python

In this tutorial, I show you how I edit my videos using Python and MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations like cuts, concatenations, title insertions, video compositing, video processing, and even to create advanced effects.

The code is available on GitHub.


It's probably not suitable for every workflow, but Moviepy is indeed capable of producing high quality videos. For example all of my videos for the Advanced Python Playlist, the Machine Learning From Scratch Playlist, and the PyTorch Beginner Course are edited with a Python script.

Example Script

import moviepy.editor as mpy vcodec = "libx264" videoquality = "24" # slow, ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow compression = "slow" title = "test" loadtitle = title + '.mov' savetitle = title + '.mp4' # modify these start and end times for your subclips cuts = [('00:00:02.949', '00:00:04.152'), ('00:00:06.328', '00:00:13.077')] def edit_video(loadtitle, savetitle, cuts): # load file video = mpy.VideoFileClip(loadtitle) # cut file clips = [] for cut in cuts: clip = video.subclip(cut[0], cut[1]) clips.append(clip) final_clip = mpy.concatenate_videoclips(clips) # add text txt = mpy.TextClip('Please Subscribe!', font='Courier', fontsize=120, color='white', bg_color='gray35') txt = txt.set_position(('center', 0.6), relative=True) txt = txt.set_start((0, 3)) # (min, s) txt = txt.set_duration(4) txt = txt.crossfadein(0.5) txt = txt.crossfadeout(0.5) final_clip = mpy.CompositeVideoClip([final_clip, txt]) # save file final_clip.write_videofile(savetitle, threads=4, fps=24, codec=vcodec, preset=compression, ffmpeg_params=["-crf",videoquality]) video.close() if __name__ == '__main__': edit_video(loadtitle, savetitle, cuts)

FREE VS Code / PyCharm Extensions I Use

✅ Write cleaner code with Sourcery, instant refactoring suggestions: Link *

* This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you! 🙏

Check out my Courses