无法使用opencv将视频写入文件

时间:2015-10-31 00:21:57

标签: c++ visual-studio opencv

我试图在visualstudio 2013中使用C ++中的OpenCV将捕获的视频写入文件。程序似乎从我的笔记本电脑上的网络摄像头捕获视频,并且还能够将每个帧保存为图像但是当我将帧写入时一个视频文件,我最终得到一个6kb的文件。程序没有错误,因为我遵循了OpenCV文档中的指令。

我正在粘贴该计划以供审核。请建议我如何使它成为一个成功的计划。

谢谢。

#include <stdio.h>
#include <iostream>
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"

using namespace std;
using namespace cv;

int main()
{
    VideoCapture video_capture(0);

    if (!video_capture.isOpened())
    {
        cout << "Error in opening video feed!" << endl;
        getchar();
        return -1;
    }

    // Creating the window to view video feed
    String window_name = "Video_Feed";
    namedWindow(window_name, CV_WINDOW_AUTOSIZE);

    //
    Mat frame;

    // Filename
    String filename = "...\\first_recording.avi";

    // four character code
    int fcc = CV_FOURCC('M', 'P', '4', '2');

    // frames per sec
    int fps = 10;

    // frame size
    Size frame_size(CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT);

    VideoWriter video_writer = VideoWriter(filename,fcc,fps,frame_size,true);

    if (!video_writer.isOpened())// || video_writer.isOpened == NULL)
    {
        cout << "Error in opening video writer feed!" << endl;
        getchar();
        return -1;
    }

    int frame_count = 0;

    while (frame_count < 100)
    {
        bool cap_success = video_capture.read(frame);

        if (!cap_success)
        {
            cout << "Error in capturing the image from the camera feed!" << endl;
            getchar();
            break;
        }

        imshow(window_name, frame);
        //imwrite("cap.jpg", frame);
        video_writer.write(frame);

        switch (waitKey(10))
        {
        case 27:
            return 0;
            break;
        }
        frame_count++;
    }

    //scvReleaseVideoWriter;
    destroyWindow(window_name);
    return 0;

}

1 个答案:

答案 0 :(得分:1)

请找下面的代码来编写视频文件。

 int main(int argc, char* argv[])
    {
        // Load input video
        cv::VideoCapture input_cap("test8.avi");
        if (!input_cap.isOpened())
        {
            std::cout << "!!! Input video could not be opened" << std::endl;
            return -1;
        }

    // Setup output video
    cv::VideoWriter output_cap("output.avi",  
                               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 -1;
    }

    // Loop to read frames from the input capture and write it to the output capture
    cv::Mat frame;
    while (true)
    {       
        if (!input_cap.read(frame))             
            break;

        output_cap.write(frame);
    }

    // Release capture interfaces   
    input_cap.release();
    output_cap.release();

    return 0;
}