【发布时间】:2020-11-15 23:46:00
【问题描述】:
我正在使用这个“逻辑”从我的网络客户端将网络摄像头流式传输回服务器:
def gen():
"""Video streaming generator function."""
cap = cv2.VideoCapture(0)
vid_format = 'MJPG'
fourcc = cv2.VideoWriter_fourcc(*vid_format)
out = cv2.VideoWriter('c:/test/output.avi', fourcc, 10.0, (640,480))
# Read until video is complete
#sample_width, data = record()
while(cap.isOpened()):
# Capture frame-by-frame
ret, img = cap.read()
img = cv2.flip(img, 1)
if ret == True:
out.write(img)
frame = cv2.imencode('.jpg', img)[1].tobytes()
#frame = cv2.flip(frame, 1)
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
time.sleep(0.1)
else:
break
并且想同时录制声音:
def record():
"""
Record a word or words from the microphone and
return the data as an array of signed shorts.
Normalizes the audio, trims silence from the
start and end, and pads with 0.5 seconds of
blank sound to make sure VLC et al can play
it without getting chopped off.
"""
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=1, rate=RATE,
input=True, output=True,
frames_per_buffer=CHUNK_SIZE)
num_silent = 0
snd_started = False
r = array('h')
while 1:
# little endian, signed short
snd_data = array('h', stream.read(CHUNK_SIZE))
if byteorder == 'big':
snd_data.byteswap()
r.extend(snd_data)
silent = is_silent(snd_data)
if silent and snd_started:
num_silent += 1
elif not silent and not snd_started:
snd_started = True
if snd_started and num_silent > 30:
break
sample_width = p.get_sample_size(FORMAT)
stream.stop_stream()
stream.close()
p.terminate()
r = normalize(r)
r = trim(r)
r = add_silence(r, 0.5)
return sample_width, r
正如我所看到的,同时进行两者的唯一方法是在不同的线程中运行录音。
这是正确的方式还是有更“结构化”或更优雅的方式?
谢谢。
【问题讨论】: