Extract Voice from a Video and Save that in a file using Python (Speech Recognition)

Extract Voice from a Video and Save that in a file using Python (Speech Recognition)

In this video, We see how to extract speech text from video and save it in a file using Python. We use Python Packages and libraries that has trained by deep learning Speech Recognition models and has high accuracy. This is a Simple and Powerful code to Extract Speech from Video and do Speech Recognition on it.

Python Code of Video is :

import moviepy.editor as mp 
import speech_recognition as sr 

clip = mp.VideoFileClip("1.mp4")

clip.audio.write_audiofile("ExtractedAudio.wav")

r = sr.Recognizer()
audio = sr.AudioFile("ExtractedAudio.wav")

with audio as source:
    audio_file = r.record(source)

try :
    result = r.recognize_google(audio_data= audio_file)

    with open("result.txt", "w") as file :
        file.write(result)
        file.close()

    print("Runs Successfully")
    
except sr.UnknownValueError :
    print("Google Speech Recognition Engine Could not Understand Audio.")
except sr.RequestError as e:
    print("Could not Get response from Google, Error is {0}".format(e))
except Exception as e:
    print(e)

clip.close()
print("End")