Installation¶
In [2]:
!pip3 install -q moviepy
Use moviepy to Manipulate Videos¶
Download a short video.
In [3]:
!wget --no-check-certificate https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_5mb.mp4 -O bunny.mp4
In [6]:
import numpy as np
from PIL import Image
import cv2
from moviepy.editor import VideoFileClip
Extract and check the first frame.
In [7]:
vidcap = cv2.VideoCapture("bunny.mp4")
In [8]:
success, arr = vidcap.read()
In [9]:
arr
Out[9]:
In [11]:
arr.shape
Out[11]:
The numpy array is the BGR (instead of RGB) representation of the image,
which means that you have to flip the last dimension of the numpy array
so that it can loaded using the Pillow library correctly.
In [10]:
Image.fromarray(np.flip(arr, 2))
Out[10]:
In [9]:
img.size
Out[9]:
Extract a sub video with part of the original frame.
In [12]:
video = VideoFileClip("bunny.mp4").subclip(0, 10).crop(x2=640)
video.write_videofile("sub.mp4")
In [13]:
vidcap = cv2.VideoCapture("sub.mp4")
In [14]:
sucess, arr = vidcap.read()
In [15]:
img = Image.fromarray(np.flip(arr, 2))
img
Out[15]:
In [16]:
img.size
Out[16]:
In [ ]: