Tom
Tom

Reputation: 71

Saving an audio file

I'm building a script that is using voice recognition. It works, but the recognizing part often gives me bad results. So I thought I record the quality and check if a bad quality could be the result of my results.

i installed using

sudo pip3 install SpeechRecognition
sudo pip3 install PyAudio

the script:

import speech_recognition as sr    
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Ich höre zu....")
    audio = r.listen(source)

print(r.recognize_google(audio, language="de_DE"))

How can I change the script to save audio to a wav/mp3-file?

Upvotes: 4

Views: 3307

Answers (1)

Steampunkery
Steampunkery

Reputation: 3874

Hello and welcome to StackOverflow! Looks like you can call get_wav_data on your audio variable to obtain a byte string that represents the WAV data. You can then write it to a file. The documentation for get_wav_data is here. You can write bytes to a file like so:

with open('your_file.wav', 'wb') as file:
    wav_data = audio.get_wav_data()
    file.write(wav_data)

Upvotes: 1

Related Questions