简体   繁体   English

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

[英]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.

相关问题 如何将您在 python 中创建的.wav 文件保存到指定目录? - How to save a .wav file you created in python to a specify directory? 上载wav文件并使用python保存在目录中 - Upload wav file and save in directory using python 如何将文件保存到 python 中的特定目录? - How to save a file to a specific directory in python? 如何将文件保存到特定目录并在python中选择文件名? - How to save a file to a specific directory and choose the file's name in python? 如何将winsound.Beep()保存到python中的音频.wav文件? - How do I save a winsound.Beep() to an audio .wav file in python? 将录制的音频从浏览器保存到 Python API 作为 WAV 文件 - Persisting recorded audio from browser to Python API as WAV file 如何在python中使用硒将下载的文件保存到特定的相对目录 - How to save a downloaded file to a specific relative directory with selenium in python 如何在 kivy 中的 python 中保存为 mp3 或 wav 文件? - how to save as mp3 or wav file in python in kivy? 您如何读取此.wav文件字节数据? - How do you read this .wav file byte data? Python:如何将文件保存在其他目录中? - Python: how do I save a file in a different directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM