使用OpenCV VideoWriter和Python BytesIO在内存中流式传输视频 [英] Streaming video in memory with OpenCV VideoWriter and Python BytesIO

查看:649
本文介绍了使用OpenCV VideoWriter和Python BytesIO在内存中流式传输视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用Python中的OpenCV VideoWriter 类'流'数据?

I was wondering if it is possible to 'stream' data using the OpenCV VideoWriter class in Python?

通常用于处理内存中的数据,否则我会使用BytesIO(或StringIO)。

Normally for handling data in memory that would otherwise go to disk I use BytesIO (or StringIO).

我尝试使用BytesIO的尝试失败了:

My attempt to use BytesIO fails though:

import cv2
from io import BytesIO

stream = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc('x264')

data = BytesIO()

# added these to try to make data appear more like a string
data.name = 'stream.{}'.format('av1')
data.__str__ = lambda x: x.name

try:
    video = cv2.VideoWriter(data, fourcc=fourcc, fps=30., frameSize=(640, 480))
    start = data.tell()

        # Check if camera opened successfully
        if (stream.isOpened() == False): 
            print("Unable to read camera feed", file=sys.stderr)
            exit(1)

        # record loop
        while True:
            _, frame = stream.read()
            video.write(frame)
            data.seek(start)
            # do stuff with frame bytes
            # ...

            data.seek(start)

    finally:
        try:
            video.release()
        except:
            pass
finally:
    stream.release()

但是,而不是编写 BytesIO 对象时,我得到以下消息:

However instead of writing the BytesIO object I end up with the following message:

Traceback (most recent call last):
  File "video_server.py", line 54, in talk_to_client
    video = cv2.VideoWriter(data, fourcc=fourcc, fps=fps, frameSize=(width, height))
TypeError: Required argument 'apiPreference' (pos 2) not found

...所以当我将VideoWriter调用修改为 cv2.VideoWriter(data,apiPreference = 0,fourcc = fourcc,fps = 30。,frameSize =(640,480))(我读到0表示自动,但是我还尝试了 cv2.CAP_FFMPEG ),却收到以下错误:

... So when I modify the VideoWriter call to be cv2.VideoWriter(data, apiPreference=0, fourcc=fourcc, fps=30., frameSize=(640, 480)) (I read that 0 means auto, but I also tried cv2.CAP_FFMPEG), I instead get the following error:

Traceback (most recent call last):
  File "video_server.py", line 54, in talk_to_client
    video = cv2.VideoWriter(data, apiPreference=0, fourcc=fourcc, fps=fps, frameSize=(width, height))
TypeError: bad argument type for built-in operation

所以我的问题是,是否可以使用内存中的 cv2.VideoWriter 类编写编码的视频,如果可以的话,怎么做?

So my question is, is it possible to write encoded video using the cv2.VideoWriter class in memory and if so how is it done?

在这一点上,我还是个新主意,因此,任何帮助都是最欢迎的:-)

At this point I'm fresh out of ideas, so any help would be most welcome :-)

推荐答案

不幸的是,OpenCV不支持对内存的编码(或从中解码)。您必须写入(或读取)磁盘以使VideoWriter(或VideoCapture)正常工作。

Unfortunately, OpenCV doesn't support encoding to (or decoding from) memory. You must write to (or read from) disk for VideoWriter (or VideoCapture) to work.

这篇关于使用OpenCV VideoWriter和Python BytesIO在内存中流式传输视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆