- Importing Libraries: We start by importing a library called
speech_recognitionand giving it the aliassr. This library helps us work with speech recognition.
pythonpy codeimport speech_recognition as sr
- Initializing the Recognizer: Next, we create an object called
recognizerfrom theRecognizerclass. This object helps us recognize speech in audio files.
p orecognizer = sr.Recognizer()
- Specifying the Audio File: We tell the program where to find the audio file that we want to convert to text. Here, it's named "audio_file.wav".
pytdeaudio_file_path = "audio_file.wav"- Opening and Recording Audio: We open the audio file using
sr.AudioFileand record the audio data from it usingrecognizer.record(source).
pytpy codewith sr.AudioFile(audio_file_path) as source:
audio_data = recognizer.record(source)
- Converting Audio to Text: We use the
recognize_sphinxmethod of therecognizerobject to convert the audio data into text.
pythoopy codetext = recognizer.recognize_sphinx(audio_data)
- Saving Recognized Text to File: We open a text file named "recognized_text.txt" in write mode and write the recognized text into it. We also print a message confirming that the text has been saved.
pythoCopy codewith open("recognized_text.txt", "w") as file:
file.write(text)
print("Text saved to 'recognized_text.txt'")pip install SpeechRecognition
pip install pocketsphinx
This code uses SpeechRecognition with CMU Sphinx to convert audio to text offline and saves the result to a text file.
# 1. How To Convert Vedio To Audio In Formate Of (wav) .
import speech_recognition as sr
import moviepy.editor as mp
clip = mp.AudioFileClip(r"C:\Users\Taj\Desktop\Manan Python Proggram\motivation.mp4")
clip.write_audiofile("converted.mp3.wav")
# 2. How To Convert Audio To Text
recognizer = sr.Recognizer()
audio_file_path = "converted.mp3.wav"
with sr.AudioFile("converted.mp3.wav") as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_sphinx(audio_data)
with open("recognized_text.txt", "w") as file:
file.write(text)
print("Text saved to 'recognized_text.txt'")
No comments:
Post a Comment