Given below is the code that I am using for getting the video from my webcam and saving it on my hard disk. On running the program it says "Video writer is not opening". Where am I going wrong?
#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
#define default_buflen 1024
using namespace std;
using namespace cv;
#define default_port "1234"
int main(int argc, char** argv)
{
Mat capture;
VideoCapture cap(0);
if(!cap.isOpened())
{
cout<<"Cannot connect to camera"<<endl;
getchar();
return -1;
}
double fps=30;
Size s=Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),(int)cap.get(CV_CAP_PROP_FRAME_WIDTH));
VideoWriter vidcapt;
vidcapt.open("c:\\out.avi",CV_FOURCC('D','I','V','X'),cap.get(CV_CAP_PROP_FPS),s,true);
if(!vidcapt.isOpened())
{
cout<<"Video writer not opening"<<endl;
getchar();
return -1;
}
while(true)
{
cap>>capture;
namedWindow("Display",1);
imshow("Display",capture);
vidcapt<<capture;
int ch=waitKey(5);
if(char(ch)==27)
{
break;
}
}
}
I have read the answer given here and here but am not understanding where I am going wrong.
Try other codecs
CV_FOURCC('P','I','M','1') = MPEG-1 codec
CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
Copy pasted from here . I managed to write video with CV_FOURCC('F', 'L', 'V', '1').
By the way, codecs should be installed on your machine of course.
As per your code I can not understand why you created window Display
every time in while loop, Also you initialize the VideoWriter
object every time it may be wrong. I have slightly modified your code as below please try, it may it help you
#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
#define default_buflen 1024
using namespace std;
using namespace cv;
#define default_port "1234"
int main(int argc, char** argv)
{
Mat capture;
VideoCapture cap(0);
if(!cap.isOpened())
{
cout<<"Cannot connect to camera"<<endl;
getchar();
return -1;
}
namedWindow("Display",CV_WINDOW_AUTOSIZE);
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("c:\\out.avi", CV_FOURCC('P','I','M','1'), 20, frameSize,true);
if ( !oVideoWriter.isOpened() )
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while(true)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("Display", frame);
if (waitKey(10) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.