网上收集的,一个不错的python录音代码,直接上代码:
# --coding:utf-8-- import wave import pyaudio # 用Pyaudio库录制音频 # out_file:输出音频文件名 # rec_time:音频录制时间(秒) def audio_record(out_file, rec_time): CHUNK = 1024 FORMAT = pyaudio.paInt16 # 16bit编码格式 CHANNELS = 1 # 单声道 RATE = 16000 # 16000采样频率 p = pyaudio.PyAudio() # 创建音频流 stream = p.open(format=FORMAT, # 音频流wav格式 channels=CHANNELS, # 单声道 rate=RATE, # 采样率16000 input=True, frames_per_buffer=CHUNK) print("Start Recording...") frames = [] # 录制的音频流 # 录制音频数据 for i in range(0, int(RATE / CHUNK * rec_time)): data = stream.read(CHUNK) frames.append(data) # 录制完成 stream.stop_stream() stream.close() p.terminate() print("Recording Done...") # 保存音频文件 wf = wave.open(out_file, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() if __name__ == '__main__': time=int(input('请输入录制时间')) audio_record('./aaa.wav',time)
运行后会生成aaa.wav在根目录下。