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