From ddc3e0df6c47a22207ef6579f50b984a13d70a79 Mon Sep 17 00:00:00 2001 From: Mrudhulraj <51220832+Mrudhulraj@users.noreply.github.com> Date: Sat, 29 Aug 2020 08:58:55 +0530 Subject: [PATCH] Create mp4&3_transcription.py Transcription for any MP3 and MP4 file added --- examples/mp4&3_transcription.py | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/mp4&3_transcription.py diff --git a/examples/mp4&3_transcription.py b/examples/mp4&3_transcription.py new file mode 100644 index 00000000..963d5432 --- /dev/null +++ b/examples/mp4&3_transcription.py @@ -0,0 +1,41 @@ +import moviepy.editor +import speech_recognition as sr +from pydub import silence, AudioSegment +from pathlib import Path +import os + +# mp4---> wav file +video=moviepy.editor.VideoFileClip('.mp4') +audio=video.audio +audio.write_audiofile('.wav') #creates a wav file in CWD + +#mp3-->wav file +my_file = Path('') # MP-3 file location +audio = AudioSegment.from_mp3(my_file) +audio.export(".wav",format="wav") #Creates a processable wav file in CWD + +#Code for recognizing mp3 or mp4 file +r = sr.Recognizer() + +with sr.AudioFile('.wav') as source: #Mention the Path of file to be read + audio = r.listen(source) + +#recognize using google_api(Google Speech Recognition) +try: + # for testing purposes, we're just using the default API key + # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")` + # instead of `r.recognize_google(audio)` + print("Google Speech Recognition thinks you said " + r.recognize_google(audio)) +except sr.UnknownValueError: + print("Google Speech Recognition could not understand audio") +except sr.RequestError as e: + print("Could not request results from Google Speech Recognition service; {0}".format(e)) + + # recognize speech using Microsoft Azure Speech +AZURE_SPEECH_KEY = "INSERT AZURE SPEECH API KEY HERE" # Microsoft Speech API keys 32-character lowercase hexadecimal strings +try: + print("Microsoft Azure Speech thinks you said " + r.recognize_azure(audio, key=AZURE_SPEECH_KEY)) +except sr.UnknownValueError: + print("Microsoft Azure Speech could not understand audio") +except sr.RequestError as e: + print("Could not request results from Microsoft Azure Speech service; {0}".format(e))