使用VideoWriter从OpenCV打开GStreamer管道 [英] Opening a GStreamer pipeline from OpenCV with VideoWriter

查看:677
本文介绍了使用VideoWriter从OpenCV打开GStreamer管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV捕获和处理视频帧,我想将它们编写为h265视频文件。我正在努力从OpenCV获得合适的Gstreamer管道来工作。

I am capturing and processing video frames with OpenCV, and I would like to write them as a h265 video file. I am struggling to get a proper Gstreamer pipeline to work from OpenCV.

Gstreamer本身可以正常工作。特别是,我能够运行此命令,该命令可以非常快速地对视频进行编码(由于GPU加速)并将其保存到mkv文件中:

Gstreamer works fine by itself. In particular, I am able to run this command, which encodes video very quickly (thanks to GPU acceleration) and saves it to a mkv file:

gst-launch-1.0 videotestsrc num-buffers=90 ! 'video/x-raw, format=(string)I420, width=(int)640, height=(int)480' ! omxh265enc ! matroskamux ! filesink location=test.mkv

现在,我想在OpenCV应用程序中执行相同的操作。我的代码是这样的:

Now I would like to do the same thing from within my OpenCV application. My code is something like:

Mat img_vid = Mat(1024, 1024, CV_8UC3);

VideoWriter video;
video.open("appsrc ! autovideoconvert ! omxh265enc ! matroskamux ! filesink location=test.mkv", 0, (double)25, cv::Size(1024, 1024), true);

if (!video.isOpened()) {
   printf("can't create writer\n");
   return -1;
}

while ( ... ) {

   // Capture frame into img_vid => That works fine

   video.write(img_vid);

   ...
}

似乎可以使用,但是它的作用是创建文件 named appsrc!autovideoconvert!omxh265enc!matroskamux!filesink location = test.mkv 并用未压缩的视频帧填充它,完全忽略了这是Gstreamer管道的事实

At first sight, this seems to work, but what it does is it creates file named "appsrc ! autovideoconvert ! omxh265enc ! matroskamux ! filesink location=test.mkv" and fills it with uncompressed video frames, completely ignoring the fact that this is a Gstreamer pipeline.

我尝试了其他管道,但是它们导致了各种错误:

I have tried other pipelines, but they result in a variety of errors:

video.open("appsrc ! autovideoconvert ! omxh264enc ! 'video/x-h264, streamformat=(string)byte-stream' ! h264parse ! qtmux ! filesink location=test.mp4 -e", 0, (double)25, cv::Size(1024, 1024), true);

结果是:


(测试:5533):GStreamer-CRITICAL **:gst_element_make_from_uri:
断言'gst_uri_is_valid(uri)'失败OpenCV错误:未指定
错误(GStreamer:在手动管道中找不到appsrc)在
CvVideoWriter_GStreamer :: open中,文件
/home/ubuntu/opencv/modules/videoio/src/cap_gstreamer.cpp,1363行
VIDEOIO(cvCreateVideoWriter_GStreamer(文件名,fourcc,fps,
frameSize,is_color)):引发OpenCV异常:

(Test:5533): GStreamer-CRITICAL **: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed OpenCV Error: Unspecified error (GStreamer: cannot find appsrc in manual pipeline ) in CvVideoWriter_GStreamer::open, file /home/ubuntu/opencv/modules/videoio/src/cap_gstreamer.cpp, line 1363 VIDEOIO(cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color)): raised OpenCV exception:

/home/ubuntu/opencv/modules/videoio/src/cap_gstreamer.cpp:1363:错误:
(-2)GStreamer:在函数
的手动管道中找不到appsrc CvVideoWriter_GStreamer :: open

/home/ubuntu/opencv/modules/videoio/src/cap_gstreamer.cpp:1363: error: (-2) GStreamer: cannot find appsrc in manual pipeline in function CvVideoWriter_GStreamer::open

我也尝试过简单:

video.open("appsrc ! autovideosink", 0, (double)25, cv::Size(1024, 1024), true);

这将产生:


GStreamer插件:嵌入式视频播放停止;模块appsrc0
报告:内部数据流错误。

GStreamer Plugin: Embedded video playback halted; module appsrc0 reported: Internal data flow error.

我正在使用支持Gstreamer的OpenCV 3.1。硬件是带有L4T 24.2.1的Jetson TX1。

I am using OpenCV 3.1 with Gstreamer support. The hardware is a Jetson TX1 with L4T 24.2.1.

推荐答案

我之前遇到过类似的问题。由于管道/文件名以 .mkv 结尾,因此OpenCV会将其解释为视频文件而不是管道。

I encountered a similar problem before. Since the pipe/file name ends with .mkv, OpenCV interprets it as a video file instead of a pipe.

您可以尝试在 mkv

video.open("appsrc ! autovideoconvert ! omxh265enc ! matroskamux ! filesink location=test.mkv ", 0, (double)25, cv::Size(1024, 1024), true);

或具有虚拟属性,如

video.open("appsrc ! autovideoconvert ! omxh265enc ! matroskamux ! filesink location=test.mkv sync=false", 0, (double)25, cv::Size(1024, 1024), true);

这篇关于使用VideoWriter从OpenCV打开GStreamer管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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