OpenCV issue camera when writing video

2.7k Views Asked by At

I'm trying to write a video with my laptop camera using OpenCV but I get the following error:

VIDEOIO ERROR: V4L/V4L2: VIDIOC_S_CROP
VIDEOIO ERROR: V4L2: getting property #5 is not supported
OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend does not support this file type.) in CvVideoWriter_GStreamer::open, file /home/tul1/Downloads/opencv-3.0.0-alpha/modules/videoio/src/cap_gstreamer.cpp, line 1265
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/tul1/Downloads/opencv-3.0.0-alpha/modules/videoio/src/cap_gstreamer.cpp:1265: error: (-210) Gstreamer Opencv backend does not support this file type. in function CvVideoWriter_GStreamer::open

The code I'm using is:

#include <opencv/cv.hpp>
#include <opencv2/opencv.hpp>
#include <opencv/highgui.h>

int main(int argc, char** argv)
{
    cvNamedWindow("DisplayCamera", CV_WINDOW_AUTOSIZE);
    CvCapture* capture = cvCreateCameraCapture(0);
    IplImage* frame;

    const char filename[] = "video";

//  int fourcc = CV_FOURCC('X','V','I','D');
//  int fourcc = CV_FOURCC('P','I','M','1');
    int fourcc = CV_FOURCC('M','J','P','G');
//  int fourcc = CV_FOURCC('M', 'P', '4', '2');
//  int fourcc = CV_FOURCC('D', 'I', 'V', '3');
//  int fourcc = CV_FOURCC('D', 'I', 'V', 'X');
//  int fourcc = CV_FOURCC('U', '2', '6', '3');
//  int fourcc = CV_FOURCC('I', '2', '6', '3');
//  int fourcc = CV_FOURCC('F', 'L', 'V', '1');
//  fourcc = -1;
//  int fourcc = 0;

//  printf("%d\n", fourcc);

    double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    int width = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH );
    int height = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT );
    CvSize size = cvSize( width , height );
    int isColor = 1;
    CvVideoWriter* writer = cvCreateVideoWriter(filename, fourcc, fps, size, isColor);

    while(1)
    {
        frame = cvQueryFrame( capture );
        if( !frame )
        {
            fprintf(stderr,"Frame error");
            break;
        }

        cvWriteFrame(writer , frame);
        cvShowImage("DisplayCamera", frame);

        char c = cvWaitKey( 30 );
        if( c == 27 ) break;    
    }

    cvReleaseVideoWriter(&writer);
    cvReleaseImage(&frame);
    cvReleaseCapture(&capture);
    cvDestroyWindow("DisplayCamera");
    return 0;
}

As you can see I've tested every codec with CV_FOURCC but still the error shows up.

What am I doing wrong? Do I have a codec issue?

2

There are 2 best solutions below

1
On

The problem might be just that filename doesn't have an extension. Update your code to:

const char filename[] = "video.avi";

Another potential problem is the fps returned by the camera. 0 would cause the problem you are having.

0
On

Try use this code.

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(){

    VideoCapture vcap(0); 
      if(!vcap.isOpened()){
             cout << "Error opening video stream or file" << endl;
             return -1;
      }

   double fps =  vcap.get(CV_CAP_PROP_FPS);
   int frame_width=   vcap.get(CV_CAP_PROP_FRAME_WIDTH);
   int frame_height=   vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
   VideoWriter video("out.avi",CV_FOURCC('M','J','P','G'),fps, Size(frame_width,frame_height),true);

   for(;;){

       Mat frame;
       vcap >> frame;
       video.write(frame);
       imshow( "Frame", frame );
       char c = (char)waitKey(33);
       if( c == 27 ) break;
    }
  return 0;
}