您可以使用opus格式提供来自麦克风的录制音频并保存到opus文件中吗

时间:2018-08-23 08:52:29

标签: python pyaudio opus

我想以opus格式录制音频5秒钟,然后以opus音频格式保存文件。

我正在使用以下代码:

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
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 recording")

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()

您能告诉我们如何转换为opus格式吗

0 个答案:

没有答案