Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write video file in OpenCV 2.4.3

I am using OpenCV 2.4.3 to read and write a video file. My code is like this:

cv::VideoCapture video;
video.open ( "D:\\testVideo.avi" );
cv::VideoWriter output;
output.open ( "D:\\outputVideo.avi", CV_FOURCC('D','I','V','X'), 120, cv::Size ( 1200,1600), true );
cv::Mat img;

for ( int n = 0; ; n ++ )
{
     video >> img;
     output.write ( img );
 }

Then the result video was an empty file, and I couldn't open it. What did I do wrong here?

like image 828
E_learner Avatar asked Nov 29 '12 10:11

E_learner


2 Answers

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();
like image 179
karlphillip Avatar answered Nov 16 '22 12:11

karlphillip


You declare cv::VideoWriter output of frame size 1200*1600. So resize the frame to 1200*1600 using cv::resize(img,img,cv::Size(1200,1600));before output.write(img);

like image 5
Saikat Avatar answered Nov 16 '22 12:11

Saikat