我正在尝试找出一种方法,为要保存的录制文件设置本地目录的自定义路径。当前,录制保存在项目目录中,但我希望为其指定其他文件夹。
def record_audio():
filename =test
chunk = 1024
FORMAT = pyaudio.paInt16
channels = 1
sample_rate = 16000
record_seconds = 5
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=channels,
rate=sample_rate,
input=True,
output=True,
frames_per_buffer=chunk)
frames = []
for i in range(int(sample_rate / chunk * record_seconds)):
data = stream.read(chunk)
# stream.write(data)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
st.write("Finished recording.")
wf = wave.open(filename, "wb")
# set the channels
wf.setnchannels(channels)
# set the sample format
wf.setsampwidth(p.get_sample_size(FORMAT))
# set the sample rate
wf.setframerate(sample_rate)
# write the frames as bytes
wf.writeframes(b"".join(frames))
# close the file
wf.close()
1条答案
按热度按时间toe950271#
只要声明一个文件变量并指定路径,你就可以找到它,无论你是在Windows上使用命令行作为CD还是在UNIX上使用命令行作为PWD。最后,你在写wav的时候声明它,就这样。
就像这样: