In my last few articles, I have been learning speech recognition by using an API. For those, I built a to-do list creator as my project. However, for this article, I wanted to revisit the speech recognition API I have been using, but with a new project. And what better way than to integrate it with an older project? The project I am referring to was when I used Python to edit videos, which you can read more about here. In the spirit of editing videos, I wondered if there was a way to create chapters through code. And that’s exactly what we will be doing today.
Similar to my previous articles, I will be using AssemblyAI’s API for speech recognition to automatically create chapters in a video file. I will also be able to recycle code from my Python video editing article. If you haven’t read that yet, I’ll briefly cover the code here as well. But enough about the background, let’s get straight into the code.
If you have been reading my recent speech recognition articles, you’ll notice that there are very few differences and you won’t have much to add. However, if you haven’t read them yet, I will still list the functions needed here, just with not as much explanation as I’ve written about them before. This will be the first file you will need, which creates a class for the API calls. This class will be used in the video editing file, which we will create here in a few minutes.
Recall, the first steps I took in my other articles were to declare my imports, create the class, and write the initializer. The difference in code is when declaring the JSON object in submit_audio, I included the auto_chapters. Otherwise, the other functions remain the same, so I will list those all first. You may also need to install the requests library, which I will not include, as I have written about the process in my previous articles.
Now for the last function to submit the audio. Again, the change was fairly simple. Essentially, I just added auto_chapters to the JSON being passed so they would be included on the return.
With our class built, it’s time to edit, or create, the video editing file.
To start, we will be using a few different libraries: pathlib, moviepy, and time. Again, because I’ve written on these before, I’m not going to include the steps to install for now, but they are in my python video editing article should you want a step-by-step on downloading the required packages.
This file is titled editor.py. Let’s take a look at the imports I needed for this project:
The next thing I did was declare my editor class and initializer. Because I’ve already gone over this code, I will also include my functions to speed_up the video. If you want more in-depth descriptions of how those work, please refer to my python video editing article that was linked in the intro.
From here, I needed to create the chapters for the video. Essentially, this will create a new instance of the speech_to_text class, upload a video file to AssemblyAI and then submit that file to be processed. I also included a little error handling in case there was an issue:
With the video editing file finished, we’re ready to create the file to test the auto_chapters.
Not too much code is required for the test file. Essentially, I had to import the video editing file, set a variable to initialize the class, and finally call the create_chapters function with a path to save the results. I accomplished this with only a few lines of code:
With the code complete, now just to do a little testing:
Today, I leveraged AssemblyAI’s API to add some new features to the Python video editor project I’ve been working on. In a nutshell, the new function eases the tedious task of creating video chapters by automatically doing it for me. To work on this project, I was able to reuse code from a few other projects with only a few minor changes. Especially with simply adding “auto_chapters” to the JSON object being sent to the API, I found this very easy to learn and replicate for other projects as well. Overall, I found this project interesting and easy to follow. Hopefully, you found this useful and interesting as well. Until next time, cheers!
References:
https://www.assemblyai.com/blog/introducing-assemblyai-auto-chapters-summarize-audio-and-video-files/
https://zulko.github.io/moviepy/