从Web客户端录制声音和视频到烧瓶服务器



我用这一代将网络摄像头从我的网络客户端流式传输回服务器"逻辑":

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'--framern'b'Content-Type: image/jpegrnrn' + frame + b'rn')
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

我看来,同时进行这两项工作的唯一方法是在不同的线程中运行录音。

这是正确的方式,还是有更"结构化"或更优雅的东西?

谢谢。

这里没有多线程(显然(:

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, 24.0, (640,480))

# Read until video is complete
#sample_width, data = 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')
# little endian, signed short

#silent = is_silent(snd_data)

#return sample_width, r
while cap.isOpened():
# Capture frame-by-frame
try:
ret, img = cap.read()
img = cv2.flip(img, 1)
snd_data = array('h', stream.read(CHUNK_SIZE))
if byteorder == 'big':
snd_data.byteswap()
r.extend(snd_data)
#r.extend(snd_data)
if ret == True:
out.write(img)
frame = cv2.imencode('.jpg', img)[1].tobytes()
#frame = cv2.flip(frame, 1)
yield (b'--framern'b'Content-Type: image/jpegrnrn' + frame + b'rn')
#time.sleep(0.1)
#print('true ret')
except:
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)
sample_width, data = sample_width, r
data = pack('<' + ('h' * len(data)), *data)
wf = wave.open('c:/test/my_wav.wav', 'wb')
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
break
#cap.release()
#out.release()
#data = pack('<' + ('h' * len(data)), *data)

最新更新