Ask Your Question

Revision history [back]

here a small program how I would play play a video :

void main(void)
{
    VideoCapture vRead;
    vRead.open("G:/Lib/opencv/samples/data/Megamind.avi");
    if (!vRead.isOpened())
    {
        cout<<"File not found";
        return ;
    }
    double xFps;
    xFps = vRead.get(CV_CAP_PROP_FPS);
    cout << "FPS = " << xFps << "\n";
    double x = vRead.get(CV_CAP_PROP_FRAME_COUNT);
    cout << "# frame = " << x << "\n";
    int fourcc = vRead.get(CV_CAP_PROP_FOURCC);
    string fourcc_str = format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
    cout<<"Fourcc "<< fourcc_str<<endl;
    cout<<"Time to play : "<< x/xFps<<" s\n";
    TickMeter t;
    t.start();
    bool test=true;
    Mat frame;
    // FIRST METHOD too fast read frame and don't care of time
    while (test)
    {
        vRead>> frame;
        if (frame.empty())
            test=false;
        else
            imshow("video",frame);
        waitKey(1);
    }
    t.stop();
    cout<<"Play video in "<<t.getTimeMilli()<<" ms\n";
    vRead.set(CV_CAP_PROP_POS_FRAMES,0);
    test = true;
    int nbFrame=0;
    t.reset();
     // SECOND METHODtake care of timing  but too slow because vRead an imshow timing is neglected
    t.start();
    while (test)
    {
        vRead >> frame;
        if (frame.empty())
            test = false;
        else
            imshow("video", frame);
        nbFrame++;
        waitKey(1000/xFps);
    }
    t.stop();
    cout << "Play video in " << t.getTimeMilli() << " ms\n";

here a small program how I would play play a video :

void main(void)
{
    VideoCapture vRead;
    vRead.open("G:/Lib/opencv/samples/data/Megamind.avi");
    if (!vRead.isOpened())
    {
        cout<<"File not found";
        return ;
    }
    double xFps;
    xFps = vRead.get(CV_CAP_PROP_FPS);
    cout << "FPS = " << xFps << "\n";
    double x = vRead.get(CV_CAP_PROP_FRAME_COUNT);
    cout << "# frame = " << x << "\n";
    int fourcc = vRead.get(CV_CAP_PROP_FOURCC);
    string fourcc_str = format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
    cout<<"Fourcc "<< fourcc_str<<endl;
    cout<<"Time to play : "<< x/xFps<<" s\n";
    TickMeter t;
    t.start();
    bool test=true;
    Mat frame;
    // FIRST METHOD too fast read frame and don't care of time
    while (test)
    {
        vRead>> frame;
        if (frame.empty())
            test=false;
        else
            imshow("video",frame);
        waitKey(1);
    }
    t.stop();
    cout<<"Play video in "<<t.getTimeMilli()<<" ms\n";
    vRead.set(CV_CAP_PROP_POS_FRAMES,0);
    test = true;
    int nbFrame=0;
    t.reset();
     // SECOND METHODtake care of timing  but too slow because vRead an imshow timing is neglected
    t.start();
    while (test)
    {
        vRead >> frame;
         if (frame.empty())
            test = false;
        else
        {
            nbFrame++;
            vRead >> frame;
            if (frame.empty())
                      test = false;                
            else
                imshow("video", frame);
         nbFrame++;
        waitKey(1000/xFps);
     waitKey(2000/xFps);
        }
    }
    t.stop();
    cout << "Play video in " << t.getTimeMilli() << " ms\n";