Compress video using moviepy in Python
In this post, we are going to learn how to compress a video using moviepy in Python. Moviepy is a Python video editing module that allows users to perform basic video editing to a given video. We install the moviepy module like every other Python module which is pip install moviepy
Now how do we import this module into our code? We are using the import
keyword to make the moviepy module available for our code. We use the following command:
from moviepy.editor import *
By compressing video files, we can reduce their size and make them easier to transfer.
To resize the video using moviepy we are going to use resize method. Resize method has an object called VideoFileClip object which will help us along the way.
Python Code: Compress video using moviepy
Downsizing the video by 70%
# Importing the module from moviepy.editor import * # uploading the video we want to edit video = VideoFileClip("hello-world.mp4") # getting width and height of video 1 width_of_video1 = video.w height_of_video1 = video.h print("Width and Height of original video : ", end = " ") print(str(width_of_video1) + " x ", str(height_of_video1)) print("#################################") # resizing.... video_resized = video.resize(0.7) # getting width and height of video 2 which is resized width_of_video2 = video_resized.w height_of_video2 = video_resized.h print("Width and Height of resized video : ", end = " ") print(str(width_of_video2) + " x ", str(width_of_video2)) print("###################################") # displaying final clip video_resized.ipython_display()
Output:
Width and Height of original video : 1280 x 720 ################################# Width and Height of resized video : 896 x 896 ################################### Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4
Example 2:
Downsizing the video by 20%
# Importing the module from moviepy.editor import * # uploading the video we want to edit video = VideoFileClip("coding.mp4") # getting width and height of video 1 width_of_video1 = video.w height_of_video1 = video.h print("Width and Height of original video : ", end = " ") print(str(width_of_video1) + " x ", str(height_of_video1)) print("#################################") # compressing video_resized = video.resize(0.2) # getting width and height of video 2 which is resized width_of_video2 = video_resized.w height_of_video2 = video_resized.h print("Width and Height of resized video : ", end = " ") print(str(width_of_video2) + " x ", str(width_of_video2)) print("###################################") # displaying final clip video_resized.ipython_display()
Output:
Width and Height of original video : 1920 x 1080 ################################# Width and Height of resized video : 384 x 384 ################################### Moviepy - Building video __temp__.mp4. MoviePy - Writing audio in __temp__TEMP_MPY_wvf_snd.mp3 MoviePy - Done. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4
With this second example, we have concluded our tutorial. Do you want to know how to Download the Youtube playlist as mp3 in Python, follow this tutorial Download the Youtube playlist as mp3 in Python
Leave a Reply