问题描述
我正在使用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);
结果是:
/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的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管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!