Connect with us
Padiwise Banner

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, VTU Platform and more. visit our contact page to get in touch with us or WhatsApp (+234) 08114313795. data
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

Entertainment1 month ago

John Okafor Mr. Ibu Biography: Net Worth, Family, Movies And More

John Okafor Mr. Ibu Biography & Net Worth ( Fully Updated ) John Okafor is more commonly known as Mr....

Entertainment1 month ago

Brely Evans Biography: Husband, Books, Age, Career

Brely Evans, who was born on December 9, 1972, in Oakland, California, and raised in Milwaukee, Wisconsin, is a multi-talented...

Entertainment1 month ago

Meet Gbadebo Rhodes Vivour, The Labour Party Governorship Candidate In Lagos State 

Gbadebo Rhodes Vivour profile Gbadebo Chinedu Rhodes-Vivour, popularly known as GRV, is a Nigerian architect, activist, and politician born on...

Entertainment1 month ago

Yinka Okeleye Biography: Age, State Of Origin, Education, Wife, Songs, Girlfriend

Adeyinka Okeleye is a talented Gospel singer and worship leader from Ogun State, Nigeria. He currently serves as the Music...

Entertainment1 month ago

Sunmisola Agbebi Biography: Age, Songs, Family, Boyfriend, Net worth, Education

Sunmisola Elizabeth Agbebi is a talented Nigerian gospel singer with a distinct and energetic style. She is 24 years old...

Entertainment1 month ago

Meet Beatrice Itubo, The LP Governorship Candidate In Rivers State

Beatrice Itubo, the Labour Party gubernatorial candidate for the 2023 governorship election in Rivers State was born on Sunday, June...

Entertainment1 month ago

Meet Somachi Chris Asoluka , The New CEO Of Tony Elumelu Foundation

Somachi Chris Asoluka was announced as the new Chief Executive Officer of Tony Elumelu Foundation on January 9, 2023. She...

Entertainment1 month ago

Tasha Cobbs Leonard Christmas Family Photos With Husband And Children

It’s December 26 and everyone especially married people is sharing their beautiful family photos with matching pajamas . Well, famous...

Entertainment1 month ago

10 THINGS YOU SHOULD KNOW ABOUT DEBORAH PAUL ENENCHE

Deborah Paul Enenche was born on 28 of March 1995 2. She is a Nigerian top-rated gospel singer and first...

Entertainment1 month ago

Lillian Mutambo Biography: Age, Investment , Marriage And More

Lillian Mutambo is a 39 year -old Zambian social worker and youtuber based in the United Kingdom. Music Career Lily...