简体   繁体   English

如何将 Opencv VideoWriter 与 GStreamer 一起使用?

[英]How to use Opencv VideoWriter with GStreamer?

I am trying to transfer a h264 stream using Opencv VideoWriter to get it on another pc on the network using VideoCapture.我正在尝试使用 Opencv VideoWriter 传输 h264 stream 以使用 VideoCapture 在网络上的另一台电脑上获取它。 However, I am stuck on VideoWriter.但是,我被困在 VideoWriter 上。 Execution of this code returns with an error and out.isOpened () is always false.执行此代码会返回错误,并且 out.isOpened () 始终为 false。

    int FOURCC = cv::VideoWriter::fourcc('H', '2', '6', '4');
    cv::VideoWriter out;
    out.open ("appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000",
        cv::CAP_GSTREAMER,
        FOURCC,
        16,
        cv::Size (640, 480),
        true);

    if (!out.isOpened ()) {
        qDebug () << "\ n ***** Failed to open videowriter *****";
        return -1;
    }

[ WARN:0] global D:\Libs\opencv-4.3.0\modules\videoio\src\cap_gstreamer.cpp (1274) cv::CvVideoWriter_GStreamer::close_ OpenCV | GStreamer warning: No source in GStreamer pipeline. Ignore
[ERROR:0] global D:\Libs\opencv-4.3.0\modules\videoio\src\cap.cpp (527) cv::VideoWriter::open VIDEOIO(GSTREAMER): raised OpenCV exception:
OpenCV(4.3.0) D:\Libs\opencv-4.3.0\modules\videoio\src\cap_gstreamer.cpp:144: error: (-215:Assertion failed) ptr in function 'cv::`anonymous-namespace'::GSafePtr<struct _GstElement>::get'

***** Failed open videowriter *****

Even a simple example returns me an error and out.isOpened() false.即使是一个简单的例子也会给我一个错误并且 out.isOpened() 错误。

    out.open("autovideosrc ! videoconvert ! appsink",
             cv::CAP_GSTREAMER,
             FOURCC,
             16,
             cv::Size(640, 480),
             true);

[ WARN:0] global D:\Libs\opencv-4.3.0\modules\videoio\src\cap_gstreamer.cpp (1500) cv::CvVideoWriter_GStreamer::open    OpenCV | GStreamer warning: OpenCV backend does not support this file type (extension): autovideosrc ! videoconvert !   appsink
[ WARN:0] global D:\Libs\opencv-4.3.0\modules\videoio\src\cap_gstreamer.cpp (1274) cv::CvVideoWriter_GStreamer::close_  OpenCV | GStreamer warning: No source in GStreamer pipeline. Ignore

    ***** Failed to open videowriter *****

The version of opencv 4.3.0 is compiled from source code with gstreamer support. opencv 4.3.0 的版本是从支持 gstreamer 的源代码编译而来的。 cv::getBuildInformation () says: cv::getBuildInformation () 说:

    Video I/O:
      DC1394: NO
      FFMPEG: YES (prebuilt binaries)
        avcodec: YES (58.54.100)
        avformat: YES (58.29.100)
        avutil: YES (56.31.100)
        swscale: YES (5.5.100)
        avresample: YES (4.0.0)
      GStreamer: YES (1.16.2)
      DirectShow: YES
      Media Foundation: YES
        DXVA: YES

How can I stream the stream?我怎样才能 stream stream? What parameters should be specified by VideoWriter? VideoWriter应该指定哪些参数? I tried various tips from google, but none of them helped me.我尝试了谷歌的各种提示,但没有一个对我有帮助。 I would be grateful for a simple example of how to send a stream from VideoWriter on one side and receive it from VideoCapture on the other.我将不胜感激一个简单的示例,说明如何从一侧的 VideoWriter 发送 stream 并从另一侧的 VideoCapture 接收它。 I am using Windows 10 x64 and Qt5.13 MSVC2017我正在使用 Windows 10 x64 和 Qt5.13 MSVC2017

You need to feed raw video to appsrc .您需要将原始视频提供给appsrc Setting fourcc to h264 forces VideoWriter to encode video instead of gstreamer pipe.fourcc设置为h264会强制VideoWriter编码视频而不是gstreamer pipe。 You can set your fourcc to 0 to push raw video.您可以将fourcc设置为0 以推送原始视频。 The following should work.以下应该工作。

cv::VideoWriter out;
out.open ("appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000",
    cv::CAP_GSTREAMER,
    0,
    16,
    cv::Size (640, 480),
    true);

I believe that this is a bug in OpenCV, which should be fixed by this PR , or maybe this one (or maybe both).我相信这是 OpenCV 中的一个错误,应该由这个 PR这个(或两者)修复。

Indeed, it seems like the OpenCV code used to work with some version of GStreamer that would set the error pointer to NULL when the function succeeds, but that is not guaranteed with 1.16.2 in my experience (and from the documentation). Indeed, it seems like the OpenCV code used to work with some version of GStreamer that would set the error pointer to NULL when the function succeeds, but that is not guaranteed with 1.16.2 in my experience (and from the documentation).

With those PRs, it should properly use the return value instead of relying on the state of the error pointer.使用这些 PR,它应该正确使用返回值,而不是依赖错误指针的 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM