Connect with us

Others

Extract speech text from video in Python

Published

on

Extract speech text from video in Python

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. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Entertainment

Hyundai Veloster N TCR Makes World Debut in Detroit Hyundai Veloster N TCR Makes World Debut in Detroit
Entertainment5 months ago

Hyundai Veloster N TCR Makes World Debut in Detroit

Just before it sets of to conquer the race tracks of the 2019 International Motor Sports Association Michelin Pilot Challenge series,...

Reconditioned 1974 Honda CB750 Looks as If It Came Straight Out of a Museum Reconditioned 1974 Honda CB750 Looks as If It Came Straight Out of a Museum
Entertainment5 months ago

Reconditioned 1974 Honda CB750 Looks as If It Came Straight Out of a Museum

You might find it hard to believe, but this 1974 Honda CB750 was nothing more than a non-running project a few years...

Beautiful Music for Successful Wedding Videos Beautiful Music for Successful Wedding Videos
Entertainment5 months ago

Beautiful Music for Successful Wedding Videos

When it comes to wedding videos, ensuring you have the right music at hand can make all the difference. Any...

Why it’s better to have a child with someone you don’t love – Yvonne Nelson spills Why it’s better to have a child with someone you don’t love – Yvonne Nelson spills
Entertainment6 months ago

Why it’s better to have a child with someone you don’t love – Yvonne Nelson spills

Popular Ghanaian actress, Yvonne Nelson has revealed why it’s better and advisable for people to have children with people they are not...

Wizkid’s Made In Lagos Hits Half A Billion Streams On Apple Music Wizkid’s Made In Lagos Hits Half A Billion Streams On Apple Music
Entertainment6 months ago

Wizkid’s Made In Lagos Hits Half A Billion Streams On Apple Music

Nigerian superstar, Wizkid has recorded another feat with his Made In Lagos Deluxe album version with a milestone of 500M...

Blake Snell Net Worth Blake Snell Net Worth
Entertainment8 months ago

Blake Snell Net Worth

What is Blake Snell’s net worth? Net Worth: $10 Million Age: 28 Born: December 4, 1992 Country of Origin: United...

Beenie Man Net Worth Beenie Man Net Worth
Entertainment8 months ago

Beenie Man Net Worth

What is Beenie Man’s net worth? Net Worth: $4 Million Age: 47 Born: August 22, 1973 Country of Origin: Jamaica...

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

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

Have you ever gotten free Airtime Credit from MTN? If you haven’t, you’re still dulling yourself. This article will teach...

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

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

Full video of a fast-rising singer identified as Oxlade smashing an unidentified lady breaks the internet, Brand Spur Nigeria reports. In the tape...

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

Oxlade S*x Tape With Unidentified Lady Breaks Internet

Full video of a fast-rising singer identified as Oxlade smashing an unidentified lady breaks the internet, Brand Spur Nigeria reports. The 24-year-old whose...

ADVERTISEMENT