Connect with us

Others

Extract speech text from video in Python

Published

on

Extract speech text from video in Python
Share our content or if you have any questions about an article, leave us a comment.

In this tutorial, we are going to see how to Extract speech text from a video in Python. We are going to extract the audio content from the video clip. And then we will convert that audio into text. It’s easy and simple let’s see how it works.

For this, we are going to use libraries available in Python. Here we are using Speech Recognition and MoviePy library.

MoviePy is a Python library used for video editing: cutting, concatenations, title insertions, video compositing, video processing, and creation of custom effects.

Whereas Speech Recognition library is used for performing speech recognition, with support for several engines and APIs, online and offline.

Before proceeding towards our task we need to install these libraries to our system. We can do this using the pip command as shown below in your terminal or shell.

pip install SpeechRecognition moviepy

Yes, that’s it! It’s pretty much simple. After executing the above command libraries will be installed in your machine.  SpeechRecognition module supports multiple recognition APIs.

We are going to use Google Speech API from it.
In the above command, MoviePy is also included.

In the next step let’s import these libraries into our Python code. The full code is given below.

# Step 1 : Importing libararies
import speech_recognition as sr 
import moviepy.editor as mp

# Step 2: Video to Audio conversion

VidClip = mp.VideoFileClip("https://cdn.codespeedy.com/content/data/video.mp4") 
VidClip.audio.write_audiofile("https://cdn.codespeedy.com/content/data/converted.wav")

# Step 3: Speech recognition

reco = sr.Recognizer()
audio = sr.AudioFile("https://cdn.codespeedy.com/content/data/converted.wav")
with audio as source:
  audio_file = reco.record(source)
result = reco.recognize_google(audio_file)

# Step 4: Finally exporting the result 

with open('https://cdn.codespeedy.com/content/data/SpeechText.txt',mode ='w') as file: 
   file.write("Recognized Speech Text:") 
   file.write("\n") 
   file.write(result) 
   print("Text file ready!")
 

We are going to divide our task into 4 steps. As you can see in the above code.

Step 1 :
As mentioned above we imported libraries.

Step 2 :
Here we are going to convert Video file to Audio using MoviePy. First, we have declared the VidClip variable. Providing it our video file with path/location. By using audio.write_audiofile function we are converting .mp4 file to audio .wav file.

For Video You can use any format file such as mp4, m4a, m4v, 3GP, OGG, WMV, etc. And for Audio you can use wav, mp3, AAC, WMA, AC3, etc. As output here we are getting audio file converted.wav.

Step 3 :
In this step, our main task is Speech Recognition. First, let’s define the recognizer. As shown in code we have defined ‘reco’ as a recognizer. Next, we are giving the audio file we obtained in step 2 to the library Speech Recognition as input.

The recognizer will try to understand the speech in that file and convert it to a text format. We are using Google’s speech recognition library for this task. Here result will be stored in the ‘result’ variable.

Step 4:

Finally, It’s time to export our result to the actual text file. By using file handling in Python we are simply writing to our text file in ‘w’ mode is write mode using ‘file. write’ function. As output ‘SpeechText.txt’ file will be saved in the given directory. In the end “Text file ready!” will be printed so that we can know the task is completed.

Output :

Text file ready!

So, in this tutorial, we have successfully able to extract speech text from a video with the help of Python programming.

Disclaimer: We sell any type of script/themes/plugins, we also offer web design/developement, graphics design, SEO, Social media followers, Admob self-clicking. visit our contact page to get in touch with us or WhatsApp (+234) 08114313795.

Ikuesan Oluwaseun is the CEO of Ofofonobs and a Full-stack Developer. Ofofonobs.com is a Nigerian digital media and news platform launched in July 2020. Kindly check our contact page to get in touch with me.

Advertisement
Click to comment

Leave a Reply

Your email address will not be published.

Entertainment

Blake Snell Net Worth Blake Snell Net Worth
Entertainment2 months ago

Blake Snell Net Worth

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItWhat is Blake...

Beenie Man Net Worth Beenie Man Net Worth
Entertainment2 months ago

Beenie Man Net Worth

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItWhat is Beenie...

How to load an already used recharge card How to load an already used recharge card
Entertainment3 months ago

See how to load an already used recharge card (Topsecrets)

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItHave you ever...

Nigerians on Twitter find humour in Oxlade's leaked s*x tape Nigerians on Twitter find humour in Oxlade's leaked s*x tape
Entertainment3 months ago

Nigerians on Twitter find humour in Oxlade’s leaked s*x tape

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItFull video of...

Oxlade S*x Tape With Unidentified Lady Breaks Internet Oxlade S*x Tape With Unidentified Lady Breaks Internet
Breaking News3 months ago

Oxlade S*x Tape With Unidentified Lady Breaks Internet

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItFull video of...

Oba Solomon Agbaye Biography, Career, Wife, Net Worth Oba Solomon Agbaye Biography, Career, Wife, Net Worth
Entertainment3 months ago

Oba Solomon Agbaye Biography, Career, Wife, Net Worth

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItAdelana Adekunle Solomon,...

Nigerians react to Obi Cubana’s first son, Alex’s grades in school Nigerians react to Obi Cubana’s first son, Alex’s grades in school
Entertainment4 months ago

Nigerians react to Obi Cubana’s first son, Alex’s grades in school

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItObi Cubana’s first...

Nigerians worry over size of Bobrisky’s head in new video Nigerians worry over size of Bobrisky’s head in new video
Entertainment4 months ago

Nigerians worry over size of Bobrisky’s head in new video

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItControversial crossdresser, Bobrisky has sparked...

Man Arrested For Advertising Fake Eggs On Social Media Man Arrested For Advertising Fake Eggs On Social Media
Entertainment4 months ago

Man Arrested For Advertising Fake Eggs On Social Media

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItA 24-year-old man,...

Scream Director Teases Sam’s Mom’s Identity Scream Director Teases Sam’s Mom’s Identity
Entertainment4 months ago

Scream Director Teases Sam’s Mom’s Identity

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItIn a recent...

ADVERTISEMENT