Ask Your Question

Writing Video to File

asked 2015-01-12 11:36:57 -0500

kingpin gravatar image

Hi im trying to write a video from my webcam to my computer but I keep getting the error that my writer isnt opened. Im using windows 8 64 bit, VS 2013 & OpenCV 2.4.10. Here is the code that I am using:

#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <iostream>

using namespace cv;
using namespace std;

string intToString(int number){

    std::stringstream ss;
    ss << number;
    return ss.str();

int main(int argc, char* argv[])

    VideoCapture cap(0); // open the video camera no. 0

    VideoWriter writer;

    if (!cap.isOpened())  // if not success, exit program
        cout << "ERROR INITIALIZING VIDEO CAPTURE" << endl;
        return -1;

    char* windowName = "Webcam Feed";
    namedWindow(windowName, CV_WINDOW_AUTOSIZE); //create a window to display our webcam feed

    string filename = "C:\\thevideo.avi";
    int fcc = CV_FOURCC('D', 'I', 'V', '3');
    double fps = 20;
    cv::Size frameSize(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));

    writer = VideoWriter(filename, fcc, fps, frameSize);

    if (!writer.isOpened())

        cout << "the writer isnt opened" << endl;
        return -1;

    while (1) {
        Mat frame;

        bool bSuccess =; // read a new frame from camera feed

        if (!bSuccess) //test if frame successfully read
            cout << "ERROR READING FRAME FROM CAMERA FEED" << endl;


        imshow(windowName, frame); //show the frame in "MyVideo" window

        //listen for 10ms for a key to be pressed
        switch (waitKey(10)){

        case 27:
            //'esc' has been pressed (ASCII value for 'esc' is 27)
            //exit program.
            return 0;



    return 0;


Can anyone help me?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-01-12 15:41:08 -0500

updated 2015-01-12 15:41:57 -0500

Windows 8 has a lot of security restrictions by default, including for system partition root path, so you'd better try to write to you project directory "./thevideo.avi" .

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-01-12 11:36:57 -0500

Seen: 418 times

Last updated: Jan 12 '15