Saturday, June 25, 2016

Writing video to File using OpenCV

Posted by Md. Hanif Ali Sohag on Saturday, June 25, 2016 in | No comments
Writing video to file using Opencv
In this tutorial you will learn how to write video file using OpenCV


#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
	// 0 is the ID of the built-in laptop camera, change if you want to use other camera
	VideoCapture cap(0);
	//check if the file was opened properly
	if (!cap.isOpened())
		cout << "Capture could not be opened succesfully" << endl;
		return -1;
	// Get size of frames
	Size S = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH), (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
	// Make a video writer object and initialize it at 30 FPS
	VideoWriter put("c://test//output.mpg", CV_FOURCC('M', 'P', 'E', 'G'), 30, S);
	if (!put.isOpened())
		cout << "File could not be created for writing. Check permissions" << endl;
		return -1;
		// Play the video in a loop till it ends
	while (char(waitKey(1)) != 'q' && cap.isOpened())
		Mat frame;
		cap >> frame;
		// Check if the video is over
		if (frame.empty())
			cout << "Video over" << endl;
		imshow("Video", frame);
		put << frame;
	return 0;



Post a Comment