如何在 OpenCV 2.4.3 中编写视频文件
我使用 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?
推荐答案
问题可能在于您使用的编解码器.
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();
相关文章