Ask Your Question
1

Unable to create video with CV_FOURCC('Y','U','Y','V')

asked 2014-02-19 06:22:40 -0600

niteshuc gravatar image

updated 2014-02-19 06:31:07 -0600

Hi,

I am working on opencv version 2.4.8 under Ubuntu 12.04 LTS environment.

I am facing problem while creating a video ,while capturing from a webcam (UVC compatible).

It is giving error

"Could not create video."

Here is my code:

// Include standard OpenCV headers
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;

// All the new API is put into "cv" namespace
using namespace cv;

int main (int argc, char *argv[])
{
    // Open the default camera
    VideoCapture capture(0); 

    // Check if the camera was opened
    if(!capture.isOpened())
    {
        cerr << "Could not create capture";
        return -1;
    }

    // Get the properties from the camera
    double width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
    double height = capture.get(CV_CAP_PROP_FRAME_HEIGHT);

    cout << "Camera properties\n";
    cout << "width = " << width << endl <<"height = "<< height << endl;

    // Create a matrix to keep the retrieved frame
    Mat frame;

    // Create a window to show the image
    namedWindow ("Capture", CV_WINDOW_AUTOSIZE);

    // Create the video writer
    VideoWriter video("capture.avi", CV_FOURCC('Y', 'U', 'Y', 'V'), 30, cvSize((int)width,(int)height));
    //video.open("capture.avi", -1, 30, cvSize((int)width,(int)height));

    // Check if the video was opened
    if(!video.isOpened())
    {
        cerr << "Could not create video.";
        return -1;
    }

    cout << "Press Esc to stop recording." << endl;

    // Get the next frame until the user presses the escape key
    while(true)
    {
        // Get frame from capture
        capture >> frame;

        // Check if the frame was retrieved
        if(!frame.data)
        {
            cerr << "Could not retrieve frame.";
            return -1;
        }

        // Save frame to video
        video << frame;

        // Show image
        imshow("Capture", frame);

        // Exit with escape key
        if(waitKey(1) == 27)
            break;
    }

    // Exit
    return 0;
}

The information about camera is as follows.

$ v4l2-ctl --all

Driver Info (not using libv4l2):
    Driver name   : uvcvideo
    Card type     : USB2.0 Camera
    Bus info      : usb-0000:00:1d.0-1.5
    Driver version: 3.2.40
    Capabilities  : 0x04000001
        Video Capture
        Streaming
Video input : 0 (Camera 1: ok)
Format Video Capture:
    Width/Height  : 640/480
    Pixel Format  : 'YUYV'
    Field         : None
    Bytes per Line: 1280
    Size Image    : 1045178410
    Colorspace    : SRGB
Crop Capability Video Capture:
    Bounds      : Left 0, Top 0, Width 640, Height 480
    Default     : Left 0, Top 0, Width 640, Height 480
    Pixel Aspect: 1/1
Streaming Parameters Video Capture:
    Capabilities     : timeperframe
    Frames per second: 0.004 (1381/377737)
    Read buffers     : 0
                     brightness (int)    : min=-127 max=127 step=1 default=0 value=0
                       contrast (int)    : min=0 max=127 step=1 default=64 value=64
                     saturation (int)    : min=0 max=255 step=1 default=64 value=64
                            hue (int)    : min=-16000 max=16000 step=1 default=0 value=0
                          gamma (int)    : min=16 max=500 step=1 default=100 value=100
           power_line_frequency (menu)   : min=0 max=2 default=1 value=1
                      sharpness (int)    : min=1 max=29 step=1 default=9 value=9
         backlight_compensation (int)    : min=0 max=1 step=1 default=0 value=0

Please help me how to solve this problem ..

edit retag flag offensive close merge delete

Comments

I find it quite weird you are including old C - API headers but you are programming in the new C++ API. I suggest changing the opencv in your includes to opencv2 first and see if it gets solved.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-02-19 06:33:38 -0600 )edit
1

Well i changed the includes from opencv to opencv2 , but still problem is not solved.

niteshuc gravatar imageniteshuc ( 2014-02-19 22:20:21 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2014-02-19 08:05:14 -0600

unxnut gravatar image

This question was answered here. It may have happened due to the codec not found. You can try with -1 in place of FOURCC code and it will give you a list of codecs available.

edit flag offensive delete link more

Comments

I had already tried with -1 in place of FOURCC ,but still the problem is the same.

niteshuc gravatar imageniteshuc ( 2014-02-19 22:22:50 -0600 )edit
1

Doesn't it give you a prompt asking you to select a format? Your problem seems to be the lack of codec. You may have to install the appropriate codecs and if you already have some codecs, try to use them.

unxnut gravatar imageunxnut ( 2014-02-19 22:39:15 -0600 )edit
2

answered 2014-02-26 22:22:12 -0600

niteshuc gravatar image

I solved the problem by compiling opencv with "WITH_FFMPEG=ON" option and changing FOURCC to CV_FOURCC('M','J','P','G').

edit flag offensive delete link more

Comments

1

Actually the prebuilt libraries should be compiled with that option enabled. You should mark your own answer as the solution, as it might help others to solve this problem. However it is quite normal if the functionality isn't compiled that it won't work decently.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-02-27 02:42:45 -0600 )edit

Question Tools

Stats

Asked: 2014-02-19 06:22:40 -0600

Seen: 7,542 times

Last updated: Feb 26 '14