Problem writing video files

asked 2015-08-18 14:15:36 -0500

H4NZ0 gravatar image

updated 2015-08-19 12:56:09 -0500

Hy Guys!

I have a C++ project in Eclipse using the OpenCV library. I'm using the code below and a mp4 file for testing. Compilation runs fine, but when I run the program i get an error. I've tried everything already. ffmpeg, avcodec, videocodecs, x264 and all the required and optional dependecies are installed. I've already searched for the problem and tried a bunch of tutorials, but the error persists. What am I doing wrong?

Program Code:

#include <iostream> // for standard I/O
#include <string>   // for strings

#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp>  // Video write

using namespace std;
using namespace cv;

static void help()
{
    cout
        << "------------------------------------------------------------------------------" << endl
        << "This program shows how to write video files."                                   << endl
        << "You can extract the R or G or B color channel of the input video."              << endl
        << "Usage:"                                                                         << endl
        << "./video-write inputvideoName [ R | G | B] [Y | N]"                              << endl
        << "------------------------------------------------------------------------------" << endl
        << endl;
}

int main(int argc, char *argv[])
{
    help();

    if (argc != 4)
    {
        cout << "Not enough parameters" << endl;
        return -1;
    }

    const string source      = argv[1];           // the source file name
    const bool askOutputType = argv[3][0] =='Y';  // If false it will use the inputs codec type

    VideoCapture inputVideo(source);              // Open input
    if (!inputVideo.isOpened())
    {
        cout  << "Could not open the input video: " << source << endl;
        return -1;
    }

    string::size_type pAt = source.find_last_of('.');                  // Find extension point
    const string NAME = source.substr(0, pAt) + argv[2][0] + ".mp4";   // Form the new name with container
    int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // Get Codec Type- Int form

    cout << NAME << endl;

    // Transform from int to char via Bitwise operators
    char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};

    Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),    // Acquire input size
                  (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));

    VideoWriter outputVideo;
    bool i = outputVideo.open(NAME, -1, inputVideo.get(CV_CAP_PROP_FPS), S, true);
    cout << i << endl;

    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write: " << source << endl;
        return -1;
    }

    cout << "Input frame resolution: Width=" << S.width << "  Height=" << S.height
         << " of nr#: " << inputVideo.get(CV_CAP_PROP_FRAME_COUNT) << endl;
    cout << "Input codec type: " << EXT << endl;


    cout << "Finished writing" << endl;
    return 0;
}

Error:

OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend does not support this codec.) in CvVideoWriter_GStreamer::open, file /home/uluyac/Bibliotecas/opencv-2.4/modules/highgui/src/cap_gstreamer.cpp, line 1304
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/uluyac/Bibliotecas/opencv-2.4/modules/highgui/src/cap_gstreamer.cpp:1304: error: (-210) Gstreamer Opencv backend does not support this codec. in function CvVideoWriter_GStreamer::open

I'm using Ubuntu 15.04 tried OpenCV versions 2.4 and 3.0, and I get the same error. I also tried different video formats, including .avi and .mp4!

Update:

The only way to make this work was setting the CV_CAP_PROP_FOURCC parameter directly to 0! The -1 as parameter still gives me the error! I also tried this test:

cout <<  "Four character code: " << input_cap.get(CV_CAP_PROP_FOURCC) <<
        "\nFrames per second: " << input_cap.get(CV_CAP_PROP_FPS) << endl;

And I ... (more)

edit retag flag offensive close merge delete

Comments

May be you should set explictely your codec int this line

outputVideo.open(NAME, -1, inputVideo.get(CV_CAP_PROP_FPS), S, true);

instead of -1.

LBerger gravatar imageLBerger ( 2015-08-19 01:56:03 -0500 )edit

I've set the codec parameter directly to 0 and worked! The -1 parameter continues to give me the error! The tutorial says that the parameter -1 for CV_CAP_PROP_FOURCC should open a window to select between the installed codecs in my system. Even more, I've updated the question with an aditional issue that I've found!

Nevertheless, thanks for the response and the help LBerger!

H4NZ0 gravatar imageH4NZ0 ( 2015-08-19 12:44:03 -0500 )edit