Created
November 9, 2023 13:14
-
-
Save spinningcat/2cbcc5baf10d65880c23ade8e33a89c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import speech_recognition as sr | |
import sounddevice as sd | |
import wave | |
def record_and_transcribe(): | |
duration =10 # Recording duration in seconds | |
sample_rate = 44100 # Standard audio sample rate | |
channels = 2 # Mono audio (change to 2 for stereo) | |
# Record audio using sounddevice | |
print("Recording...") | |
audio_data = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=channels) | |
sd.wait() # Wait for recording to finish | |
# Save the audio as a temporary WAV file | |
with wave.open("temp_audio.wav", "wb") as wf: | |
wf.setnchannels(channels) | |
wf.setsampwidth(4) # 16-bit audio | |
wf.setframerate(sample_rate) | |
wf.writeframes(audio_data.tobytes()) | |
# Initialize the recognizer | |
recognizer = sr.Recognizer() | |
try: | |
with sr.AudioFile("temp_audio.wav") as source: | |
audio = recognizer.record(source) # Record audio from the source | |
text = recognizer.recognize_google(audio) | |
print("Transcribed text: ", text) | |
# Save the transcribed text to a file | |
with open("transcribed_text.txt", "w") as file: | |
file.write(text) | |
print("Transcribed text saved to 'transcribed_text.txt'") | |
except sr.UnknownValueError: | |
print("Speech recognition could not understand audio.") | |
except sr.RequestError as e: | |
print(f"Could not request results from Google Web Speech API; {e}") | |
if __name__ == "__main__": | |
record_and_transcribe() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment