linux python 录音
时间: 2023-07-20 08:29:10 浏览: 196
python录音
要在Linux上使用Python进行录音,可以使用PyAudio库。首先,您需要在终端中安装PyAudio库:
```
pip install pyaudio
```
然后,您可以使用以下代码来录制音频:
```python
import pyaudio
import wave
# 设置音频参数
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
# 初始化PyAudio
audio = pyaudio.PyAudio()
# 开始录音
stream = audio.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()
audio.terminate()
# 保存录音文件
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
```
这段代码将录制5秒钟的音频并将其保存到名为“output.wav”的文件中。您可以根据需要更改参数。
阅读全文