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.
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

Entertainment3 weeks ago

Complete Guide: Hotel Jobs in Canada with Visa Sponsorship

Hotel Jobs in Canada with Visa Sponsorship   Welcome to a world of exciting opportunities in the heart of hospitality!...

Entertainment1 month ago

Truck Drivers Jobs In Canada With Visa Sponsorship

Truck Drivers Jobs In Canada With Visa Sponsorship   This is a great opportunity for aspiring truck drivers! Explore the...

Entertainment1 month ago

American Visa Lottery Sponsorship Program: What You Need To Know

American Visa Lottery Sponsorship Program: What You Need To Know   Welcome to a comprehensive guide on the American Visa...

Entertainment1 month ago

Cleaning Jobs in USA with Visa Sponsorship – APPLY NOW

Cleaning Jobs in USA with Visa Sponsorship – APPLY NOW   Check out cleaning jobs in the USA with visa...

Entertainment1 month ago

U.S. Visa Sponsorship Opportunities in 2024/2025 – Apply Now

U.S. Visa Sponsorship Opportunities in 2024/2025 – Apply Now   Explore career opportunities in the U.S. with evolving international talent...

Entertainment1 month ago

Fully Funded University of Miami Stamps Scholarship 2024/2025

If you are looking for scholarships, then you should check out an exciting opportunity for aspiring scholars! The University of...

Entertainment1 month ago

Mechanic Jobs in the USA with Visa Sponsorship – APPLY NOW

Mechanic Jobs in the USA with Visa Sponsorship – APPLY NOW   Welcome to a world of opportunity for mechanics...

Entertainment1 month ago

Graphic Designer Jobs in USA with Visa Sponsorship – APPLY NOW

Explore exciting opportunities in the field of graphic design with visa sponsorship in the USA. Dive into a world of...

Entertainment1 month ago

Accounting Jobs in the USA with Visa Sponsorship – APPLY NOW

Welcome to an unparalleled opportunity for individuals aspiring to build a rewarding career in accounting in the United States with...

Entertainment1 month ago

Bartender Jobs in the USA with Visa Sponsorship – APPLY NOW

Bartender Jobs in the USA with Visa Sponsorship – APPLY NOW   Welcome to an extraordinary opportunity for those with...