Hi,
I’m would like to save a video obtained from another video after applying some filters and functions.
My problem is that, once I saved the video, I’m unable to read it using a media player (Windows Media Player can’t open it and VLC just start and don’t display anything) .
Here is my code :
int videoProcessing(char videoPath[], char destVideoPath[], int thresh,bool save)
{
vector<vector<Point>> contours;
VideoCapture cap(videoPath, CAP_ANY);
VideoWriter out;
Mat imgOut;
Mat imgBin;
Size frameSize;
// Check if camera opened successfully
if (!cap.isOpened())
{
cout << "Error opening video stream or file" << endl;
return -1;
}
while (cap.isOpened())
{
Mat frame;
cap >> frame;
if (save == true && !out.isOpened())//If we decide to record the output
{
out.open(destVideoPath, VideoWriter::fourcc('M', 'J', 'P', 'G'), (double)40, frame.size(), false);
}
if (frame.empty())
{
cap.release();
destroyAllWindows();
return 0;
}
else
{
if(toBinary(&frame,&imgBin,&thresh) == 0)
{
shapeDetect(&imgBin,&imgOut,&contours);
out.write(frame);
imshow("Video", imgOut);
waitKey(25);
}
else
{
imshow("Video", frame);
waitKey(25);
}
}
// Press ESC on keyboard to exit
char c = (char)waitKey(25);
if (c == 27) break;
}
cap.release();
destroyAllWindows();
return 0;
}
I looked at this example to write mine. The file I create ends with the .avi extension.
I think the problem might come from the codec, I don’t really understand how to work with codecs.
Should I have one installed on my computer ? Or do I have to choose one depending on my OS ?
I’m working on Windows 10 and I use the lastest OpenCV version 4.6.0