How I Automated Video Editing Using FFmpeg | theapache64
Skip to content

How I Automated Video Editing Using FFmpeg

Updated: at 06:30 PM

Note: This blog was originally written in 2020.

💭 The Thought

While watching YouTube videos, I would often dream about becoming a YouTuber, as I’m sure many of us do 😄. I feel like I have a wealth of knowledge I’d like to share with the world. Small tutorial videos, talking about how I develop my hobby projects, and providing developers some productivity tips. Just something, anything, even if on a small scale. But to do these things, 🙄 I’d have to:

I know, I know. All these things make me sound like a really lazy person. That’s because I am! 😁 So what do I do?

🌩️ Moment of Inspiration

I know some silly programming. Would I be able to automate this process?

I asked myself, and that was it. What did I have to lose? 🤷‍♂️ So I decided to try and automate this process.

🕵️ Research

I’ve heard that FFmpeg is a good tool to process video frames. So I quickly browsed through the docs and found that it’s pretty straightforward even for a simpleton like me.

🤖 Core Feature

Let’s say we’ve an input video of 10 minutes, and if it has 1 minute of non-talking area (the area where no one is talking) after every 2 minutes, it’d look something like this:

input

I want to timelapse those red areas to 15 seconds of footage. So the total video length becomes 7 minutes and 45 seconds.

Also, while running the timelapse, I want to replace the footage’s raw audio with some cool BGM 🥁.

So this was the core feature I wanted to implement in this thing, for which I had not decided a name at that point.

⌨️ Initial Development

I created a shell script to process a sample video I downloaded from YouTube. I wrote some basic stuff in it, like:

It took me 3 days to write it all by hand. I just wanted to check if FFmpeg has the potential to do what I want it to, and it really impressed me, going beyond my expectations.

The next thing I wanted to do was generate these FFmpeg commands dynamically. 💣

🧑‍💻 Code Improvements

It took me around 5 days to code the entire program. Then I proceeded to make some code improvements and made all possible input variables dynamic, which means you can control pretty much anything in it: timelapse speed, intro title, watermark title, minimum timelapse duration, you name it.

Here’s the overview of the core stack:

At this stage, I also settled on a name for this project, Auto-Motion.

Now, let’s run it! 🏃

🕹 Input

Input is a 6 minutes video from Comma Archive, captured by George Hotz in a recent hackathon they conducted.

🚀 Execution

It only took ~1 minute to edit and render the output, whereas a manual edit would have taken hours, perhaps even days, to accomplish it. 🤷

📺 Output

The output, reduced to 4 minutes 43 seconds, is here. It includes 7 time-lapses of different lengths.

I know the output is not as enjoyable as a manually edited one, but I believe I can improve this in due time 😇

📩 Download

You can view the source code and download the program from GitHub. Please note that this was originally written in 2020, and the testing was mostly done on Ubuntu. If you want to make this work in macOS or Windows, you probably have to tweak something. If there’s enough interest from the community in this project, I’ll make it compatible with other OSes too ;)

📖 Thanks for reading!

I’m really happy I wrote this and I’m excited to share more with you all. If you liked the post, do share it with your friends. Feel free to post your suggestions for future posts! Thank you for reading this. Ciao!