Ask Your Question
0

VideoWriter Not Working on Some Macintosh Machines

asked 2013-11-14 14:56:10 -0600

Simon Tang gravatar image

I am trying to capture the video from the camera onto disk. I have a script that works on a Retina MacBook Pro 18-inch, Late 2013 but does not work on MacBook Pro 13-inch, Mid 2010. Both have similar environments: running on Maverick and have OpenCV 2.4.7 installed. However, I cannot get the older Macbook to write to disk correctly. The file size of output remains constant (414 bytes) and does not grow.

What is the problem that I'm having? What should I look into or check?

#include "opencv2/opencv.hpp"
using namespace cv;
#define fps 15

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    VideoWriter m_vw;

    bool m_isRecording = true;


    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);

    int skip = 30;
    int n=0;
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        std::cout << "skip: " << skip << std::endl;
        if (frame.empty()) {
            if(frame.rows != 480 && frame.cols != 640){ exit(0); }
            std::cout << "no frame" << std::endl;
            if (cap.grab()) {
                std::cout << "grab" << std::endl;
                if (cap.retrieve(frame)) {
                    imshow("edges", frame);
                }

            }
        } else if (skip-- > 0) {
            std::cout << "hi" << std::endl;
        } else {
            edges = frame;

            double width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
            double height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

            if (m_isRecording && !m_vw.isOpened()) {
                m_vw = cv::VideoWriter("~/ctrythis.avi", CV_FOURCC('M','J','P','G'), fps, cv::Size(width,height), true);
                std::cerr << m_vw.isOpened()<<std::endl;
            }
            putText( edges, "caption",
                    cv::Point( width/4, height/2),
                    CV_FONT_HERSHEY_COMPLEX, 1, cv::Scalar(255, 255, 255) );
            imshow("edges", edges);

            std::cerr << n++ << " " <<  m_vw.isOpened() << std::endl;
            m_vw << edges;
        }

        if(waitKey(1000/fps) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-11-18 16:37:08 -0600

I have a very similar issue, running XCode 5.0.2 on my MacBook Pro 13-inch (Mid 2010) with the latest OS X Mavericks, and openCV 2.4.7. I am trying to write a series of data matrices into a video. The avi file is created with a size of 286 bytes. Then, when I add a single image frame, the file size goes up to 414 bytes. This is far too small for the amount of data that I added to the video file (which makes me sure that the frame isn't added correctly to the video). Indeed, no matter how many frames I then add to the video, the file size remains at exactly 414 bytes until the code terminates. Here is my code:

string filename = "Video from data.avi";
int fps = 20;

VideoWriter *writer = new VideoWriter(filename,CV_FOURCC('M', 'J', 'P', 'G'),fps,cvSize(400,400));

for (int i=0; i<100; i++){

    Mat imgMat(400,400,CV_8UC3);

    (*writer).write(imgMat);
}

No matter what value I use to terminate the for-loop (currently i<100 in the code), the video file is created and remains at 414bytes. Obviously, none of my video players is able to read the file, and they crash or report errors when I try to open the avi file. I have tried the following to no effect: changing the fps to both high and low values; changing the initialisation of the matrix from nothing to various Scalar(r,g,b) values; changing the FOURCC codec to over 100 different values (most of which I am sure I don't have the codec for!).

Any help would be much appreciated! I'm sure I've done something silly...

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-11-14 14:56:10 -0600

Seen: 1,245 times

Last updated: Nov 18 '13