Ask Your Question
0

why video is not playing only one frame showing

asked 2018-06-06 15:04:50 -0600

Kaleem gravatar image

updated 2018-06-07 00:11:18 -0600

berak gravatar image

//PlayVideoFilecpp.sIn

//main.cpp

#include<opencv2\core\core.hpp>

#include<opencv2\highgui\highgui.hpp>

#include<opencv2\imgproc\imgproc.hpp>

#include<iostream>

#include<conio.h>          //it may be necessary to change or remove this line if not using windos
//////////////////

int main(void) {

    cv::VideoCapture capVideo;

    cv::Mat imgFrame;

    capVideo.open("768x576.avi");

    if (!capVideo.isOpened()) {

        std::cout << "\nerror reading video file" << std::endl << std::endl;

        _getch();


        return(0);
    }

    if (capVideo.get(CV_CAP_PROP_FRAME_COUNT) < 1) {

        std::cout << "\nerror:video file must have at least one frame";

        _getch();

        return(0);

    }

    capVideo.read(imgFrame);

    char chCheckForEscKey = 0;

    while (capVideo.isOpened() && chCheckForEscKey != 27) {

        cv::imshow("imgFrame", imgFrame);

        if ((capVideo.get(CV_CAP_PROP_POS_FRAMES) + 1) < capVideo.get(CV_CAP_PROP_FRAME_COUNT)) {


            capVideo.read(imgFrame);
        }
        else

        {


            std::cout << "end of video\n";

            break;
        }


        //chCheckForEscKey = cv::waitkey(1);
    }

    if (chCheckForEscKey != 27) {

        cv::waitKey(0);




    }
    return(0);
}

code is working okay but it only show frame why not playing whole video ?

edit retag flag offensive close merge delete

Comments

1

cv::waitKey(0) will hang until a key is pressed. Try a value larger than 0. Maybe you know that, but I can't tell with how poorly formatted your code is.

Der Luftmensch gravatar imageDer Luftmensch ( 2018-06-06 15:18:26 -0600 )edit

i increase the value but still no effect

Kaleem gravatar imageKaleem ( 2018-06-06 15:24:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-06-07 00:26:30 -0600

berak gravatar image

you overcomplicated it (main noob problem).

you also should NOT rely on things like CAP_PROP_FRAME_COUNT, depending on your hw/os/backend/codec it might not be supported. also, those _getch() calls are non-portable, and will be terribly annoying, once you run your program outside your ide, get rid of it.

#include<opencv2/opencv.hpp> // please use opencv3 headers and *forward* slashes

#include<iostream>


int main(void) {

    cv::VideoCapture capVideo;
    capVideo.open("768x576.avi");

    if (!capVideo.isOpened()) {
        std::cout << "\nerror reading video file" << std::endl << std::endl;
        return(0);
    }

    cv::Mat imgFrame;
    while ( capVideo.read(imgFrame) ) { // will stop at the last frame

        cv::imshow("imgFrame", imgFrame);

        int k = cv::waitKey(10);
        if (k == 27) // esc. pressed
            break;
    }
    return(0);
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-06-06 15:04:50 -0600

Seen: 577 times

Last updated: Jun 07 '18