点此进入CSDN

点此添加QQ好友 加载失败时会显示




pyaudio音频录制python

 

python3.7不支持pyaudio pip在线安装

whl下载地址https://github.com/intxcc/pyaudio_portaudio/releases

下载后使用pip离线安装

pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl

  

# pyrec.py 文件内容
import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
RECORD_SECONDS = 3

def rec(file_name):
    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    print("开始录音,请说话......")

    frames = []

    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(file_name, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()


if __name__ == '__main__':
    rec("audio/xinyuliu.wav")

  

posted @ 2020-09-09 19:08  高颜值的殺生丸  阅读(216)  评论(0编辑  收藏  举报

作者信息

昵称:

刘新宇

园龄:4年6个月


粉丝:1209


QQ:522414928