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?
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();
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With