Extracting Audio from Video using Python
In this post, I will show you how to extract audio from a video recording file. After extracting the audio, you can use it in different kinds of projects. It is a great skill to add to your python programming skillset. To give you some idea, here is an example: in a previous post, I showed how to extract speeches from those audio files using Google Speech Recognition.
Table of Contents:
- Getting Started
- Step 1: Import MoviePy
- Step 2: Define the Video Clip
- Step 3: Extracting the Audio
- Conclusion
Getting Started
This will a short and simple project. It will not take us more than five minutes to finish the whole project. Before moving to the programming part, I would like to give some information about the library we will use in this project: MoviePy. Let’s get started!
As you can understand from the title, we will need a video recording for this project. It can even be a short recording of yourself speaking to the camera. Using a library called MoviePy, we will extract the audio from the video recording. If you are ready, let’s start by installing the libraries!
Installing a module library is very straightforward in python. You can even install a couple of libraries in one line of code. Write the following line in your terminal window:
pip install ffmpeg moviepy
Ffmpeg is a leading multimedia framework, that decodes, encodes, transcodes, mux, demux, stream, filter and play pretty much anything that humans and machines have created. (Reference: http://ffmpeg.org/about.html)
MoviePy is a library that can read and write all the most common audio and video formats, including GIFs. In case you are having issues when installing moviepy library, make sure the ffmpeg library is installed correctly.
Now, let’s get to programming. You can use a text editor or Jupyter Notebook. We will start by importing the libraries.
Step 1 — Import MoviePy
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 the Video Clip
Hoping that you already have the video clip ready. Let’s copy the file inside our project folder; this will make it easier to define the video file. MoviePy has a method called VideoFileClip, that will do the whole thing for us:
my_clip = mp.VideoFileClip(r"videotest.mov")
Inside the method, we are adding the path of the video. And r stands for read.
My recording is in mov format, I will share more about the formats in the next step. It will be helpful when we are exporting the audio file.
Step 3 — Extracting the Audio
This is the final step. We will extract the audio of the video that we defined. Before we move to the final line, let’s learn about the formats. There are many video formats; some of them can be listed as:
- WMV (wmv, wma, asf*)
- OGG (ogg, oga, ogv, ogx)
- 3GP (3gp, 3gp2, 3g2, 3gpp, 3gpp2)
- MP4 (mp4, m4a, m4v, f4v, f4a, m4b, m4r, f4b, mov)
We should know our video’s format to do the conversion without any problem. Besides the video format, it’s also a good practice to know some audio formats. Here are some of them:
- MP3
- AAC
- WMA
- AC3 (Dolby Digital)
Now, we have some ideas about both formats. It’s time to do the conversion using the MoviePy library. I will convert it to MP3 format, it is a widespread one. Depending on your use case, feel free to change the format in this line. As an example for speech recognition the wav format works better. Here is the final line of our project:
my_clip.audio.write_audiofile(r"my_result.mp3")
Conclusion
Perfect!! You have created a simple project that extracts the audio of a video without using any software. We also learned about video and audio formats. As I mentioned earlier, you can use these audio files in different kinds of projects. Speech recognition is a great place to get started.
Hoping that you enjoyed reading this post and working on the project. I am 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.
Comments