How to write video file in OpenCV 2.4.3

The problem might be the codec you are using.

A simple test to make sure your stuff is working properly is to simply retrieve frames from a webcam and write them on a video file:

// Load input video
cv::VideoCapture input_cap(argv[1]);
if (!input_cap.isOpened())
{
        std::cout << "!!! Input video could not be opened" << std::endl;
        return;
}

// Setup output video
cv::VideoWriter output_cap(argv[2], 
               input_cap.get(CV_CAP_PROP_FOURCC),
               input_cap.get(CV_CAP_PROP_FPS),
               cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH),
               input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));

if (!output_cap.isOpened())
{
        std::cout << "!!! Output video could not be opened" << std::endl;
        return;
}


// Loop to read from input and write to output
cv::Mat frame;

while (true)
{       
    if (!input_cap.read(frame))             
        break;

    output_cap.write(frame);
}

input_cap.release();
output_cap.release();

Leave a Comment