I'm trying to get video resolution and count the frames of an .avi video file, but the program always returns zero values.
Here's my code:
cv::VideoCapture cap("something.avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
cout << "Resolution: " << cap.get(CAP_PROP_FRAME_WIDTH) <<
"x" << cap.get(CAP_PROP_FRAME_HEIGHT) << endl;
int counter = 0;
for (;;) {
cv::Mat frame;
cap >> frame;
if (frame.empty()) break;
cv::imshow("Preview", frame);
char c = (char)cv::waitKey(30);
if (c == 'q' || c == 'Q' || c == 27) break;
counter++;
}
cout << counter << endl;
return 0;
I tried adding the video codec info since a lot of problems like this seemed to have codec issues but doesn't seem to be the case here. What else can I try? I have the latest OpenCV installed on Win10 and Visual Studio 22.