Extracting Speech from Video using Python | by Behic Guven | Towards Data Science

In this post, I will show you how to extract speeches from a video recording file. After recognizing the speeches we will convert them into a text document. This will be a simple machine learning project, that will help you to understand some basics of the Google Speech Recognition library. Speech Recognition is a popular topic under machine learning concepts. Speech Recognition is getting used more in many fields. For example, the subtitles that we see on Netflix shows or YouTube videos are created mostly by machines using Artificial Intelligence. Other great examples of speech recognizers are personal voice assistants such as Google’s Home Mini, Amazon Alexa, Apple’s Siri.
Getting Started
Step 1: Import Libraries
Step 2: Video to Audio Conversion
Step 3: Speech Recognition
Final Step: Exporting Result
As you can understand from the title, we will need a video recording for this project. It can even be a recording of yourself speaking to the camera. Using a library called MoviePy, we will extract the audio from the video recording. And in the next step, we will convert that audio file into text using Google’s speech recognition library. If you are ready, let’s get started by installing the libraries!
We are going to use two libraries for this project:
Speech Recognition
MoviePy
Before importing them to our project file, we have to install them. Installing a module library is very easy 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 SpeechRecognition moviepy
Yes, that was it. SpeechRecognition module supports multiple recognition APIs, and Google Speech API is one of them. You can learn more about the module from here.
MoviePy is a library that can read and write all the most common audio and video formats, including GIF. If you are having issues when installing moviepy library, try by installing ffmpeg. Ffmpeg is a leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created.
Now, we should get to writing code in our code editor. We will start by importing the libraries.
import speech_recognition as sr 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.
In this step, we will something really cool, which is converting our video recording into an audio file. There are many video formats, some of them can be listed as:
MP4 (mp4, m4a, m4v, f4v, f4a, m4b, m4r, f4b, mov)
3GP (3gp, 3gp2, 3g2, 3gpp, 3gpp2)
OGG (ogg, oga, ogv, ogx)
WMV (wmv, wma, asf*)
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 idea about both formats. It’s time to do the conversion using MoviePy library. You will not believe how easy it is.
clip = mp.VideoFileClip(r”video_recording.mov”)  clip.audio.write_audiofile(r”converted.wav”)
I recommend converting it to wav format. It works great with the speech recognition library, which will be covered in the next step.
towardsdatascience.com
First, let’s define the recognizer.
r = sr.Recognizer()
Now let’s import the audio file that was created in the previous step (Step 2).
audio = sr.AudioFile("converted.wav")
Perfect! Here comes the best part, which is recognizing the speech in an audio file. The recognizer will try to understand the speech and convert it to a text format.
with audio as source:  audio_file = r.record(source)result = r.recognize_google(audio_file)
Well done! The hard work is completed. In this step, we will just export the recognized speech into a text document. This will help you to store your work. I’ve also added a print(“ready!”) at the end of the code. So that we know when the file is ready and the work is completed.
# exporting the result with open('recognized.txt',mode ='w') as file:    file.write("Recognized Speech:")    file.write("\n")    file.write(result)    print("ready!")
Just started my journey on YouTube, I will be demonstrating Machine Learning, Data Science, Artificial Intelligence and more projects for you. Enjoy!
lifexplorer.medium.com
Congrats! You have created a program that converts a video into an audio file and then extracts the speech from that audio. And lastly, exporting the recognized speech into a text document. 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.
Feel free to contact me if you have any questions while implementing the code.
Follow my blog and Towards Data Science to stay inspired.