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