Share

How to extract Speech from Video using Python?

Author: neptune | 25th-May-2021 | views: 28
#Python

Simple and easy way to convert video into audio then extract text using Google Speech Recognition API.

In this article, We will extract speeches using google api from videos. After extracting the speeches we will convert them into a text file. It is going to be a simple machine learning task  using google speech recognition library. Speech Recognition is widely used nowadays under machine learning concepts. Speech Recognition is also used in many fields.

For example, the subtitles that we see on Amazon prime, Netflix, and YouTube videos are best examples of using Artificial Intelligence in Speech Recognition.

Steps to follow:-

  • Step 1: Little Understanding about API
  • Step 2: Video to Audio Conversion
  • Step 3: Audio to Text Conversion
  • Final Step: Enjoy your day

Libraries used for conversion:-

We are going to use two libraries for this conversion task.

  • Speech Recognition
  • MoviePy

Before we start, let's install them if you haven't installed them yet. Installing a module library is very easy in python. We can even install libraries in one or two lines of code.

Run the following cmd in your terminal window:-

pip install SpeechRecognition moviepy

SpeechRecognition module supports multiple APIs for recognition, Google Speech API we are going to use here. You can learn more about the module from here.

MoviePy is a module library that can read and write almost each type of audio and video formats, including GIF’s.

Now, start the most important part i.e code. Open your editor and start by importing the libraries.


Video to Audio Conversion

Now, we will convert the video into an audio file. Lot’s of video formats are available, some of them MP4, 3GP, OGG, WMV etc. Let’s also take a look in some audio formats. Here are some of them MP3, AAC, WMA, AC3 (Dolby Digital) etc. We should know our video’s format to do the conversion without any problem.

Now, start the conversion using MoviePy library. It is going to be very easy. 

# Libraries import
import moviepy.editor as mp

# It will clip the video
# subclip(starttime, endtime) to clip portion of video
# you can remove the subclip to convert complete video
clip = mp.VideoFileClip(r"sample1.mp4").subclip(10, 100)

# It will write the audio in converted_audio.wav file.
clip.audio.write_audiofile(r"Converted_audio.wav")
print("Finished the convertion into audio...")

I recommend converting it to wav format. It works great with the speech recognition library, which will be covered in the next step.

 


Audio to text conversion

In this step, we will convert the audio into the text using “recognize_google” API. Finally we save the text file i.e. recognized.txt .

# Libraries import
import speech_recognition as sr

# It will read audio file
audio = sr.AudioFile("Converted_audio.wav")
print("Audio file readed...")

# Here the magic start
# create an instance of recognizer as r
r = sr.Recognizer()

with audio as source:
audio_file = r.record(source)

# Here we get our text
result = r.recognize_google(audio_file)

# Now we will store the text in file
with open('recognized.txt',mode ='w') as file:
file.write(result)

print("Wooh.. You did it...")

If you are getting error like broken pipe then reduce the duration of audio file or use “.subclip(starttime, endtime)” as shown in code. Still face any other error let me know in comment section.


We did it! We have finally got our text. We 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. 

Here is the Complete code:-

# Libraries import
import speech_recognition as sr
import moviepy.editor as mp

# It will clip the video
# subclip(starttime, endtime) to clip portion of video
# you can remove the subclip to convert complete video
clip = mp.VideoFileClip(r"sample1.mp4").subclip(10, 100)

# It will write the audio in converted_audio.wav file.
clip.audio.write_audiofile(r"Converted_audio.wav")
print("Finished the convertion into audio...")


# Now from here we convert audio into text

# It will read audio file
audio = sr.AudioFile("Converted_audio.wav")
print("Audio file readed...")

# Here the magic start
# create an instance of recognizer as r
r = sr.Recognizer()

with audio as source:
audio_file = r.record(source)

# Here we get our text
result = r.recognize_google(audio_file)

# Now we will store the text in file
with open('recognized.txt',mode ='w') as file:
file.write(result)

print("Wooh.. You did it...")
view rawComplete_code.py hosted with ❤ by GitHub

Hoping that you enjoyed reading this post and working on the project.

I hope you have learned something new today. Working on hands-on programming projects like this one is the best way to sharpen your coding skills.

Link to complete project on Github.

Thanks for reading!




anonymous | May 15, 2021, 10:28 a.m.

Explained in a Simple way Add more articles about JavaScript.



Related Blogs
Python 3.9 new amazing features ?
Author: neptune | 25th-May-2021 | views: 110
#Python
Some of the best new feature of Python 3.9 like Dict union, Type hinting with examples..
Best Python package manager and package for virtual environment ?
Author: neptune | 29th-May-2021 | views: 57
#Python #Anaconda #Virtualenv #Pip
Which is the best package manager for python and Virtual environment management using Virtualenv and Anaconda...
Will, AI kills developers and programmers job ?
Author: neptune | 05th-Jun-2021 | views: 47
#Python #Machine learning #AI
GPT-3’s performance has convinced that Artificial intelligence is closer or at least AI-generated code is closer than we think. It generates imaginative, insightful, deep, and even excellent content...
Python Built-in functions lambda, map, filter, reduce.
Author: neptune | 26th-May-2021 | views: 36
#Python
We are going to explore in deep some important Python build-in functions lambda,map,filter and reduce with examples..
Do you know Jupyter is now full-fledged IDE?
Author: neptune | 25th-May-2021 | views: 32
#Python #Jupyter
Jupyter is a widely used tool by Data scientists. So developers from institutions like Two Sigma, Bloomberg and fast.ai convert it into IDE lets see..
How to download video from youtube using python module ?
Author: neptune | 26th-May-2021 | views: 31
#Python
We will let you know how you can easily download the Youtube high quality videos along with subtitle, thumbnail, description using python package..
What exactly you can do with Python?
Author: neptune | 29th-May-2021 | views: 31
#Python
Well, it's a tricky question to answer because there are lots of application of Python. But, I will tell you about 3 main applications of Python...
View More