import pyaudio
import wave
def record_audio(seconds, filename):
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = seconds
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
frames = []
print("开始录音...")
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("结束录音.")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(filename, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
if __name__ == '__main__':
record_audio(5, '1.wav') # 5秒录音并保存为output.wav文件
上一篇
SSH链接服务器操作流程以及命令
SSH链接服务器操作流程以及命令
2023-08-11
下一篇
python找到数组中最大或最小的N个数
python找到数组中最大或最小的N个数
2023-04-01