How to extract Speech from Video using Python?

Author: neptune | 16th-Jun-2023
#Python #Projects

In this article, we will delve into the process of extracting speeches from videos using the Google Speech Recognition API. We will then convert the extracted speeches into a text file. This task involves leveraging the Google Speech Recognition library, which is an effective tool for machine learning-based speech recognition. Speech recognition technology finds wide applications in various fields, including the generation of subtitles for popular platforms such as Amazon Prime, Netflix, and YouTube. Let's explore the detailed steps below to accomplish this task.


Step 1: Understanding the Google Speech Recognition API

Before we proceed with the implementation, let's gain a comprehensive understanding of the Google Speech Recognition API. This API provides us with the capability to perform speech recognition tasks using Google's powerful speech recognition technology. By leveraging this API, we can harness the accuracy and reliability of Google's speech recognition algorithms.


Step 2: Video to Audio Conversion

The initial step involves converting the video file into an audio file. To achieve this, we will utilize the MoviePy library, a versatile tool for video editing and processing in Python. Let's begin by importing the necessary libraries.



  import moviepy.editor as mp




Next, we need to specify the video file we want to convert and the specific portion of the video we are interested in. For instance, let's assume we want to clip the video from the 10th second to the 100th second.



# It will clip the video

# subclip(starttime, endtime) to clip a portion of the video

# you can remove the subclip to convert the complete video

clip = mp.VideoFileClip(r"sample1.mp4").subclip(10, 100)




If your video file is large and you want to process only a specific portion, using the `subclip()` function allows you to specify the start and end times to clip the desired segment. However, if you wish to convert the entire video, you can remove the `subclip()` function.


Finally, we will save the extracted audio as a WAV file.


clip.audio.write_audiofile(r "Converted_audio.wav")

print("Conversion to audio finished.")



Step 3: Audio to Text Conversion

Now that we have the audio file, we can proceed to convert it into text using the SpeechRecognition library. This library provides a convenient interface to perform speech recognition tasks. Let's import the necessary library.



import speech_recognition as sr




Next, we need to read the audio file.



audio = sr.AudioFile("Converted_audio.wav")

print("Audio file read.")




We will now utilize the "recognize_google" API from the SpeechRecognition library to perform the speech recognition.



r = sr.Recognizer()


with audio as source:

    audio_file = r.record(source)


result = r.recognize_google(audio_file)




Finally, we will store the recognized text in a file named "recognized.txt".



with open('recognized.txt', mode='w') as file:

    file.write(result)


print("Speech recognition completed.")




Final Step: Enjoy Your Day

Congratulations! You have successfully extracted the speech from the video and converted it into text. Feel free to further explore and enhance the functionality of this project to suit your requirements.


By engaging in hands-on programming projects like this, you can significantly improve your coding skills and gain valuable experience. You can find the complete code for this project on GitHub here


If you have any questions or would like to share your thoughts, please don't hesitate to reach out in the comments section below. Your feedback and contributions are greatly appreciated!





anonymous | July 17, 2022, 5:48 p.m.

It works๐Ÿ‘


anonymous | May 16, 2022, 9:21 p.m.

Helped me


anonymous | Sept. 20, 2021, 11:19 p.m.

๐Ÿ‘


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

Explained in a Simple way Add more articles about JavaScript.



Related Blogs
How to download video from youtube using python module ?
Author: neptune | 15th-Jun-2023
#Python
We will let you know how you can easily download the Youtube high quality videos along with subtitle, thumbnail, description using python package..

5 Selenium Project Ideas & for Beginners in Automation Testing
Author: neptune | 30th-Mar-2023
#Selenium #Testing #Projects
In this article, we will discuss 5 interesting Selenium project ideas for beginners in automation testing...

Best Python package manager and package for virtual environment ?
Author: neptune | 18th-Jun-2023
#Python #Pip
We will explore the options of Pip, Virtualenv, Anaconda, and also introduce Pyenv as a helpful tool...

Deploy Django project on AWS with Apache2 and mod_wsgi module.
Author: neptune | 25th-May-2023
#Python #Django
In this blog I use the AWS Ubuntu 18.22 instance as Hosting platform and used Apache2 server with mod_wsgi for configurations. We create a django sample project then configure server...

Core Python Syllabus for Interviews
Author: neptune | 26th-Jul-2023
#Python #Interview
STRING MANIPULATION : Introduction to Python String, Accessing Individual Elements, String Operators, String Slices, String Functions and Methods...

Mostly asked Python Interview Questions - 2023.
Author: neptune | 30th-May-2023
#Python #Interview
Python interview questions for freshers. These questions asked in 2022 Python interviews...

How to reverse string in Python ?
Author: neptune | 16th-May-2022
#Python
We are going to explore different ways to reverse string in Python...

Python Built-in functions lambda, map, filter, reduce.
Author: neptune | 15th-Jun-2023
#Python
We are going to explore in deep some important Python build-in functions lambda, map, filter and reduce with examples...

Python 3.9 new amazing features ?
Author: neptune | 26th-Jul-2023
#Python
Python 3.9 introduces new features such as dictionary union, string methods to remove prefixes and suffixes, type hinting, and speed improvements for built-in functions...

5 Languages that Replace Python with Proof
Author: neptune | 13th-Apr-2023
#Python
Julia, Rust, Go, Kotlin, and TypeScript are modern languages that could replace Python for specific use cases...

10 Proven Ways to Earn Money Through Python
Author: neptune | 11th-Apr-2023
#Python
Python offers numerous earning opportunities from web development to teaching, data analysis, machine learning, automation, web scraping, and more...

5 Best Python Testing Frameworks.
Author: neptune | 12th-Apr-2023
#Python #Testing
Python offers various testing frameworks, including Pytest, unittest, Nose, Robot Framework, and Behave, to build robust and reliable software...

Input and Output in Python
Author: neptune | 13th-Jul-2023
#Python
In this article, we will see how Python take input from user and How it display the output to user. First we cover input then output...

Monkey Patching in Python: A Powerful Yet Controversial Technique
Author: neptune | 01st-Aug-2023
#Python
Monkey patching in Python is a dynamic technique to modify code at runtime. It can add/alter behavior, but use it judiciously to avoid maintainability issues...

GoodbyeX: A Step-by-Step Guide to Remove the "X" Branding from Twitter
Author: neptune | 26th-Jul-2023
#Github #Projects
Twitter has been known for its continuous updates and changes to its user interface. One such change was the introduction of the "X" branding, which might not be appreciated by all users...

View More