Hands-on machine learning exercise using moviePy library
In this post, I will show you how to add text to your videos using Python. It may sound very difficult at first glance, but trust me it’s much easier than you think, and also it’s cool. This will be a simple exercise where we will be able to see how machine learning can be used in real life. If you check my other articles, you will see that I like to work on hands-on projects. I think this is the best way to practice our coding skills and improve ourselves. You will find a handful of project ideas online when you do a little research. But I highly recommend doing your own brainstorming and writing down your personal project ideas, then start working on how you can make them happen. Let’s get back to our project.
Table of Contents
- Getting Started
- Step 1: Import Library
- Step 2: Define your video
- Step 3: Create your text
- Final Step: Rendering Text on Video
As you can understand from the title, for this project we will need a video recording file. It can even be a recording of yourself speaking to the camera. Using a library called MoviePy, we will add text to our video recording. First, we will import the libraries, I will show you how to install them. In the second step, we will define the video we will be using for the project, it can be a short video. In the third step, we will define a text variable, it will be the text that will be added to the video. And finally, we will render the text on the video recording. If you are ready, let’s get started by installing the libraries!
We are going to use one library for this project, but to make the text rendering we will also need to install another module. The library is called MoviePy, and here how to install it:
pip install moviepy
You can learn more about moviepy from its official documentation page.
And the second module that we need to install is called ImageMagick. Without this module, you may get an error while rendering text on to the video recording. To install ImageMagick, I used Homebrew which is a package manager for macOs and Linux devices. You can search online “how to install ImageMagick” if you are using a different operating system.
brew install imagemagick
You can learn more about imagemagick from its official documentation page.
MoviePy is a library that can read and write all the most common audio and video formats, including GIF. If you are having issues when installing MoviePy library, try by installing ffmpeg. Ffmpeg is a leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter, and play pretty much anything that humans and machines have created.
Now, we should get to writing code in our code editor. We will start by importing the libraries.
Step 1 — Import Library
import moviepy.editor as mp
Yes, that’s all we need to get the task done. Without losing any time let’s move to the next step.
Step 2 — Define your video
In this step, we will define a variable called “my_video”. Then using a special method by moviepy library, we will define our video file. Also, I want to keep the audio of the video, that’s why the value for the video is True.
my_video = mp.VideoFileClip(“data/video_test.mov”, audio=True)
By the way, there are many video formats, some of them can be listed as:
- MP4 (mp4, m4a, m4v, f4v, f4a, m4b, m4r, f4b, mov)
- 3GP (3gp, 3gp2, 3g2, 3gpp, 3gpp2)
- OGG (ogg, oga, ogv, ogx)
- WMV (wmv, wma, asf*)
Make sure your path is correct, and your video format is working properly. If you are having issues in this step, try converting your video to different formats.
Before we move to the next step, let’s define the width and the height of our video. We will need these when rendering the text into the video. Don’t worry, this will be very easy.
w,h = moviesize = my_video.size
Step 3— Create your text
In this step, we will define a new variable for our text. I called it “my_text”. And using the TextClip method we will be able to create the text.
my_text = mp.TextClip("The Art of Adding Text on Video", font='Amiri-regular', color='white', fontsize=34)
As you can see, we can even play with the parameters. It’s really cool that we can customize our text. Text font, text color and font size can be changed to your preferences.
Now, we will locate the text on our video. And also we can design a background for our text. You can play with the parameters and see how it works. Background color, background opacity, and position of the text are some of the parameters.
txt_col = my_text.on_color(size=(my_video.w + my_text.w, my_text.h+5), color=(0,0,0), pos=(6,'center'), col_opacity=0.6)
Final Step — Rendering text on video
Great! You made it until the final step. This step is where the magic happens.
First, we will define a new variable called “text_mov” where we can add some animation. The movement of the text over the video. You can find your favorite result by playing with the parameters. Here is the line of code where we do the animation:
txt_mov = txt_col.set_pos( lambda t: (max(w/30,int(w-0.5*w*t)),max(5*h/6,int(100*t))) )
Finally, it’s time to render the text on our video. Thanks to the moviepy library, we will use a method called “CompositeVideoClip” which will do the rendering for us. As mentioned earlier, if you are getting errors in this line I highly recommend installing imageMagick and ffmpeg modules. Here is the line to make the magic happen:
final = mp.CompositeVideoClip([my_video,txt_mov])
Export your result
Almost there, to see how the final result looks, let’s export the video. We will save it as a new video file. What’s really cool about exporting is you can adjust the video format and even the frames per second. Here is the code to export the first 5 seconds of the rendered video:
Sharing the result
Congrats! You have created a program that renders a custom text on a video without using any video editing software. It is really cool to see how machine learning can be used in our daily life. Hoping that you enjoyed reading this post and working on the project. I would be glad if you learned something new today. Working on hands-on programming projects like this one is the best way to sharpen your coding skills.
Feel free to contact me if you have any questions while implementing the code.
Follow my blog to stay inspired.