Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem writing video files

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!

Problem writing video files

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 get the exception:

VIDEOIO ERROR: V4L2: Unable to get property <unknown property string>(6) - Invalid argument
Video stats:
Four character code: -1
Frames per second: 24

Maybe there is a problem with V4L2 too!