Wednesday, 24 April 2024

CONVERT AUDIO TO TEXT

 

  1. Importing Libraries: We start by importing a library called speech_recognition and giving it the alias sr. This library helps us work with speech recognition.
pythonpy code
import speech_recognition as sr
  1. Initializing the Recognizer: Next, we create an object called recognizer from the Recognizer class. This object helps us recognize speech in audio files.
p orecognizer = sr.Recognizer()
  1. 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"
  1. Opening and Recording Audio: We open the audio file using sr.AudioFile and record the audio data from it using recognizer.record(source).
pytpy code
with sr.AudioFile(audio_file_path) as source: audio_data = recognizer.record(source)
  1. Converting Audio to Text: We use the recognize_sphinx method of the recognizer object to convert the audio data into text.
pythoopy code
text = recognizer.recognize_sphinx(audio_data)
  1. 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 code
with 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

MAKE A CALANDER

  # 1.  convert calander year and month. your choice month or year , print while True :   import calendar   year = int ( input ( "P...