Python MoviePy Tutorial Part 1

Hi, welcome to my blog. In this article you will get to know about Python MoviePy. Python MoviePy is a Python Library for video and audio editing, creating, modifying the video and audio files.

Well let's start learning the usages of Python MoviePy Library. You can skip to your required content instead of reading all the content.
 

 
 
Python MoviePy Tutorial Code With TJ Part 1

 
 

#1 How to Install MoviePy Python 

To install moviepy on Windows system just type below command
pip install moviepy

To install moviepy on Ubuntu/ any Linux system run the below command on the terminal.
sudo pip install moviepy

Also you can even download the MoviePy source code from Python Package Installer and then install on your machine here is the link PyPi
 
 
 

#2 Combine Two Video in to One Video

You got two video files and now you try to combine both the video files into one video file. Assuming you have video1.mp4 (having length 1 minute) and video2.mp4 (having length 30 seconds) the output video will be of length 1 minute and 30 seconds.

Also you should take care of video files having different widths and different heights. First convert the video files according to your required width and height using resize((width, height))where in the below example I am trying to resize the first video according to the second video and then combine both the videos
 
You can combine these two files using below code make sure you first print the width and height and then resize accordingly and then concatenate both the videos.
 
from moviepy.editor import *
video_clip1 = VideoFileClip("video1.mp4")
video_clip2 = VideoFileClip("video2.mp4")
print("Video1 Width : " + str(video_clip1.w) + " Height : " + str(video_clip1.h))
print("Video2 Width : " + str(video_clip2.w) + " Height : " + str(video_clip2.h))

resized_video1 = video_clip1.resize((3840, 2160))
videos = [resized_video1, video_clip2]

final_video = concatenate_videoclips(videos, method="compose")
final_video.write_videofile("output.mp4") 
 
Finally use the write_videofile() function to write the output.mp4 video. 
 
 
 
 

#3 Combine Two Audio in to One Audio File

You got two audio files and now trying to combine both the audio files into one audio file. Assuming you have audio1.mp3 (having length 1 minute) and audio2.mp3 (having length 2 minutes)
 
The final audio.mp3 file will be of 3 minutes

from moviepy.editor import concatenate_audioclips, AudioFileClip

input_audio1 = AudioFileClip("audio1.mp3")
input_audio2 = AudioFileClip("audio2.mp3")
final_audio = concatenate_audioclips([input_audio1, input_audio2])
final_audio.write_audiofile("audio.mp3")
 
Above is the code to combine two audio files and finally write the audio.mp3 file as output.


 

#4 Adding Background Music to Video File

You got two video files and an audio file having the same length, video length is 1 minute and audio length is also 1 minute. Try to add audio to video and finally write the new video file.
 
You can add background music to your video file using below code. 
 
import moviepy.editor as mymovie

inputvideo = "video.mp4"
inputaudio = "audio.mp3"
outputvideo = "output.mp4"

videoclip = mymovie.VideoFileClip(inputvideo)
audioclip = mymovie.AudioFileClip(inputaudio)
finalclip = videoclip.set_audio(audioclip)
finalclip.write_videofile(outputvideo, fps=60)
 
Finally write the video file to output.mp4 by giving the argument fps equals 60.
 

 

#5 Cut Audio in to Two Audio Files

With the below code you can cut audio files into two / any numbers. Just get the audio clip into your program and then get the sub clip, provide the start and end time of the sub clip and finally write to the audio file.
 
from moviepy.editor import AudioFileClip

input_audio_clip = AudioFileClip("audio1.mp3")
final_clip = input_audio_clip.subclip(30, 60)
final_clip.write_audiofile("output.mp3")
 
The audio file will be saved to the output.mp3 file.
 
 
 

#6 Adding frame to video

Adding a margin / frame looks good to videos. You can do this using the below code. Get the video, add the margin and write the new video file.
 
Below is the code.
 
from moviepy.editor import VideoFileClip

input_clip = VideoFileClip("input.mp4")
final_clip = input_clip.margin(60)
final_clip.write_videofile("output.mp4", fps=60)

 
Focus on margin(60)the 60 is the margin size, you can change this as per your requirement.
 
 
 

#7 Cut video file into smaller sub clips / Trim Video

You got a video file that is of 2 minutes and you need to cut them out into 2 videos of size 1 minute each. You can do this using the below code: cut the video file into sub clips and save each video with the different name.

 
Below is the code.
 
from moviepy.editor import VideoFileClip

input_video = VideoFileClip("video.mp4")
clip1 = input_video.subclip(0, 59)
clip2 = input_video.subclip(60, 120)
clip1.write_videofile("first_half_video.mp4", fps=60)
clip2.write_videofile("second_half_video.mp4", fps=60)
 
You can change the length that is from 0 - 59 accordingly as per your requirement. You want to cut only 30 seconds then you can provide 0 - 29 seconds in the sub clip and use the write_videofile() function call.
 
 

 
 

#8 How to Get Audio File / Video File Duration / Length

Python MoviePy Library supports the duration function to get the length of Audio / Video. The duration is in seconds. for example if the audio is of 1 minute 30 seconds. The duration function will return 90 seconds

Below is the code to print duration of Audio and Video Files

from moviepy.editor import AudioFileClip, VideoFileClip

audio_clip = AudioFileClip("audio.mp3")
video_clip = VideoFileClip("output.mp4")
print(audio_clip.duration)
print(video_clip.duration)
 
 
 
 
 

#9 Get the Audio from Video / Convert MP4 to MP3

You got a video file, having good background music, you are interested in audio rather than video. So to copy the audio from the video you can do it using the moviepy editor and save the music of a video file as shown below.

Below is the code to get the audio file.

from moviepy.editor import VideoFileClip

final_audio = VideoFileClip("video.mp4").audio
final_audio.write_audiofile("audio.mp3")

You can also change it to another audio format as per your requirement.
 
 
 
 
 

#10 How to Get FPS of Video

Python MoviePy Library supports fps function to get the frames per second Video. The fps can be retrieved as shown below code.

Below is the code to print frames per second of video.mp4 Files

from moviepy.editor import VideoFileClip

video_clip = VideoFileClip("video.mp4")
print(video_clip.fps)





#11 How to Get Resolution of Video

There are two methods in MoviePy Library to print resolution, the width and height can be printed individually using w x h or in list format [w, h].

Below is the code to print resolution using two methods.

from moviepy.editor import VideoFileClip

myclip = VideoFileClip("video.mp4")
print(myclip.w, myclip.h) # Print Width / Height
print(myclip.size)        # Print List containing
Width / Height





#12 Mute Audio in Video/ Write Video File without Audio/ Remove Audio From Video

You have a video with audio and your requirement is to get only the video file without any background music/ mute the audio from video.

This can be done by using the below 3 line code.

from moviepy.editor import VideoFileClip

myclip = VideoFileClip("input_video.mp4").without_audio
myclip.write_videofile("output_video.mp4")






#13 Speed Up Video using MoviePy

Given a video file video.mp4 you want to modify the speed of the video. This can be done by modifying the fx (Video Effects) parameter of video. To get the same quality of the video you should also set the fps to the same as fx parameter. In the below example I have set fps to 3 and also the speed to 3. In my case Thirty five second video becomes a twelve second video, changing the 3 fps and speed value according to your requirement.


Below is the code.

from moviepy.editor import VideoFileClip
import moviepy.video.fx.all as fx

video_clip = VideoFileClip("video.mp4")
video_clip = video_clip.set_fps(video_clip.fps * 3)
final_clip = video_clip.fx(fx.speedx, 3)
final_clip.write_videofile("output.mp4")




#14 Slow Down Video using MoviePy

Given a video file video.mp4 you want to slow down the video. This can be done by modifying the fx (Video Effects) parameter of video.  In the below example I have set the speed to 1/3. In my case Thirty five second video becomes a 1 minute and 30 seconds video, changing the 1/3 speed value according to your requirement.


Below is the code.

from moviepy.editor import VideoFileClip
import moviepy.video.fx.all as fx

video_clip = VideoFileClip("video.mp4")
final_clip = video_clip.fx(fx.speedx, 1/3)
final_clip.write_videofile("output.mp4")

 

 

#15 Resize Video using MoviePy

Given a video file video.mp4 you want to resize it to another resolution. You can try the below code to resize the video file by providing the width and height of the required video..

Below is the code for modifying the video to Ultra HD resolution (3840x2160)

from moviepy.editor import VideoFileClip
video_clip = VideoFileClip("video.mp4")
final_video = video_clip1.resize((3840, 2160))
final_video.write_videofile("quadhdvideo.mp4")
 

You can also change it to Full HD (1920x1080), HD (1280x720)

 
 
 
 

#16 Audio Mixing

Given two are more number of audio files,  if you want to mix  all the audio files into One audio file/  sandwich the file all together into single audio file so you can do this using moviepy editor CompositeAudioClip, as shown below


from moviepy.editor import AudioFileClip, CompositeAudioClip
 
audio1 = AudioFileClip("file1.mp3")
audio2 = AudioFileClip("file2.mp3")
audio3 = AudioFileClip("file3.mp3")
 
result_audio = CompositeAudioClip([audio1, audio2, audio3])
result_audio.write_audiofile("result.mp3", fps=audio1.fps)





Creating Audio with Some Delay

If you want to set a delay in between the audio clip start you can use set_start() Function call and provide the delay as shown below.

result_audio = CompositeAudioClip([audio1, audio2.set_start(5), audio3.set_start(10)])
 

 

 

#17 Zooming In and Zooming Out Video Every 5 seconds

Given a video your task is to zoom in the video for first 5 seconds and then zoom out the video for another 5 seconds and then keep repeating the cycle for complete video. You can use the sin() function to perform the resizing of video as shown below.


from moviepy.editor import VideoFileClip, CompositeVideoClip
from math import sin

size = (1920, 1080)
myvideo = VideoFileClip("video.mp4")

clip = myvideo.set_fps(25).set_duration(20).resize(size)
clip = clip.resize(lambda swelling: 1.3 + 0.3 * sin(swelling / 3))
clip = clip.set_position(('center', 'center'))

clip = CompositeVideoClip([clip], size=size)
clip.write_videofile('zoomout.mp4')

 
 
 

Conclusion :  This is the first part of the tutorial. Part 2 of this tutorial will be available soon, watch out for this section.


Comment down below your thoughts, suggestions, queries related to this article. 

 

Post a Comment

3 Comments