用OpenCV FOURCC编解码器编写视频

时间:2018-07-25 08:43:58

标签: c++ opencv video ffmpeg fourcc

我正在尝试将YOLO的结果保存到.avi文件中。已经看过this post ...(我什至在那问过问题),我找不到任何解决方案。这是我尝试保存.avi的代码。

{
    CvSize size;
    size.width = disp->width;
    size.height = disp->height;

    static CvVideoWriter* output_video = NULL;    // cv::VideoWriter output_video;
    if (output_video == NULL)
    {
        printf("\n SRC output_video = %p \n", output_video);
        const char* output_name = "test_dnn_out.avi";
        output_video = cvCreateVideoWriter(output_name, CV_FOURCC('D', 'I', 'V', 'X'), 25, size, 1);
        printf("\n cvCreateVideoWriter, DST output_video = %p  \n", output_video);
    }
    cvWriteFrame(output_video, disp);
}

尝试大量编解码器(更改CV_FOURCC('D', 'I', 'V', 'X'))时出现以下错误:

**CODEC :-1**

**ERROR**

SRC output_video = (nil)  OpenCV: FFMPEG: tag 0xffffffff/'����' is not
found (format 'avi / AVI (Audio Video Interleaved)')' OpenCV Error:
Unsupported format or combination of formats (Gstreamer Opencv backend
does not support this codec.) in CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1522 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1522:
error: (-210) Gstreamer Opencv backend does not support this codec. in
function CvVideoWriter_GStreamer::open


**CODEC: CV_FOURCC('M', 'J', 'P', 'G')**

**ERROR**

SRC output_video = (nil)  OpenCV Error: Unspecified error (GStreamer:
cannot put pipeline to play ) in CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1690 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1690:
error: (-2) GStreamer: cannot put pipeline to play  in function
CvVideoWriter_GStreamer::open


**CODEC: CV_FOURCC('P','I','M','1')** SRC output_video = (nil) 

**ERROR**

** (Demo:6801): CRITICAL **: gst_ffmpeg_cfg_set_property: assertion 'qdata- 
size == sizeof (gint64)' failed OpenCV Error: Unspecified
error (GStreamer: cannot put pipeline to play ) in
CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1690 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1690:
error: (-2) GStreamer: cannot put pipeline to play  in function
CvVideoWriter_GStreamer::open



**CODEC:CV_FOURCC('M', 'P', '4', '2')**

**ERROR**

SRC output_video = (nil) 

** (Demo:7437): CRITICAL **: gst_ffmpeg_cfg_set_property: assertion 'qdata- 
size == sizeof (gint64)' failed OpenCV Error: Unspecified
error (GStreamer: cannot put pipeline to play ) in
CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1690 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1690:
error: (-2) GStreamer: cannot put pipeline to play  in function
CvVideoWriter_GStreamer::open


**CODEC::CV_FOURCC('D', 'I', 'V', '3')**

**ERROR** 

SRC output_video = (nil)  OpenCV Error: Unspecified error (GStreamer:
cannot put pipeline to play ) in CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1690 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1690:
error: (-2) GStreamer: cannot put pipeline to play  in function
CvVideoWriter_GStreamer::open


**CODEC:CV_FOURCC('D', 'I', 'V', 'X')**

**ERROR** 

SRC output_video = (nil)  OpenCV Error: Unspecified error (GStreamer:
cannot put pipeline to play ) in CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1690 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1690:
error: (-2) GStreamer: cannot put pipeline to play  in function
CvVideoWriter_GStreamer::open



**CODEC:CV_FOURCC('U', '2', '6', '3')**

**ERROR** 

OpenCV Error: Unspecified error (GStreamer: cannot put pipeline to
play ) in CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1690 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1690:
error: (-2) GStreamer: cannot put pipeline to play  in function
CvVideoWriter_GStreamer::open


cvCreateVideoWriter, DST output_video = (nil)  

SRC output_video = (nil)  Could not open codec 'h263': Unspecified
error

** (Demo:8342): CRITICAL **: gst_ffmpeg_cfg_set_property: assertion 'qdata- 
size == sizeof (gint64)' failed



**CODEC:CV_FOURCC('I', '2', '6', '3')**

**ERROR** 

SRC output_video = (nil)  Could not find encoder for codec id 21:
Encoder not found OpenCV Error: Unspecified error (GStreamer: cannot
link elements ) in CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1626 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1626:
error: (-2) GStreamer: cannot link elements  in function
CvVideoWriter_GStreamer::open


cvCreateVideoWriter, DST output_video = (nil)  


**CODEC:CV_FOURCC('F', 'L', 'V', '1')**

**ERROR** 

SRC output_video = (nil)  OpenCV Error: Unspecified error (GStreamer:
cannot link elements ) in CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1626 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1626:
error: (-2) GStreamer: cannot link elements  in function
CvVideoWriter_GStreamer::open


cvCreateVideoWriter, DST output_video = (nil)


**CODEC:CV_FOURCC('i','Y', 'U', 'V')**

**ERROR** 

SRC output_video = (nil)  OpenCV: FFMPEG: tag 0x56555969/'iYUV' is not
supported with codec id 14 and format 'avi / AVI (Audio Video
Interleaved)' OpenCV Error: Unsupported format or combination of
formats (Gstreamer Opencv backend does not support this codec.) in
CvVideoWriter_GStreamer::open, file
/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp, line
1522 VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps,
frameSize, is_color)): raised OpenCV exception:

/home/akde/opencv-3.3.0/modules/videoio/src/cap_gstreamer.cpp:1522:
error: (-210) Gstreamer Opencv backend does not support this codec. in
function CvVideoWriter_GStreamer::open

这是我的设置

-ubuntu 16.04

-opencv 3.3.0

-ffmpeg version 2.8.14

我认为问题很可能与编解码器有关。因为人们通过使用不同的编解码器解决了相同的问题。即使我尝试了许多编解码器并正确安装了ffmepeg,也找不到任何解决方案。所以我该怎么做?期待您的回复...

1 个答案:

答案 0 :(得分:3)

您没有使用ffmpeg来创建视频,因此@zindarod指出,您应该首先检查OpenCV是否使用ffmpeg构建。为此,您可以在C ++中运行以下代码:

#include <opencv2/opencv.hpp>

int main(void)
{
  std::cout << cv::getBuildInformation() << std::endl;
}

除非有必要,否则请不要使用C API。不建议使用它,并且不应该使用它,特别是如果您的代码使用C ++作为问题提示的标记时。

实际上,您可以在C ++ API中选择其他后端。看看documentation of VideoWriter

  

cv :: VideoWriter :: VideoWriter (const字符串和文件名,整数     apiPreference,int fourcc,double fps,Size frameSize,bool     isColor = true)

     

这是一个重载成员函数,为方便起见而提供。它   与上面的函数的不同之处仅在于它接受什么参数。   apiPreference参数允许指定要使用的API后端。能够   如果有多个阅读器,则用于实施特定的阅读器实现   可用:例如cv :: CAP_FFMPEG或cv :: CAP_GSTREAMER。

这意味着,如果两者都安装并且OpenCV是在两者的支持下构建的,则可以将其切换为自己喜欢的方式:)

GStreamer并不是一个不好的后端,我实际上使用它,但是我更多地使用它来打开视频而不是创建视频,但是您需要安装插件才能使其与更多编解码器一起使用... { {3}}建议像这样安装它:

apt-get install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools

1.0可能会更改,具体取决于您正在使用的版本。

我希望这能解决您的问题,即使不发表评论。