如何将录制的.wav文件保存到python中的特定目录



我在网上找到了一个python程序,可以直接使用麦克风进行录音。但是,当程序运行结束时,程序创建的.wav文件将存储在创建python程序的目录中。那么,如何将录制的文件保存到特定的目录中呢?

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'

最新更新