Moviepy is Awesome! — Part1

Tejas Bobhate
3 min readApr 8, 2018

Moviepy is a video editing library in python.

Photo by Jakob Owens on Unsplash

If you deal with a lot of videos, you must have came across a case where you either needed to process a video at frame level or compose (JARGON) small video clips into a larger one or add some subtitles to a video, manipulate existing audio and what not! You can do all of it with Moviepy.

In this series I’ll try to explain how to use Moviepy to get things done.

To get started you need to have python installed on your system. You can refer to this article for setting up python on mac, this one for linux based systems. Once you have python on our system, you can install Moviepy with the instructions on it’s github page.

Let’s get started with some basic terms related to videos.

A Video in general, is a sequence of image frames played at a certain fps. Every video has certain attributes like duration, fps(frames per second) etc. We will see more of them as and when needed.

In this article let’s start with understanding basic usage of Moviepy, later we will see how it works internally.

Let’s see how to read a video in Moviepy.

Moviepy has a class called VideoFileClip to deal with videos.

from moviepy.editor import VideoFileClipmy_clip = VideoFileClip(/path/to/video)
print("Duration of video : ", my_clip.duration)
print("FPS : ", my_clip.fps)

Now we can start tinkering with the video.

Let’s say the video duration is 10 seconds and you want to cut out a small section of the video (from 2 sec to 7 sec) as a new video. You can do,

new_clip = my_clip.subclip(2,7)
print('new clip duration : ',new_clip.duration)
new_clip.write_videofile("new_clip.mov", codec = "libx264", fps=25)
new_clip.close()

Yes, you can take out a small portion of the video aside by using .subclip(start_time, end_time) method.

You can save the new_clip to disk with .write_videofile method. You need to mention the required filename, fps for the new clip (if not mentioned, my_clip.fps will be taken as default) and the codec.

In the end we call .close() on new_clip.

Time being we will use libx264 as codec ( more on this later) and 25 as the fps.

When python starts executing write_vid… command you will see a progress bar in the terminal / IDE. It will indicate how many frames have been saved to disk out of total frames and the rate at which it is rendering.

e.g. In above case we are saving a new_clip to disk which of duration 5 seconds and at 25 fps. That means Moviepy has to render 125 frames. You will see a progress bar with information like,

80%|████████████████████████████████████████████ | 100/125 [00:10<00:02, 10it/s]

it means, 100 frames out of 125 have been saved to disk in 10 sec and it will take 2 more seconds to complete the render. On an average ffmpeg (more on this later) is rendering (saving) 10 frames to disk per second.

Higher the it/s value faster is the rendering.

Cool, now we know,

  1. How to read a video in Moviepy
  2. Subclip a certain part of the video
  3. Save a video to disk.

This is just the beginning. Let’s do more.

Say you want to rotate the subclipped video through 90 degrees counter clockwise.

from moviepy.editor import VideoFileClipmy_clip = VideoFileClip(/path/to/video)new_clip = my_clip.subclip(2,7).rotate(90)new_clip.write_videofile("new_clip.mov", codec = "libx264", fps=25)

If you notice, it is same as the code above with just a new function call.

The sequence of function calls is like subclip then rotate.

So far so good.

Now, Let’s make a gif for social media.

from moviepy.editor import VideoFileClipmy_clip = VideoFileClip(/path/to/video)new_clip = my_clip.subclip(2,3) new_clip.speedx(2).to_gif('my_first_gif.gif')

Above code will subclip a 1 sec clip from original video, save it as a gif where the clip plays at 2x of the original speed. Do try it out. How about a meme?

I know it’s fun :)

Read Part2 here.

Stay tuned for more.

Thank you for reading!

(more on this later) will be covered as and when required.

--

--