Simple AVI player with OpenCV QT 4.8 (Playing video file with slider position)

I use iplimage in here but CV::Mat is always advisable.  We change position during video playback with qSlider.

demo
We need the following tools to create a simple player:

  • Qtimer 
  • CvCapture
  • QSlider
  • QFileDialog
  • PushButton

We need include following library

#include <QFileDialog>
#include <QDebug>
#include <QMessageBox>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/video.hpp>
#include <videoInput.h>
#include <QTimer>
#include <iostream>
    QString dirOpen;
    IplImage * imageRead;
    CvCapture* capture;
    int READCOUNT;
    int FPS_open,numFrames,frameW,frameH;
    QTimer * timerOpen;
READCOUNT = 0;

Declaration “Open File” button

void MainWindow::on_OpenFile_clicked()
{

dirOpen = QFileDialog::getOpenFileName(this, tr("Save File"),"/home/",tr("Videos (*.avi)"));

    if(dirOpen.isEmpty())
    {
        QMessageBox msgBox;
        msgBox.setText("open file");
        msgBox.exec();
        emit on_OpenFile_clicked();
    }else
    {
        ui->Play->setEnabled(true);
    }

}

Declaration “Play” button

void MainWindow::on_Play_clicked()
{
    ui->OpenFile->setEnabled(false);
    ui->Pause->setEnabled(true);
    ui->Stop->setEnabled(true);

capture = cvCaptureFromAVI(dirOpen.toStdString().c_str());

frameH    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
frameW    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
FPS_open  = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); // frame pes secund
numFrames = (int) cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT); // count frames

imageRead =  cvCreateImage(cvSize(frameW,frameH),IPL_DEPTH_8U,3);

ui->SliderFrame->setMaximum(numFrames);
ui->SliderSec->setMaximum( (int) (numFrames / FPS_open) );

    if(!cvGrabFrame(capture)){
        QMessageBox msgBox;
        msgBox.setText("Error");
        msgBox.exec();
    }else
    {
        ui->SliderFrame->setEnabled(true);
        READCOUNT = 0;
        timerOpen->start( (int) (1000/FPS_open) );
    }
}

Declaration “Pause” button

void MainWindow::on_Pause_clicked()
{

    ui->Play->setEnabled(true);
    ui->Pause->setEnabled(false);
    timerOpen->stop();
}

Declaration “Stop” button

void MainWindow::on_Stop_clicked()
{
    ui->Stop->setEnabled(false);
    ui->Play->setEnabled(false);
    ui->Pause->setEnabled(false);
    ui->OpenFile->setEnabled(true);
    dirOpen = "";

    timerOpen->stop();

    READCOUNT = 0;

    ui->SliderFrame->setEnabled(false);
    ui->SliderSec->setEnabled(false);
    cvReleaseImage(&imageRead);
    cvDestroyAllWindows();
}

Qtimer and Slider connection

timerOpen = new QTimer(this);
 connect(timerOpen,SIGNAL(timeout()),this,SLOT(Read()));

SliderFrameOn = true;

SliderSecOn   = true;

connect(ui->SliderFrame,SIGNAL(sliderPressed()),this,SLOT(SliderFramePress()));
connect(ui->SliderFrame,SIGNAL(sliderReleased()),this,SLOT(SliderFrameRelease()));
connect(ui->SliderFrame,SIGNAL(sliderMoved(int)),this,SLOT(SliderFrameMove(int)));

Head of SLOTs declaration

private slots:
    void Read();
    void SliderFramePress();
    void SliderFrameRelease();
    void SliderFrameMove(int);

Body of SLOTs declaration

void MainWindow::Read()
{
    READCOUNT++;

    ui->SliderFrame->setValue(READCOUNT);

    imageRead = cvQueryFrame(capture);
    cvShowImage("Play",imageRead);

    if(READCOUNT >= numFrames) // if avi ends
    {
        timerOpen->stop();
        ui->Stop->setEnabled(false);
        ui->Pause->setEnabled(false);
        ui->Play->setEnabled(true);
        READCOUNT = 0;
        cvReleaseImage(&imageRead);
        cvDestroyAllWindows();

    }
}
void MainWindow::SliderFramePress()
{

    timerOpen->stop();

}
void MainWindow::SliderFrameRelease()
{

        timerOpen->start((int) (1000/FPS_open));

}
void MainWindow::SliderFrameMove(int k)
{
    cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,k);

    imageRead = cvQueryFrame(capture);
    cvShowImage("Play",imageRead);

    READCOUNT = k;
}

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Leave a Reply