文件系统:Debian9.9
安装pyaudio
apt-get install python3-pyaudio
root@ubuntu:/yangfeiwu/python# pip3 list
cryptography (1.7.1)
idna (2.2)
keyring (10.1)
keyrings.alt (1.3)
pip (9.0.1)
pyasn1 (0.1.9)
PyAudio (0.2.11)
pycrypto (2.6.1)
pygobject (3.22.0)
pyxdg (0.25)
SecretStorage (2.3.1)
setuptools (45.3.0)
six (1.10.0)
wheel (0.29.0)
可以看到PyAudio (0.2.11)已经安装成功了。
使用Python录音
import pyaudio
import wave
import os
import sys
CHUNK = 512
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("done")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
执行后会录制一段5秒的音频,输出为同目录下的output.wav文件。
python3 rec.py
来源:CSDN
作者:Yfw&武
链接:https://blog.csdn.net/u012577474/article/details/104732187