项目实战:Qt+OpenCV视频播放器(支持播放器操作,如暂停、恢复、停止、时间、进度条拽托等)

若该文为原创文章,未经允许不得转载
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/103235954

红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)

Qt开发专栏:项目实战(点击传送门)

OpenCV开发专栏(点击传送门)

 

需求

使用OpenCV库的视频播放器(支持播放器操作,如暂停、恢复、停止、时间、进度条拽托等)。

 

原理

       使用OpenCV打开视频文件,获取总帧数,根据当前帧数,刷新当前时间戳与预期的时间间隔,调用槽函数动态刷新播放内容。

 

注意

       当前只测试了avi文件。

 

相关博客

《OpenCV开发笔记(四):OpenCV图片和视频数据的读取与存储》

《项目实战:Qt视频播放器控件(不依赖系统编解码)》

《项目实战:Qt+OpenCV操作摄像头拍照、调节参数和视频录制》

《项目实战:Qt+OpenCV视频播放器(支持播放器操作,如暂停、恢复、停止、时间、进度条拽托等)》

《Qt实用技巧:使用QMediaPlayer和Windows自带组件播放swf、rmvb、mpg、mp4等视频文件》

《Qt实用技巧:使用QMediaPlayer播放mp4文件》

 

Demo:OpenCVPlayer v1.0.0

运行效果

下载地址

CSDN:https://download.csdn.net/download/qq21497936/11996319

 

核心类代码

OpenCVPlayerManager.h

#ifndef OPENCVPLAYERMANAGER_H
#define OPENCVPLAYERMANAGER_H

/************************************************************\
 * 控件名称: OpenCVPlayerManager,OpenCV管理类
 * 控件描述:
 *          1.OpenCV打开视频文件
 *          2.播放器操作:播放、暂停、停止
 *          3.播放时显示:当前时间、总时间
 *          4.设置当前播放的时间点
 *
 * 作者:红模仿    联系方式:QQ21497936
 * 博客地址:https://blog.csdn.net/qq21497936
 *       日期                版本               描述
 *   2019年11月25日      v1.0.0         opencv打开文件
\************************************************************/

#include 
#include 
#include 
#include 
// opencv
#include "opencv/highgui.h"
#include "opencv/cxcore.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"

class OpenCVPlayerManager : public QObject
{
    Q_OBJECT
public:
    enum PLAY_STATE {
        PLAY_STATE_PLAYING,
        PLAY_STATE_PAUSE,
        PLAY_STATE_STOP
    };

public:
    explicit OpenCVPlayerManager(QObject *parent = 0);
    ~OpenCVPlayerManager();

public:
    QString getWindowTitle() const;

public:
    void setWindowTitle(const QString &windowTitle);

signals:
    void signal_captureOneFrame(cv::Mat mat);           // 接收图像后抛出信号
    void signal_playStateChanged(OpenCVPlayerManager::PLAY_STATE playState);
                                                        // 播放器状态
    void signal_durationChanged(qint64 duration);       // 视频总长度
    void signal_positionChanged(qint64 position);       // 当前位置

public:
    bool startPlay(QString filePath, int width = 480, int height = 320);
    void pause();                       // 暂停
    void resume();                      // 恢复播放
    void stopPlay();                    // 停止播放
    void setPosition(qint64 position);  // 切换到播放位置

public slots:
    bool slot_start();                  // 开启线程
    bool stop();                        // 关闭线程

protected slots:
    void slot_captrueFrame();           // 消息循环获取图像
    void slot_stopPlay();               // 停止播放
    void slot_setPosition(qint64 position);

public:
    static QImage cvMat2QImage(const cv::Mat &mat);

private:
    cv::VideoCapture *_pVideoCapture;   // 播放文件实例
    QString _filePath;                  // 播放文件路径

    bool _running;                      // 线程是否运行

    int _totalFrames;                   // 总帧数
    int _fps;                           // 每秒帧数
    int _currentFrame;                  // 当前帧数
    int _width;                         // 高度
    int _height;                        // 宽度

    QElapsedTimer _elapsedTimer;        // 计时器
    qint64 _pauseMs;                    // 暂停的毫秒
    qint64 _duration;                   // 视频总长度(毫秒)

    PLAY_STATE _playState;              // 播放器状态

    int _position;                      // 改变到的播放位置
    bool _setPostion;                   // 播放位置修改标志

private:
    QString _windowTitle;
};
#endif // OPENCVPLAYERMANAGER_H

OpenCVPlayerManager.cpp

#include "OpenCVPlayerManager.h"

#include 

OpenCVPlayerManager::OpenCVPlayerManager(QObject *parent)
    : QObject(parent),
      _pVideoCapture(0),
      _totalFrames(0),
      _currentFrame(0),
      _playState(PLAY_STATE_STOP),
      _pauseMs(0),
      _duration(0)
{
    QString version = "v1.0.0";
    _windowTitle = QString("OpenCVPlayer %1(红模仿-红胖子 QQ21497936 博客地址: blog.csdn.net/qq21497936").arg(version);
}

OpenCVPlayerManager::~OpenCVPlayerManager()
{

}

bool OpenCVPlayerManager::startPlay(QString filePath, int width, int height)
{
    // 状态判断
    switch (_playState)
    {
    case PLAY_STATE_PAUSE:
        resume();
    case PLAY_STATE_PLAYING:
        return true;
        break;
    case PLAY_STATE_STOP:
        break;
    default:
        break;
    }
    if(!_pVideoCapture->open(filePath.toStdString()))
    {
        qDebug() << __FILE__ << __LINE__ << "Failed to start video :" << filePath;
        return false;
    }
    _width = width;
    _height = height;
    _pVideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, _width);
    _pVideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, _height);
    _width = _pVideoCapture->get(CV_CAP_PROP_FRAME_WIDTH);
    _height = _pVideoCapture->get(CV_CAP_PROP_FRAME_HEIGHT);
    _fps = _pVideoCapture->get(CV_CAP_PROP_FPS);
    _totalFrames = _pVideoCapture->get(CV_CAP_PROP_FRAME_COUNT);
    qDebug() << __FILE__ << __LINE__ << "width =" << width;
    qDebug() << __FILE__ << __LINE__ << "height =" << height;
    qDebug() << __FILE__ << __LINE__ << "_width =" << _width;
    qDebug() << __FILE__ << __LINE__ << "_height =" << _height;
    qDebug() << __FILE__ << __LINE__ << "_fps =" << _fps;
    qDebug() << __FILE__ << __LINE__ << "_totalFrames =" << _totalFrames;

    _currentFrame = 0;
    _pauseMs = 0;

    QTimer::singleShot(0, this, SLOT(slot_captrueFrame()));
    _elapsedTimer.start();

    _playState = PLAY_STATE_PLAYING;
    emit signal_playStateChanged(_playState);

    _duration = (_totalFrames - 1) * (1000.0f / _fps);
    emit signal_durationChanged(_duration);

    return true;
}

void OpenCVPlayerManager::pause()
{
    // 状态判断
    switch (_playState)
    {
    case PLAY_STATE_PAUSE:
    case PLAY_STATE_STOP:
        return;
        break;
    case PLAY_STATE_PLAYING:
        break;
    default:
        break;
    }
    _playState = PLAY_STATE_PAUSE;
    emit signal_playStateChanged(_playState);
}

void OpenCVPlayerManager::resume()
{
    // 状态判断
    switch (_playState)
    {
    case PLAY_STATE_PLAYING:
    case PLAY_STATE_STOP:
        return;
        break;
    case PLAY_STATE_PAUSE:
        break;
    default:
        break;
    }
    QTimer::singleShot(0, this, SLOT(slot_captrueFrame()));
    _elapsedTimer.start();
    _playState = PLAY_STATE_PLAYING;
    emit signal_playStateChanged(_playState);
}

void OpenCVPlayerManager::stopPlay()
{
    // 状态判断
    switch (_playState)
    {
    case PLAY_STATE_STOP:
        return;
        break;
    case PLAY_STATE_PAUSE:
    case PLAY_STATE_PLAYING:
        break;
    default:
        break;
    }
    QTimer::singleShot(0, this, SLOT(slot_stopPlay()));
}

void OpenCVPlayerManager::setPosition(qint64 position)
{
    QMetaObject::invokeMethod(this, "slot_setPosition", Q_ARG(qint64, position));
}

void OpenCVPlayerManager::slot_setPosition(qint64 position)
{
    // 当前帧数
    _currentFrame = position / (1000.0f / _fps);
    _pauseMs = _currentFrame * (1000.0f / _fps);
     _pVideoCapture->set(CV_CAP_PROP_POS_FRAMES, _currentFrame);
    _elapsedTimer.start();
    qDebug() << __FILE__ << __LINE__ 
             << _currentFrame << ":" << _totalFrames << ":" << _pauseMs;
}


bool OpenCVPlayerManager::slot_start()
{
    _pVideoCapture = new cv::VideoCapture;
    _running = true;
}

bool OpenCVPlayerManager::stop()
{
    if(_running)
    {
        _running = false;
        delete _pVideoCapture;
        _pVideoCapture = 0;
    }
}

void OpenCVPlayerManager::slot_captrueFrame()
{
    if(!_running)
    {
        return;
    }
    if(_pVideoCapture->isOpened())
    {
        // 没有下一帧,表示播放结束
        if(_currentFrame >= _totalFrames)
        {
            _playState = PLAY_STATE_STOP;
            emit signal_playStateChanged(_playState);
            cv::Mat mat(_height, _width, CV_8UC3, cv::Scalar(0, 0, 0));
            emit signal_captureOneFrame(mat);
            return;
        }
        cv::Mat mat;
        *_pVideoCapture >> mat;
        emit signal_captureOneFrame(mat);
        if(_currentFrame % _fps == 0)
        {
            emit signal_positionChanged(_currentFrame * (1000.0f / _fps));
        }
        _currentFrame++;
        int nowTime;
        switch (_playState)
        {
        case PLAY_STATE_STOP:
        case PLAY_STATE_PAUSE:
            _pauseMs = _currentFrame * (1000.0 / _fps);
            break;
        case PLAY_STATE_PLAYING:
            nowTime = _elapsedTimer.elapsed();
            if((_currentFrame * (1000.0 / _fps) - (nowTime + _pauseMs) < 0))
            {
                QTimer::singleShot(0, this, SLOT(slot_captrueFrame()));
            }else{
                QTimer::singleShot(_currentFrame * (1000.0 / _fps) 
                   - (nowTime + _pauseMs), this, SLOT(slot_captrueFrame()));
            }
            break;
        default:
            break;
        }
    }
}

void OpenCVPlayerManager::slot_stopPlay()
{
    if(_pVideoCapture->isOpened())
    {
        _playState = PLAY_STATE_STOP;
        _pVideoCapture->release();
        cv::Mat mat(_width, _height, CV_8UC3, cv::Scalar(0, 0, 0));
        emit signal_captureOneFrame(mat);
        _currentFrame = 0;
        emit signal_positionChanged(0);
        _duration = 0;
        emit signal_durationChanged(_duration);
    }
}


QImage OpenCVPlayerManager::cvMat2QImage(const cv::Mat &mat)
{
    // 8-bits unsigned, NO. OF CHANNELS = 1
    if(mat.type() == CV_8UC1)
    {
        QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
        // Set the color table (used to translate colour indexes to qRgb values)
        image.setColorCount(256);
        for(int i = 0; i < 256; i++)
        {
            image.setColor(i, qRgb(i, i, i));
        }
        // Copy input Mat
        uchar *pSrc = mat.data;
        for(int row = 0; row < mat.rows; row ++)
        {
            uchar *pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    // 8-bits unsigned, NO. OF CHANNELS = 3
    else if(mat.type() == CV_8UC3)
    {
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        QImage image2 = image.rgbSwapped();
        return image2;
    }
    else if(mat.type() == CV_8UC4)
    {
        qDebug() << "CV_8UC4";
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
//        return image.copy();
        return image;
    }
    else
    {
        qDebug() << "ERROR: Mat could not be converted to QImage.";
        QImage image;
        return image;
    }
}

QString OpenCVPlayerManager::getWindowTitle() const
{
    return _windowTitle;
}

void OpenCVPlayerManager::setWindowTitle(const QString &windowTitle)
{
    _windowTitle = windowTitle;
}

 

入坑

入坑一:调整播放位置时,宕机

原因:

       直接主线程操作设置当前帧的位置,同时子线程也却在读取,OpenCV内部没有错异步处理。

解决方法:

       先调用设置位置,然后槽调用,将设置位置放置到子线程当中去。


原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/103235954

你可能感兴趣的:(#,OpenCV,#,Qt实用技巧,项目实战,opencv播放器,播放器基本操作,opencv视频播放器)