【问题标题】:How to specify the file path for saving the audio file recorded with PyaudioPyaudio录制的音频文件如何指定保存路径
【发布时间】:2023-02-02 12:03:58
【问题描述】:

我正在尝试找出一种方法来为要保存的记录文件设置本地目录的自定义路径。目前,录音保存在我的项目目录中,但我想为其指定一个不同的文件夹。

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()

【问题讨论】:

  • 尝试在 filename 中包含相对路径?

标签: python pyaudio portaudio


【解决方案1】:

就这么简单,声明一个文件变量,指定路径,无论是在Windows上用命令行当CD,还是在UNIX上当PWD,都可以定位到。最后你在写 wav 时声明它就是这样。

Linux 中的示例:var 文件:'/home/root/Projects/projectName/'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 2021-01-15
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多