如何在OpenCV 2.4.3中编写视频文件 [英] How to write video file in OpenCV 2.4.3

查看:231
本文介绍了如何在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();

这篇关于如何在OpenCV 2.4.3中编写视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆