Skip to content

Instantly share code, notes, and snippets.

@khaledsaikat
Last active December 29, 2015 15:39
Show Gist options
  • Save khaledsaikat/7691960 to your computer and use it in GitHub Desktop.
Save khaledsaikat/7691960 to your computer and use it in GitHub Desktop.
OpenCV video write to disk. (VideoCapture.read() method is not somehow working so using CvCapture)
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
CvCapture *camCapture;
string filePath = "/Users/saikat/Desktop/MyVideo.avi";
if (!(camCapture = cvCaptureFromCAM(CV_CAP_ANY))) {
cout << "Failed to capture from camera" << endl;
return -1;
}
cout << "Camera opened successfully" << endl;
double width = cvGetCaptureProperty(camCapture, CV_CAP_PROP_FRAME_WIDTH);
double height = cvGetCaptureProperty(camCapture, CV_CAP_PROP_FRAME_HEIGHT);
cout << "Frame Size = " << width << "x" << height << endl;
Size frameSize(static_cast<int>(width), static_cast<int>(height));
VideoWriter videoWriter (filePath, CV_FOURCC('M','I','V','3'), 20, frameSize, true); //initialize the VideoWriter object
if( !videoWriter.isOpened() ){
cout << "Error: Writing failed" << endl;
return -1;
}
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);
IplImage *cameraFrame;
while (true) {
Mat frame;
if ((cameraFrame = cvQueryFrame(camCapture))) {
Mat frame(cameraFrame);
videoWriter.write(frame);
imshow("MyVideo", frame); //show the frame in "MyVideo" window
}
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
cout << "Done" << endl;
cvReleaseCapture(&camCapture);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment