Animesh Pandey
Animesh Pandey

Reputation: 6018

Error in using cvVideoWriter in opencv?

This code snippet is supposed to save part of a video whose range is defined by start and end. There is an array of structures (data[i]) that holds the starting and end frame of a video shot in the original video. There are total of 8 shots.

for (int i = 0; i < finalCount-1; ++i) {
    capture = cvCaptureFromAVI("Stats\\Shots\\Cricketc1.avi");
    assert(capture);

    int frame_number = 0;
    int start = data[i].start_frame;
    int end   = data[i].end_frame;

    char shotname[100];
    strcpy_s(shotname, "shot_");
    char shot_id[30];
    _itoa_s(data[i].shot_no, shot_id, 10);
    strcat_s(shotname, shot_id);
    strcat_s(shotname, ".avi");

    IplImage* image = NULL;

    CvVideoWriter* writer = NULL;
    writer = cvCreateVideoWriter (shotname, CV_FOURCC('i','Y','U','V'), fps, cvSize(width, height), 1);
    assert(writer);
    while (frame_number >= start && frame_number < end) {
        image = cvQueryFrame(capture);
        assert(image);
        cvWriteFrame(writer, image);
    }
    cvReleaseImage(&image);
    cvReleaseVideoWriter(&writer);
    cvReleaseCapture(&capture);
    cout << shotname << " saved ..." << endl;
}

After running the program 8 video files are created that have a size of 6kb and do not run. I have tried various codecs like divx, mjpg, mpg2, iyuv etc but all give the same result.

Upvotes: 0

Views: 1362

Answers (2)

cyriel
cyriel

Reputation: 3522

If Gunther Fox answer won't help try to use different codec - it's very strange, but in my situation iyuv is not working at all and some other codecs works ok, but i can't read them while debugging... For me - ms video and radius cinepak always works fine(writing and reading), iyuv is not working at all, other codes - writing and reading, but not while debugging.

Upvotes: 1

Foggzie
Foggzie

Reputation: 9821

In your while loop, frame_number is never incremented. Since you say the program actually executes and creates the files this means nothing in your while loop ever runs... otherwise you'd get stuck in an infinite loop because frame_number will always be 0.

I would advise you initialize frame_number to start instead of 0 and there's no reason for it to exist outside of the scope of the loop so a for seems more appropriate:

int start = data[i].start_frame;
int end   = data[i].end_frame;

...

for (int frame_number = start; frame_number < end; frame_number++) {
    image = cvQueryFrame(capture);
    assert(image);
    cvWriteFrame(writer, image);
}

Upvotes: 2

Related Questions