TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial…

Automate Video Editing with Python

Samir Saci
TDS Archive
Published in
6 min readNov 10, 2022

--

Flowchart showing the process of automating video editing with Python. The raw video is processed by muting the background music and cutting the video into shorter clips. Each short video is then paired with text-to-speech comments, creating three distinct clips, ‘Short 1’, ‘Short 2’, and ‘Short 3’, each with its corresponding comment.
(Image by Author)

Looking for a way to automate your training videos and make them more engaging for your warehouse operators?

I have 100 videos to prepare with simple editing. How can I use Python to automate the process?

In this article, we’ll show you how to use the Python library Moviepy to edit explainer videos.

This tool can automatically cut them into short clips, remove background music and add text-to-speech comments.

Scenario

You are supporting the operational manager in the logistics department of an international fashion retailer.

She would like to prepare training videos to support onboarding new operators and administrative staff.

Illustration of the inbound process for warehouse operations, showing unloading, receiving, and pallet wrapping. Step 1 arranges the receiving time slot; Step 2 shows unloading goods from the truck; and Step 4 depicts pallet wrapping using a semi-automatic wrapping machine. The diagram emphasizes productivity optimization during these steps in the inbound process.
Examples of training screens — (Image by Author)

Your colleague has shared an explainer video (video by the author) introducing all the processes of warehouse receiving.

You want to cut this video into short clips explaining each process to animate training sessions.

How can you use python to cut the video and create shorts?

How to edit videos with Python?

Training Material

This video presents a complete overview of several processes involved in receiving products in a Warehouse.

Expanded view of the inbound process in a warehouse, detailing unloading, receiving, and pallet wrapping operations. The process includes arranging the receiving time slot, unloading goods, checking quantities, receiving new inventory, and wrapping pallets using a semi-automatic machine. The image illustrates the step-by-step warehouse workflow for efficient inbound operations.
Processes presented in the video — (Video by Author: Supply Science)

Therefore, you would like to use this video that is listing the different processes as training support for your onboarding sessions:

  • Total Duration: 00:03:18
  • 18 sections, including the intro and outro
  • Background music but no background voice

Objective

You want to cut the video, remove the background music and create shorts with audio comments.

Flowchart showing the process of automating video editing with Python. The raw video is processed by muting the background music and cutting the video into shorter clips. Each short video is then paired with text-to-speech comments, creating three distinct clips, ‘Short 1’, ‘Short 2’, and ‘Short 3’, each with its corresponding comment.
Workflow in three steps — (Image by Author)

The idea is to build an automated workflow using Python that will

  1. Load the original video
  2. Remove the background music
  3. Cut the video into several shots that focus on different processes
  4. Add audio comments generated with a Text-to-speech solution

Can we automate each step with Python?

Solution to Automate Video Editing with Python

We recommend using the Python library Moviepy to automate the editing of explainer videos and create shorts with text-to-speech comments for training purposes.

In the following sections, we will explore a three-step workflow that involves

  1. Loading the original video
  2. Removing the background music
  3. Cutting the video into several shots
  4. Adding audio comments generated with a Text-to-speech solution.

Please refer to the official documentation for more details on the Moviepy library.

🏫 Discover 70+ case studies using python to automate manual tasks 🤖 and support business optimization 🏪 in this: Cheat Sheet

Implementation

Step 1: Loading the video

We start by loading the original video's .mp4 file, which you can find on my YouTube channel (Link).

Step 2: Cut the video

As you want to focus on some specific processes, you will not use the entire video.

Flowchart illustrating the automated video editing process using Python for warehouse training videos. The image shows a timeline of the video being divided into steps, with each step representing a specific process, such as unloading, receiving, and pallet wrapping. Each process is accompanied by timestamps (e.g., 00:00:15, 00:00:30) to indicate where the video should be cut, creating individual video segments focused on specific tasks.
Generate shorts by cutting at certain times — (Image by Author)

Therefore, we need to find a method to cut the video at specific times to generate short clips that will be used to show a specific process.

Function: Convert to seconds

💡 Insights
We’ll need this function later when we cut the video.

Action: Cut the video

📹 Result

(Video by Author)

💡 Insights
You have a short video clip with the background music cut from the original video at the right timestamps.

As we want to add Text-to-speech, how can we remove the original music?

Step 3: Remove the sound

You would like to play videos without sound, so you want to take your shorts and remove the sound.

📹 Result

💡 Insights
You can also apply a fraction of the volume using .volumex(fraction) that can be lower than 1(reduction) or higher than 1 (increase)

Now lets merge the videos.

Step 4: Combine two shorts

You have cut two videos, and would you like to play them successively? There is a Moviepy function for this,

📹 Result

Step 5: Stack the videos

Would you like to show several processes simultaneously?

You can stack the videos on the same screen.

📹 Result

Example of synchronized videos stacked with Python Moviepy
Stacked videos — (Image by Author)

Step 6: Add some lag

It can be done if you want to stack the videos but with a lag of 5 seconds between each video. (I have no idea why someone would do that, but why not!)

📹 Result

Example of four stacked video with time lags using Python Moviepy
Stacked video with lag — (Image by Author)

Final Step: Add Text-To-Speech Comments

We have generated comments using a Text-to-speech API.

Can we add them to the video?

The answer is yes!

📹 Result

💡 Insights
Google TTS (free tier) is very bad! Do you know a better one at a reasonable price?

Conclusion

Automating video editing with Python can be a powerful tool for creating training materials and explainer videos.

With Moviepy, you can easily cut, combine, and add text-to-speech comments to your videos, all with just a few lines of code.

And while it may not have all the bells and whistles of professional editing software, it gets the job done and saves you time and effort.

So why not give it a try and see how it can streamline your video production process?

Moreover, I have shared with you only a fraction of what the library can do. I strongly suggest reviewing the documentation for more insights.

You are ready to design training materials or start your fully automated Tiktok Channel.

Discover another Python library to automate the design of miniatures, illustrations or any graphic work in this article.

If you want also to automate graphic design, there is another tutorial for you

About Me

Let’s connect on Linkedin and Twitter. I am a Supply Chain Engineer who uses data analytics to improve logistics operations and reduce costs.

For consulting or advice on analytics and sustainable supply chain transformation, feel free to contact me via Logigreen Consulting.

If you are interested in Data Analytics and Supply Chain, look at my website.

💌 New articles straight in your inbox for free: Newsletter
📘 Boost your Productivity with Data Analytics: Productivity Cheat Sheet

I am sharing other video tutorials on automating manual tasks like PowerPoint, Graphic Design or Business Planning in a dedicated YouTube channel,

💡 Follow me on Medium for more articles related to 🏭 Supply Chain Analytics, 🌳 Sustainability and 🕜 Productivity.

--

--

TDS Archive
TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Samir Saci
Samir Saci

Top Supply Chain Analytics Writer — Case studies using Data Science for Supply Chain Sustainability 🌳 and Productivity: https://bit.ly/supply-chain-cheat