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.
If you are new to my blog please support me by subscribing my YouTube Channel Tech blog
#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")
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)
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")
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. 
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)
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")
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
from moviepy.editor import VideoFileClip
input_clip = VideoFileClip("input.mp4")
final_clip = input_clip.margin(60)
final_clip.write_videofile("output.mp4", fps=60)
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 in to smaller sub clips / Trim Video
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)
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 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)
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.
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 you requirement.final_audio = VideoFileClip("video.mp4").audio
final_audio.write_audiofile("audio.mp3")
#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)
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
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")
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")
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")
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..
from moviepy.editor import VideoFileClip
video_clip = VideoFileClip("video.mp4")
final_video = video_clip1.resize((3840, 2160))
final_video.write_videofile("quadhdvideo.mp4")
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)
0 Comments