bugfix
This commit is contained in:
parent
e3322b9bac
commit
865543cf56
@ -1,64 +0,0 @@
|
||||
import pyaudio
|
||||
import av
|
||||
from av import AudioFrame
|
||||
import numpy as np
|
||||
|
||||
from appPublic.folderUtils import temp_file
|
||||
|
||||
# 录音参数
|
||||
|
||||
def MonoMircoPhone():
|
||||
CHUNK = 1024
|
||||
FORMAT = pyaudio.paInt16
|
||||
CHANNELS = 1
|
||||
RATE = 44100
|
||||
RECORD_SECONDS = 10
|
||||
|
||||
# 初始化PyAudio
|
||||
p = pyaudio.PyAudio()
|
||||
|
||||
# 打开音频流
|
||||
stream = p.open(format=FORMAT,
|
||||
channels=CHANNELS,
|
||||
rate=RATE,
|
||||
input=True,
|
||||
frames_per_buffer=CHUNK)
|
||||
|
||||
print("开始录音...")
|
||||
frames = []
|
||||
|
||||
for _ in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
|
||||
data = stream.read(CHUNK)
|
||||
ndarr = np.frombuffer(data, dtype=np.int16)
|
||||
print(ndarr.shape)
|
||||
c = ndarr.shape[0]
|
||||
ndarr.reshape(c,1)
|
||||
print(ndarr.shape)
|
||||
|
||||
frame = AudioFrame.from_ndarray(ndarr, format='s16', layout='mono')
|
||||
yield frame
|
||||
print(f'{type(data)}')
|
||||
frames.append(data)
|
||||
|
||||
print("录音结束.")
|
||||
|
||||
# 停止并关闭音频流
|
||||
stream.stop_stream()
|
||||
stream.close()
|
||||
p.terminate()
|
||||
|
||||
def frames_write_wave(frames):
|
||||
path = temp_file(suffix='.wav')
|
||||
output_container = av.open(path, 'w')
|
||||
out_stream = output_container.add_stream('pcm_s16le')
|
||||
for frame in frames:
|
||||
for packet in out_stream.encode(frame):
|
||||
output_container.mux(packet)
|
||||
for packet in out_stream.encode(None):
|
||||
output_container.mux(packet)
|
||||
output_container.close()
|
||||
return path
|
||||
|
||||
if __name__ == '__main__':
|
||||
frames = [f for f in MonoMircoPhone()]
|
||||
frame_write_wave(frames)
|
Loading…
Reference in New Issue
Block a user