简体   繁体   中英

How do you save a recorded .wav file to a specific directory in python

I found a python program on the internet that can do voice recording directly using a microphone. However, when the program finishes running, the resulting .wav file created by the program is stored in the directory where the python program was created. So, how do you save the recorded files in a specific directory?

import pyaudio
import wave

form_1 = pyaudio.paInt16
chans = 1 # 1 channel
samp_rate = 48000
chunk = 1024
record_secs = 2
dev_index = 2 
wav_output_filename = 'test1.wav' # name of .wav file

audio = pyaudio.PyAudio() # create pyaudio instantiation

# create pyaudio stream
stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \
                    input_device_index = dev_index,input = True, \
                    frames_per_buffer=chunk)
print("recording")
frames = []

# loop through stream and append audio chunks to frame array
for ii in range(0,int((samp_rate/chunk)*record_secs)):
    data = stream.read(chunk)
    frames.append(data)

print("finished recording")

# stop the stream, close it, and terminate the pyaudio instantiation
stream.stop_stream()
stream.close()
audio.terminate()

# save the audio frames as .wav file
wavefile = wave.open(wav_output_filename,'wb')
wavefile.setnchannels(chans)
wavefile.setsampwidth(audio.get_sample_size(form_1))
wavefile.setframerate(samp_rate)
wavefile.writeframes(b''.join(frames))
wavefile.close()

在第 10 行:

wav_output_filename = '/path/to/specific/directory/test1.wav'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM