Automated Video Editing with MoviePy in Python

NeuralNine · Beginner ·💻 AI-Assisted Coding ·4y ago

Key Takeaways

The video demonstrates how to automate video editing using Python and the MoviePy library, covering topics such as clip creation, concatenation, video effects, and audio addition.

Full Transcript

[Music] what is going on guys welcome back in today's video i'm going to show you how you can automate video editing in python using movie pie so let's get right into it all right so what we're going to do is we're going to edit videos we're going to cut videos in python we're going to add some effects and for this we're going to use the library movie pi which is an external library which means that we need to install it using the command line so we open up cmd on windows or the terminal on linux and mac and we type pip install movie pi like that once you've done that you will be able to import it so we're going to say from movie pi and we're going to use a sub module editor so we're going to say from movie pi dot editor because we can do different things with uh with movie pie so we're going to pick the editor to edit videos and from the editor we're going to import a bunch of classes and functions and so on so if you just want to play around you can use a wildcard import but never use it in code if you have to import many many things in code you might want to use an alias something like import moviepie.editor s i don't know me or something i think there might even be a convention for that uh but don't just import everything you can do that if you just want to play around and if you want to to experiment uh for today's video we're just gonna import everything manually and the first thing you want to import here is the video file clip and before we continue with the code let me show you what the basic idea is of what we're going to do today so i have here a directory with an intro mp4 which is a neural nine intro and we have two videos which are copyright free um so i got them from pixabay to to be able to show them in that video here and when we open one of these videos here we're going to see they're quite simple so just some smoke um 59 seconds you can see some smoke animation here and the second one is also something similar as you can see here um and then i have the neural nine intro which i'm mainly going to use for the music so we're not going to use the the intro itself really as a video and the basic idea is just to show you here how you can add it how you can combine how you can cut videos with movie pie and python uh it is quite interesting especially if you want to i don't know do some compilation style video or you want to do some random compilation or you want to just automate your video cutting process if it's always the same you just need to enter the seconds or something like that uh there are many use cases for that so i'm going to give you the very simple example of just picking these videos that i showed you taking parts out of them combining them then adding some transitions then adding some music and so on uh just to see how this works so the first thing we want to do is we want to define clips we want to take for example the first video of the smoke animation if we want to take the part from second 10 to second 20. how do we do that we can say something like clip 1 equals and then i say video file clip and here i enter the file name now so i enter 1.mp4 which is the first video and then i can apply certain functions at the end of that video file clip so i can say ok dot sub clip and this is going to return again a clip now if i'm not mistaken video file clip um do we see that here okay we don't see that but video file clip returns a clip and when i then apply sub clip it returns a clip and i think that when i apply something else it also returns a clip so it's a little bit like a builder as far as i know but the important thing is we we do something like mp4 and then dot sub clip and here we specify second 10 second 20. this means we take the video uh at the location1.mp4 this is the file name then we sub clip meaning that we take uh the part of the video from second 10 to second 20 and then we can add some more stuff in the end if we want to we're just going to leave it like that for now then we can say clip 2 equals video file clip 2.mp4 and we can do something something similar here so for example same time range and then we can say okay clip three is going to be just the one mp4 again dot sub clip 20 30 for example just to mix it up a little bit um [Music] yeah so this is without effect this is without transitions this is just a basic uh the basic clips here and now we can go ahead and combine these into one full video that we then save that we then export to the hard drive and how we can do that is we need to import another function which is called concatenate video clips and as the name already says it concatenates video clips so we're going to say combined equals concatenate video clips and here we basically just pass a list of clips so clip 1 clip 2 clip 3 like that and all we need to do now to produce to produce the final video is combined dot write video file and then combine dot mp4 for example so we run this and we're hopefully going to see the progress here you can see it's working uh it may take a little bit longer if you don't have a strong computer and then it's done we can go ahead and open it up in the explorer and then just click on it and you can see here that we have the first 10 seconds are the animation from video one from the second 10 to second 20 and then after 10 seconds we're going to switch you're going to see here there you go it switches and then at around second 20 there you go it switches back again so we just combined three clips we took a video we took two videos we got some clips out of that we combined them in a certain order and that's basically it so this is how you basically just cut videos now you can also add some animation you can add transitions you can add effects uh so let me show you a very simple one you can go just ahead here and say clip four and we're gonna take uh let's say again 10 20 so we basically take the same clip but we're now gonna add some effects and how do we add effects now i'm not sure if we need to import that no we don't need to import that but we need to import uh something else so we can call the dot fx function to add some effect to add some filter basically and here now we need to use vfx so we need to import vfx and we're going to use vfx for um for all sorts of filters for all sorts of video transitions everything that's related to video effects is in vfx everything that's related to audio effects is in afx so later on we're also going to import afx now let me just see if you can see all the imports yes you can um and now what we can do here is we can just say okay let's say vfx dot color x and now i can just say 1.5 and this is going to change the color color x value i can also change the contrast now let me just start a new line here how do we do that in python again like that right uh so dot fx and then vfx dot loom underscore contrast and we're gonna say this is 0 50 128 or something i hope these are acceptable values now let me indent that a little bit here and now we can just add this clip here in the end and this is going to take some time probably because it has to do the calculations so you're going to see that when it works it's going to start quite quickly and then in the end it's going to take some more time i think there you go now now it uh it is a little bit slower because it has to apply onto each frame all these effects now those are just two effects but if you have a lot of effects on each frame this needs to be calculated what it looks like and so on uh and then it produces the final video and now we're going to see that we have an additional clip an additional 10 second clip and if we open this up again in the explorer and we go to the end you're going to see that we now have this thing here which is essentially uh the first the first clip with these filters again i didn't choose quite i didn't choose any interesting or meaningful filters i just showed you something again if you want to see all the functions like usual you just go to documentation but this is the general idea you call the fx function on a clip and then you pass vfx for video effects and then uh you you pick some filter you pick some setting that you want to change now another thing that we can do here is transitions as i already said so what i usually do in my videos you have probably noticed is i have one clip at least when i split the video up into multiple clips i have one clip and then in the end i have a one second fade out or half a second fade out and then fade in again to switch between the individual clips so we can do something similar here as well we can just say fx and then vfx dot fade in and then specify the second so one second fade in and then also fx vfx and now i think i'm starting to block the code right there you go so let me just move this down here um i can say vfx dot fade out and vfx fade out is gonna have also one second and now i can just take this here i can copy this and i can paste this and there you go and that will then generate a simple fade in fade out effect now before we just do that i also want to add an audio clip or actually let's first see what what this looks like since we don't have the effects now anymore this is not going to take too long uh but then we're also going to get uh we're we're going to add the audio from my intro just to have some audio again the result of this is not going to be a fancy video it's just showing you how to use everything and then you can make your own fancy video if you want to but now we should be able to see a fade in effect there you go we saw the fade in effect now at second 10 we see it fade out fade in and so on and so forth so this is quite useful um if you want to use python for video editing i don't i use davinci resolve but still you can use python for video editing if you want to now let's go ahead and get the audio clip and for that we're going to also import here the audio file clip and we're also going to import afx for the audio effects um and how do we do that essentially we just say okay audio equals audio file clip and we don't have to load an mp3 or an audio file we can also get a video file and get the audio from that video file and now we can add some some effects here as well so we can say fx and then for example af afx dot audio underscore fade in to fade in the audio obviously and also here we can specify a second and then we're going to say fx af afx and we can change the volume now let's do it first without the volume change i'm actually not sure if you're going to hear something because i think i have disabled the desktop sound um but this is how you create your audio clip and now to the final video clip you just have to say combined dot audio equals and then this is another thing that we need to import here composite audio clip um and we just say here composite audio clip audio like that so we can run that and then it's going to get the audio from the intro again i don't think that you're going to hear it maybe i can change something about the settings here so i now enable the desktop audio maybe you're going to hear something if not you can just run this on your own computer and you're going to see that the audio is there and again [Music] you want to do it on your own and um now we can add a final filter here we can just say fx af x dot uh what was the name volume x and this basically means that we can uh increase or decrease the volume so if i say 0.1 we're not going to hear anything now i don't know if you heard anything um right now but if you have heard anything right now now you would hear it way uh way more silent so the the volume is going to be decreased and we can see the effect hopefully you can also hear it in the video again otherwise just try it on your own and there you go so there is still audio but it's very very silent i'm not sure if silent is the correct word so this is how you can cut videos how you can edit videos in python you just have video clips you have audio clips you can cut them you can add effects you can add transitions you can change the audio you can fade in fade out and a ton of more things that you can read about in a documentation this was a very basic introduction i hope you enjoyed it and that's it so that's it for today's video i hope you enjoyed it and i hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you

Original Description

Today we learn how to automate video editing using Python and MoviePy. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to automate video editing using Python and MoviePy, covering topics such as clip creation, concatenation, video effects, and audio addition. By following this lesson, viewers can learn how to create automated video editing scripts and apply effects to their videos.

Key Takeaways
  1. Import the MoviePy library
  2. Define clips using video file clip and sub clip functions
  3. Add transitions and music to the video
  4. Combine multiple video clips into one video
  5. Apply effects to clips using vfx and afx functions
  6. Add audio to a video using audio file clip and afx functions
  7. Combine video and audio clips using composite audio clip function
💡 The MoviePy library provides a simple and efficient way to automate video editing tasks in Python, allowing users to create complex video edits with minimal code.

Related Reads

📰
Indian AI coding startup Emergent becomes a unicorn with $130M Series C
Emergent, an AI coding startup, reaches unicorn status with $130M Series C funding, achieving $120 million annualized revenue run rate and 200,000 paying customers, demonstrating the growing demand for AI-powered coding solutions
TechCrunch AI
📰
Disposable software
Learn how the rise of AI-powered coding tools like Claude Code is changing the way we think about software development and maintenance
Seth Godin's Blog
📰
Join live event: Building With AI Without Creating Technical Debt
Learn how to build with AI without creating technical debt in a live event hosted by Software Mansion
Reddit r/learnprogramming
📰
I Built an AI Coding Cost Tracker to Finally See What Copilot and Cursor Are Actually Costing Me
Learn how to track the costs of AI coding assistants like Copilot and Cursor to optimize your workflow and budget
Dev.to AI
Up next
How to Create ONE PAGE Website using Claude AI (FREE & FAST)
Quick Tips - Web Desiign & Ai Tools
Watch →