Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Exporting video from image sequence

Hey guys,

So I'm working on a project that involves the user outputting a series of images that I would like to combine into a single video. Right now, these images are exported as HDR files, but I have tried this with a png sequence as well and run into the same issue. I'm running into an issue with combining these into a video. The images are read in fine, but when I try to open the VideoWriter, something isn't working.

I'm not sure if it has to do with the codec that I'm trying to use (I'll admit to a slight bit of ignorance when it comes to those) or something else entirely. The program does output an empty .AVI video of 0kb, which is obviously wrong.

Any help would be appreciated, so thanks in advance. Let me know if there's anything I can clarify on.

int main()
{

    //Passes through this sequence without any issues
    VideoCapture sequence("data/00001.HDR");
    if (!sequence.isOpened())
    {
        cerr << "Failed to open Image Sequence!\n" << endl;
        return -6;
    }

    double FPS = 60.0;
    int wid = sequence.get(CV_CAP_PROP_FRAME_WIDTH);
    int het = sequence.get(CV_CAP_PROP_FRAME_HEIGHT);

    VideoWriter oVideoWriter;
    oVideoWriter.open("data/MyVideo.avi", CV_FOURCC('F', 'M', 'P', '4'), FPS, Size(wid, het), true);

    //This is where something goes wrong and the program returns a -1
    if (!oVideoWriter.isOpened())
    {
        cout << "ERROR: Failed to write the video" << endl;
        return -1;
    }

    //Larger body of the remainder of the function
    ...

    return 1;
}

Exporting video from image sequence

Hey guys,

So I'm working on a project that involves the user outputting a series of images that I would like to combine into a single video. Right now, these images are exported as HDR files, but I have tried this with a png sequence as well and run into the same issue. I'm running into an issue with combining these into a video. The images are read in fine, but when I try to open the VideoWriter, something isn't working.

I'm not sure if it has to do with the codec that I'm trying to use (I'll admit to a slight bit of ignorance when it comes to those) or something else entirely. The program does output an empty .AVI video of 0kb, which is obviously wrong.

Any help would be appreciated, so thanks in advance. Let me know if there's anything I can clarify on.

int main()
{

    //Passes through this sequence without any issues
    VideoCapture sequence("data/00001.HDR");
    if (!sequence.isOpened())
    {
        cerr << "Failed to open Image Sequence!\n" << endl;
        return -6;
    }

    double FPS = 60.0;
    int wid = sequence.get(CV_CAP_PROP_FRAME_WIDTH);
    int het = sequence.get(CV_CAP_PROP_FRAME_HEIGHT);

    VideoWriter oVideoWriter;
    oVideoWriter.open("data/MyVideo.avi", CV_FOURCC('F', 'M', 'P', '4'), FPS, Size(wid, het), true);

    //This is where something goes wrong and the program returns a -1
    if (!oVideoWriter.isOpened())
    {
        cout << "ERROR: Failed to write the video" << endl;
        return -1;
    }

    //Larger body of the remainder of the function
    ...

    return 1;
}

Final Solution

This was my final solution, based off of the comment string below.

int main()
{   
    VideoCapture sequence("data/00001.HDR", CAP_IMAGES);

    if (!sequence.isOpened())
    {
        cerr << "Failed to open Image Sequence!\n" << endl;
        return -6;
    }

    double FPS = 60.0;
    int wid = sequence.get(CV_CAP_PROP_FRAME_WIDTH);
    int het = sequence.get(CV_CAP_PROP_FRAME_HEIGHT);

    VideoWriter oVideoWriter;
    oVideoWriter.open("data/MyVideo.avi", CV_FOURCC_DEFAULT, FPS, Size(wid, het));

    if (!oVideoWriter.isOpened())
    {
        cerr << "ERROR: Failed to write the video" << endl;
        return -1;
    }

    Mat frame8;
    Mat frame32;

    try {
        do
        {
            sequence >> frame32;
            if (frame32.empty())
                break;

            frame32.convertTo(frame8, CV_8UC3, 550, 25);

            oVideoWriter.write(frame8);

        } while (!frame32.empty());
    }
    catch (Exception ex) {
        cout << ex.code << " " << ex.msg << endl;
        cin.get();
    }

    return 1;
}