0

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.

marivez
  • 15
  • 5
  • 1
    The second parameter you pass to `cv::VideoCapture` constructor doesn't make any sense. It should be a value from [`enum cv::VideoCaptureAPIs`](https://docs.opencv.org/4.6.0/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d) – Dan Mašek Dec 01 '22 at 13:00
  • @DanMašek thank you, I removed that part and added cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc(....)); but that didn't solve the problem – marivez Dec 02 '22 at 09:02

1 Answers1

0

Hi here is a working code using some of your logic, hope it helps, i tested on opencv4 running ubuntu 20.04, should work fine on windows.

//Compile with:
            //g++ example.cpp -o salida `pkg-config --cflags --libs opencv4`
            //Ruben Estrada Marmolejo
            //ruben.estrada@hetpro.com.mx
            //Original idea: https://stackoverflow.com/questions/44752240/how-to-remove-shadow-from-scanned-images-using-opencv/44752405#44752405 
            #include<opencv4/opencv2/cvconfig.h>
            #include<opencv2/core/core.hpp>
            #include<opencv2/ml/ml.hpp>
            //#include<opencv/cv.h>
            #include<opencv2/imgproc/imgproc.hpp>
            #include<opencv2/highgui/highgui.hpp>
            #include<opencv2/video/background_segm.hpp>
            #include<opencv2/videoio.hpp>
            #include<opencv2/imgcodecs.hpp>
            #include <iostream>

            cv::VideoCapture cap;

            int main(){

                cap.open("1.avi");
                int maxFrame = cap.get(cv::CAP_PROP_FRAME_COUNT);
                int frameWidth = cap.get(cv::CAP_PROP_FRAME_WIDTH);
                int frameHeight = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
                std::cout<<"Max frame: " << maxFrame << std::endl;
                std::cout<<"Frame widht: " << frameWidth << std::endl;
                std::cout<<"Frame height: " << frameHeight << std::endl;
                int position = 0;
                for(;;){
                    cv::Mat frame;
                    cap.set(cv::CAP_PROP_POS_FRAMES,position);
                cap >> frame;
                 std::cout<<"Current frame: " << position << " of: " << maxFrame << std::endl;
                position++;
                if(position >= maxFrame){
                    break;
                }
                
                imshow("Display window", frame);
                if (cv::waitKey(30) >= 0){
                    break;
                }
                }
                

                // Wait for a keystroke in the window
                
                return 0;

                


            }