使用Qt示例代码实现的视频播放。VS项目

2 篇文章 0 订阅

最近越发感到Qt的强大,简单易用!

从最开始接触是在今年4月底,花了半个月时间使用Qt和QtitanRibbon,开发了一个程序框架,类似于office风格那种。

当时以为自己好厉害,现在相信全是因为Qt太强大(傻笑~~)

好的库确实可以造福万民啊!(程序员从业人员已经超过千万了)

言归正传,项目需要,要做一个视频播放功能,要求支持h264、mpg4等等格式。

借助Qt示例代码,半天搞定了。。。   速度快的很大一部分原因:作为程序中的非关键功能,基本没要求,稍微改下Qt示例代码就好了。Qt实现的功能比预想的还周全。(美滋滋~)

程序运行截图如下:

可以设置播放速度、显示播放列表,画面颜色控制等等。

开发环境:VS2013、Qt5.3.1、qt-vs-adin1.2.5

源码下载链接:http://download.csdn.net/download/jingmiaa/10135083




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了使更多的Qt初学者能尽快入门Qt,也为了QtQt Creator的快速普及,我们花费大量精力写出了这一系列教程。虽然教程的知识可能很浅显,虽然教程的语言可能不规范,但是它却被数十万网友所认可。我们会将这一系列教程一直写下去,它将涉及Qt的方方面面 一、Qt Creator的安装和hello world程序的编写 二、Qt Creator编写多窗口程序 三、Qt Creator登录对话框 四、Qt Creator添加菜单图标 五、Qt Creator布局管理器的使用 六、Qt Creator实现文本编辑 七、Qt Creator实现文本查找 八、Qt Creator实现状态栏显示 九、Qt Creator中鼠标键盘事件的处理实现自定义鼠标指针 十、Qt Creator中实现定时器和产生随机数 十一、Qt 2D绘图(一)绘制简单图形 十二、Qt 2D绘图(二)渐变填充 十三、Qt 2D绘图(三)绘制文字 十四、Qt 2D绘图(四)绘制路径 十五、Qt 2D绘图(五)显示图片 十六、Qt 2D绘图(六)坐标系统 十七、Qt 2D绘图(七)Qt坐标系统深入 十八、Qt 2D绘图(八)涂鸦板 十九、Qt 2D绘图(九)双缓冲绘图简介 二十、Qt 2D绘图(十)图形视图框架简介 二十一、Qt数据库(一)简介 二十二、Qt数据库(二)添加MySQL数据库驱动插件 二十三、Qt数据库(三)利用QSqlQuery类执行SQL语句(一) 二十四、Qt数据库(四)利用QSqlQuery类执行SQL语句(二) 二十五、Qt数据库(五)QSqlQueryModel 二十六、Qt数据库(六)QSqlTableModel 二十七、Qt数据库(七)QSqlRelationalTableModel 二十八、Qt数据库(八)XML(一) 二十九、Qt数据库(九)XML(二) 三十、Qt数据库(十)XML(三) 三十一、Qt 4.7.0及Qt Creator 2.0 beta版安装全程图解 三十二、第一个Qt Quick程序(QML程序) 三十三、体验QML演示程序 三十四、Qt Quick Designer介绍 三十五、QML组件 三十六、QML项目之Image和BorderImage 三十七、Flipable、Flickable和状态与动画 三十八、QML视图 三十九、QtDeclarative模块 四十、使用Nokia Qt SDK开发Symbian和Maemo终端软件 四十一、Qt网络(一)简介 四十二、Qt网络(二)HTTP编程 四十三、Qt网络(三)FTP(一) 四十四、Qt网络(四)FTP(二) 四十五、Qt网络(五)获取本机网络信息 四十六、Qt网络(六)UDP 四十七、Qt网络(七)TCP(一) 四十八、Qt网络(八)TCP(二)
以下是一个简单的视频转字符动画的 QT 项目示例,其中使用多线程实现视频帧转换为字符图像的过程: mainwindow.h 文件: ```cpp #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QThread> #include <QMutex> #include <QWaitCondition> #include <QTimer> #include <QVector> namespace Ui { class MainWindow; } class VideoConverter : public QThread { Q_OBJECT public: explicit VideoConverter(QObject *parent = nullptr); void setFileName(const QString& fileName); signals: void frameConverted(const QString& frame); protected: void run() override; private: QString m_fileName; QMutex m_mutex; QWaitCondition m_waitCond; }; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_selectButton_clicked(); void on_startButton_clicked(); void on_stopButton_clicked(); void on_frameConverted(const QString& frame); void on_timer_timeout(); private: Ui::MainWindow *ui; VideoConverter m_converter; QVector<QString> m_frames; QMutex m_mutex; QWaitCondition m_waitCond; QTimer m_timer; }; #endif // MAINWINDOW_H ``` mainwindow.cpp 文件: ```cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <opencv2/opencv.hpp> VideoConverter::VideoConverter(QObject *parent) : QThread(parent) { } void VideoConverter::setFileName(const QString &fileName) { m_fileName = fileName; } void VideoConverter::run() { cv::VideoCapture cap(m_fileName.toStdString()); if (!cap.isOpened()) { qDebug() << "Cannot open video file"; return; } cv::Mat frame; while (cap.read(frame)) { // Convert video frame to ASCII art QString ascii = ""; for (int y = 0; y < frame.rows; y += 5) { for (int x = 0; x < frame.cols; x += 2) { cv::Vec3b color = frame.at<cv::Vec3b>(y, x); int gray = 0.2989 * color[2] + 0.5870 * color[1] + 0.1140 * color[0]; ascii += QString::fromUtf8(" .-:=+*#%@")[gray / 25]; } ascii += "\n"; } // Emit signal to notify main thread that a frame has been converted m_mutex.lock(); m_waitCond.wait(&m_mutex); emit frameConverted(ascii); m_mutex.unlock(); } } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(&m_converter, &VideoConverter::frameConverted, this, &MainWindow::on_frameConverted); connect(&m_timer, &QTimer::timeout, this, &MainWindow::on_timer_timeout); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_selectButton_clicked() { QString fileName = QFileDialog::getOpenFileName(this, "Select Video File", "", "Video Files (*.avi *.mp4)"); if (!fileName.isEmpty()) { ui->fileNameEdit->setText(fileName); } } void MainWindow::on_startButton_clicked() { QString fileName = ui->fileNameEdit->text(); if (fileName.isEmpty()) { return; } m_converter.setFileName(fileName); m_converter.start(); m_timer.start(100); // Show frames every 100 ms } void MainWindow::on_stopButton_clicked() { m_converter.terminate(); m_converter.wait(); m_timer.stop(); ui->frameLabel->clear(); m_frames.clear(); } void MainWindow::on_frameConverted(const QString &frame) { m_mutex.lock(); m_frames.append(frame); m_waitCond.wakeAll(); m_mutex.unlock(); } void MainWindow::on_timer_timeout() { if (!m_frames.isEmpty()) { ui->frameLabel->setText(m_frames.takeFirst()); } } ``` 在该示例中,VideoConverter 类继承自 QThread,并重写了 run() 函数,用于处理视频帧的转换工作。具体地,run() 函数中使用了 OpenCV 库读取视频文件,并将每一帧转换为 ASCII 码表示的字符图像。转换后的字符图像通过信号 frameConverted 发送给主线程。在主线程中,使用 QVector 容器存储转换好的字符图像,并使用 QTimer 控制字符图像的展示顺序。同时,在主线程中通过连接信号和槽的方式,将 VideoConverter 的 frameConverted 信号与 MainWindow 的 on_frameConverted 槽函数连接起来,以便在接收到转换好的字符图像时更新界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值