[英]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.我在网上找到了一个可以直接用麦克风录音的python程序。 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.但是,当程序完成运行时,程序创建的结果 .wav 文件存储在创建 python 程序的目录中。 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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.