Ask Your Question

Revision history [back]

Problems writing multiple .avi files

Hi everyone!

I'm using the latest 2.4 branch withou ffmpeg and Ubuntu 14.04 and I don't have at all ffmpeg in my Virtual Machine.

My previous goal was to write .mp4 video files, but now the challenge is to write files with AVI container and with XVID codec.

I was commenting in this post: http://answers.opencv.org/question/32287/ubuntu-opencv-videowriter-using-ffmpeg-with-could-not-open-codec-libx264-error/#53914

So now I'm having problems with the property FPS.

[avi @ 0x8e14540] time base not set
Could not open the output video for write: /home/margarida/Output.avi

My .get doesn't work, so how can I get the correct FPS automatically?

My code is this:

int main(int argc, char* argv[]) {
    VideoCapture cap;
    VideoWriter outputVideo;
    string name;
    Mat frame;
    int j;

        for (j = 1; j < argc; j++) {

            printf("%d: %s\n", j, argv[j]);

            string str1 = argv[j];
            unsigned found = str1.find_last_of("/");
            name = str1.substr(0, found);
            string newname = name + "/Output.avi";
            cout << newname << '\n';

            cap.open(argv[j]);
            if (!cap.isOpened()) {
                cout << "Could not open the input video: " << argv[j] << endl;
                return -1;
            }

            //codec type
            int ex = CV_FOURCC('X', 'V', 'I', 'D');
            //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 };

            cout << "Input Codec: " << EXT << endl;

            //acquire input size
            Size S = Size((int) cap.get(CV_CAP_PROP_FRAME_WIDTH),
                    (int) cap.get(CV_CAP_PROP_FRAME_HEIGHT));

            //get fps
            double fps = cap.get(CV_CAP_PROP_FPS);

            // Open the output
            outputVideo.open(newname, ex, fps, S, true);

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

            while (true) {
                cap >> frame;
                if (frame.empty()) {
                    cerr << "didnt catch frame" << endl;
                    break;
                }

                imshow("OUTPUT", frame);

                outputVideo.write(frame);

            }
        }

    return 0;
}