方法一:
import pyaudio,wave,cv2,threading
wav = pyaudio.PyAudio()
# 打开声卡,设置 采样深度为16位、声道数为2、采样率为16、输入、采样点缓存数量为2048
stream = wav.open(format=pyaudio.paInt16, channels=2, rate=16000, input=True, frames_per_buffer=2048)
record_buf = []
count = 0
while count <500:
audio_data = stream.read(2048) # 读出声卡缓冲区的音频数据
record_buf.append(audio_data) # 将读出的音频数据追加到record_buf列表
count += 1
if cv2.waitKey(1) == 27:
break
print(count)
wf = wave.open('01.wav', 'wb') # 创建一个音频文件,名字为“01.wav"
wf.setnchannels(2) # 设置声道数为2
wf.setsampwidth(2) # 设置采样深度为
wf.setframerate(16000) # 设置采样率为16000
# 将数据写入创建的音频文件
wf.writeframes("".encode().join(record_buf))
# 写完后将文件关闭
wf.close()
# 停止声卡
stream.stop_stream()
# 关闭声卡
stream.close()
# 终止pyaudio
wav.terminate()
方法二:
import speech_recognition as sr #pyaudio SpeechRecognition模块
def rec(rate=16000): #从系统麦克风拾取音频数据,采样率为 16000
r = sr.Recognizer()
with sr.Microphone(sample_rate=rate) as source:
print("录音开始") #这里会打印please say something,提示你说话进行录音
audio = r.listen(source)
with open("recording.wav", "wb") as f: #把采集到的音频数据以 wav 格式保存在当前目录下的recording.wav 文件
f.write(audio.get_wav_data())
print('录音结束')
return 1
rec() #运行rec函数,录制音频
import os
os.system('recording.wav')