Heading image

This is Day 12 of the #100DaysOfPython challenge.

This post will use the MoviePy library to annotate some captions onto a short video I recorded about my site workingoutloud.dev.

Prerequisites

Getting started

At this stage, we can write our script to annotate the video.

Annotating the video

Original video

The video is a short clip of my workingoutloud.dev site. It is around 9 seconds long and I want to add some text to it.

From watching the video, there are three things that are essentially in the demo:

  1. Viewing tasks and subtasks.
  2. Sorting subtasks.
  3. Opening the current goals.

These occur at the 0:00, 0:03 and 0:06 second mark respectively.

We can add in some captions for each of these easily thanks to MoviePy.

In main.py, add the following:

In the above code, we simply import the required modules and then we create a VideoFileClip object from the video file (relative to the project root directory).

We now want to create a TextClip object for each of the three points in the video.

According to the MoviePy TextClip docs, we can see the TextClip constructor can take some relevant arguments around the text, font, color and font size.

The TextClip instance itself then has access to methods to set the position, duration and start time. We will abstract our sensible defaults into a function and implement this:

After writing out the helper method, we are creating three TextClip instances for each of our different annotations that we wish to add to the video.

Finally, we can compose them all together into a CompositeVideoClip object and write out the video file.

I am also writing out a .gif file to use as a preview of the video on this post.

We can now run our script with python main.py. There will be some progress bars to indicate the progress.

After the script has completed, we can now see wol_dev_edited.mp4 and wol_dev_edited.gif in the hello-moviepy directory.

The outcome looks like so:

Annotated video

Summary

I will be hoping to make use of this to help annotate some of my videos that I am working through to save some time in future.

The final code looks like so:

Resources and further reading

  1. MoviePy TextClip docs
  2. Pipenv
  3. MoviePy
  4. GitHub — project code

Photo credit: element5digital

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

--

--

Senior Engineer @ Visibuild

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store