【问题标题】:Record Sound & Video form web client to flask server将声音和视频从 Web 客户端录制到烧瓶服务器
【发布时间】: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

正如我所看到的,同时进行两者的唯一方法是在不同的线程中运行录音。

这是正确的方式还是有更“结构化”或更优雅的方式?

谢谢。

【问题讨论】:

    标签: python flask cv2 pyaudio


    【解决方案1】:

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

    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'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
    
                #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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多