libVLC 视频界面分割

先看看分割后的界面吧,根据分割的数量,来分割视频画面。

其实视频界面分割很简单,看过叠加窗口的这篇文章,不难理解,如何分割。

libVLC 视频窗口上叠加透明窗口-CSDN博客

如果还是不懂的话,我讲解一下原理,其实就是在上面叠加透明窗口界面,在叠加的窗口上面调用

paintEvent绘制线条,很简单吧。

void TopWidget::paintEvent(QPaintEvent *pEvent)
{//创建QPainter对象,指明父对象,否则不能看见QPainter painter(this);//先画一条线QPen pen;//设置线宽pen.setWidth(10);//设置颜色pen.setColor(Qt::white);//设置线的样式 实线、虚线pen.setStyle(Qt::SolidLine);//设置线端点样式pen.setCapStyle(Qt::RoundCap);//设置画笔painter.setPen(pen);for (int i = 0; i < m_num; i++){//画竖线painter.drawLine(QPoint(width() * (i+1)/ (m_num+1), 0), QPoint(width() * (i + 1) / (m_num + 1), height()));//画横线painter.drawLine(QPoint(0, height() * (i + 1) / (m_num + 1)), QPoint(width(), height() * (i + 1) / (m_num + 1)));}}

更多QPainter相关的东西,请看:

Qt QPainter绘图_qt painter-CSDN博客

还是像之前的文章一样,先创建一个叠加界面,设置窗体无边框和透明。

	//窗口无边框setWindowFlags(Qt::FramelessWindowHint | Qt::Window);// 窗口透明this->setAttribute(Qt::WA_TranslucentBackground);

界面源码如下,ui上面没有任何控件。 

#pragma once#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include "ui_TopWidget.h"class TopWidget : public QWidget
{Q_OBJECTpublic:TopWidget(QWidget *parent = nullptr);~TopWidget();void setNum(int num);protected:void paintEvent(QPaintEvent *pEvent);private:Ui::TopWidgetClass ui;int m_num = 0;
};#include "TopWidget.h"TopWidget::TopWidget(QWidget *parent): QWidget(parent)
{ui.setupUi(this);//窗口无边框setWindowFlags(Qt::FramelessWindowHint | Qt::Window);// 窗口透明this->setAttribute(Qt::WA_TranslucentBackground);
}TopWidget::~TopWidget()
{}void TopWidget::setNum(int num)
{m_num = num;update();
}void TopWidget::paintEvent(QPaintEvent *pEvent)
{//创建QPainter对象,指明父对象,否则不能看见QPainter painter(this);//先画一条线QPen pen;//设置线宽pen.setWidth(10);//设置颜色pen.setColor(Qt::white);//设置线的样式 实线、虚线pen.setStyle(Qt::SolidLine);//设置线端点样式pen.setCapStyle(Qt::RoundCap);//设置画笔painter.setPen(pen);for (int i = 0; i < m_num; i++){//画竖线painter.drawLine(QPoint(width() * (i+1)/ (m_num+1), 0), QPoint(width() * (i + 1) / (m_num + 1), height()));//画横线painter.drawLine(QPoint(0, height() * (i + 1) / (m_num + 1)), QPoint(width(), height() * (i + 1) / (m_num + 1)));}}

主界面重写绘图和窗体移动事件:我们需要在主窗体移动的时候,叠加窗体跟随着主窗体移动。

void showWidget::paintEvent(QPaintEvent *event)
{QPoint pos_form = ui.widgetShow->mapToGlobal(ui.widgetShow->pos());m_widget->move(pos_form);m_widget->setGeometry(pos_form.x(), pos_form.y(), ui.widgetShow->width(), ui.widgetShow->height());
}
void showWidget::moveEvent(QMoveEvent *event)
{QPoint pos_form = ui.widgetShow->mapToGlobal(ui.widgetShow->pos());m_widget->move(pos_form);m_widget->setGeometry(pos_form.x(), pos_form.y(), ui.widgetShow->width(), ui.widgetShow->height());
}

完整源码:

#pragma once
#define LIBVLC_USE_PTHREAD_CANCEL 1
#include <QtWidgets/QWidget>
#include "ui_showWidget.h"
#include <QMenu>
#include <QActionGroup>
#include <vlc/vlc.h>#include <QDebug>
#include <QFileDialog>
#include <QThread>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QPainter>
#include "TopWidget.h"enum Rate
{Rate2X,Rate1_5X,Rate1_25X,Rate1_0X,Rate0_75X,Rate0_5X
};class showWidget : public QWidget
{Q_OBJECTpublic:showWidget(QWidget *parent = nullptr);~showWidget();protected:virtual void mouseDoubleClickEvent(QMouseEvent *event);virtual void keyPressEvent(QKeyEvent *event);virtual bool eventFilter(QObject *watched, QEvent *event);virtual void paintEvent(QPaintEvent *event);virtual void moveEvent(QMoveEvent *event);private slots:void slotOpenFile();void slotPlay();void slotPause();void slotStop();void slotValueChanged(int value);void slotCurrentIndexChanged(int index);void slotSnap();void slotCurrentSplitChanged(int index);private://事件处理回调static void vlcEvents(const libvlc_event_t *ev, void *param);private:Ui::showWidgetClass ui;private:libvlc_instance_t *vlc_base = nullptr;libvlc_media_t *vlc_media = nullptr;libvlc_media_player_t *vlc_mediaPlayer = nullptr;QList<float> m_lstRate;TopWidget *m_widget = nullptr;
};//=========================================================#include "showWidget.h"
#include <QTimer>
#include <QTime>
#include <QFileDialog>#pragma execution_character_set("utf-8")showWidget::showWidget(QWidget *parent): QWidget(parent)
{ui.setupUi(this);this->setWindowTitle("视频播放器");m_widget = new TopWidget(this);//m_widget->setStyleSheet(QString("background-color: rgba(255, 0, 0, 0%);"));m_widget->show();ui.cbxRate->setCurrentIndex(Rate1_0X);m_lstRate << 2.0 << 1.5 << 1.25 << 1.0 << 0.75 << 0.5;vlc_base = libvlc_new(0, NULL);ui.btnOpen->setFocusPolicy(Qt::NoFocus);ui.btnPlay->setFocusPolicy(Qt::NoFocus);ui.btnPause->setFocusPolicy(Qt::NoFocus);ui.btnStop->setFocusPolicy(Qt::NoFocus);ui.hSliderVolumn->setFocusPolicy(Qt::NoFocus);ui.cbxRate->setFocusPolicy(Qt::NoFocus);ui.horizontalSlider->installEventFilter(this);connect(ui.btnOpen, &QPushButton::clicked, this, &showWidget::slotOpenFile);connect(ui.btnPlay, &QPushButton::clicked, this, &showWidget::slotPlay);connect(ui.btnPause, &QPushButton::clicked, this, &showWidget::slotPause);connect(ui.btnStop, &QPushButton::clicked, this, &showWidget::slotStop);connect(ui.btnSnap, &QPushButton::clicked, this, &showWidget::slotSnap);connect(ui.hSliderVolumn, &QSlider::valueChanged, this, &showWidget::slotValueChanged);connect(ui.cbxRate,SIGNAL(currentIndexChanged(int)), this, SLOT(slotCurrentIndexChanged(int)));connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCurrentSplitChanged(int)));
}showWidget::~showWidget()
{libvlc_release(vlc_base); //减少libvlc实例的引用计数,并销毁
}void showWidget::mouseDoubleClickEvent(QMouseEvent *event)
{if (this->isFullScreen()){this->showNormal();}else{this->showFullScreen();}
}void showWidget::keyPressEvent(QKeyEvent *event)
{if (!vlc_mediaPlayer)return;int value = ui.hSliderVolumn->value();if (event->key() == Qt::Key_W)	//添加音量{qDebug() << "up";slotValueChanged(value+10);}else if (event->key() == Qt::Key_S)	//减小音量{slotValueChanged(value - 10);}else if (event->key() == Qt::Key_Space){if (vlc_mediaPlayer && libvlc_media_player_get_state(vlc_mediaPlayer) == libvlc_Playing){libvlc_media_player_pause(vlc_mediaPlayer);}else if (vlc_mediaPlayer && libvlc_media_player_get_state(vlc_mediaPlayer) == libvlc_Paused){libvlc_media_player_play(vlc_mediaPlayer);}}
}bool showWidget::eventFilter(QObject *watched, QEvent *event)
{if (watched == ui.horizontalSlider) {if (event->type() == QEvent::MouseButtonPress) {if (!vlc_mediaPlayer)return false;QMouseEvent *mouse = static_cast<QMouseEvent*>(event);libvlc_time_t totalLen = libvlc_media_player_get_length(vlc_mediaPlayer);QPoint pos = mouse->pos();libvlc_time_t time = 0;time = (double)pos.x() / ui.horizontalSlider->width() * totalLen;libvlc_media_player_set_time(vlc_mediaPlayer, time);return true;}else {return false;}}else {// pass the event on to the parent classreturn QWidget::eventFilter(watched, event);}
}void showWidget::paintEvent(QPaintEvent *event)
{QPoint pos_form = ui.widgetShow->mapToGlobal(ui.widgetShow->pos());m_widget->move(pos_form);m_widget->setGeometry(pos_form.x(), pos_form.y(), ui.widgetShow->width(), ui.widgetShow->height());
}
void showWidget::moveEvent(QMoveEvent *event)
{QPoint pos_form = ui.widgetShow->mapToGlobal(ui.widgetShow->pos());m_widget->move(pos_form);m_widget->setGeometry(pos_form.x(), pos_form.y(), ui.widgetShow->width(), ui.widgetShow->height());
}void showWidget::slotOpenFile()
{/*选择文件*/QString filename = QFileDialog::getOpenFileName(this, "选择打开的文件", "D:/", tr("*.*"));std::replace(filename.begin(), filename.end(), QChar('/'), QChar('\\'));vlc_media = libvlc_media_new_path(vlc_base, filename.toUtf8().data());if (!vlc_media) {return;}// 创建libvlc实例和媒体播放器vlc_mediaPlayer = libvlc_media_player_new_from_media(vlc_media);if (!vlc_mediaPlayer) {return;}// 等待元数据加载完成libvlc_media_parse(vlc_media);libvlc_video_set_mouse_input(vlc_mediaPlayer, 0);libvlc_video_set_key_input(vlc_mediaPlayer, 0);// 获取各种元数据const char *title = libvlc_media_get_meta(vlc_media, libvlc_meta_Title);const char *artist = libvlc_media_get_meta(vlc_media, libvlc_meta_Artist);const char *album = libvlc_media_get_meta(vlc_media, libvlc_meta_Album);const char *url = libvlc_media_get_meta(vlc_media, libvlc_meta_URL);const char *date = libvlc_media_get_meta(vlc_media, libvlc_meta_Date);const char *lang = libvlc_media_get_meta(vlc_media, libvlc_meta_Language);int duration = libvlc_media_get_duration(vlc_media);  // 获取时长(单位:毫秒)qDebug("Title: %s", title ? title : "N/A");qDebug("Artist: %s", artist ? artist : "N/A");qDebug("Album: %s", album ? album : "N/A");qDebug("Duration: %d ms", duration);qDebug("url: %s", url ? url : "N/A");qDebug("date: %s", date ? date : "N/A");qDebug("lang: %s", lang ? lang : "N/A");libvlc_media_track_t **tracks;int track_count = libvlc_media_tracks_get(vlc_media,&tracks);for (unsigned i = 0; i < track_count; i++) {libvlc_media_track_t* track = tracks[i];// 显示轨道信息printf("Track #%u: %s\n", i, track->psz_description);// 这里可以获取到每一个轨道的信息,比如轨道类型 track->i_type// 可能是 libvlc_track_video, libvlc_track_audio 或者 libvlc_track_text (字幕)if (track->i_type == libvlc_track_video) {// 处理视频轨道信息qDebug("width = %d",track->video->i_width);qDebug("height = %d", track->video->i_height);qDebug("rate_num = %d", track->video->i_frame_rate_num);qDebug("rate_den = %d", track->video->i_frame_rate_den);}else if (track->i_type == libvlc_track_audio) {// 处理音频轨道信息qDebug("channels = %d", track->audio->i_channels);qDebug("rate = %d", track->audio->i_rate);}else if (track->i_type == libvlc_track_text) {// 处理字幕轨道信息qDebug("psz_encoding = %s", track->subtitle->psz_encoding);}}//获取事件管理器libvlc_event_manager_t *em = libvlc_media_player_event_manager(vlc_mediaPlayer);// 注册事件监听器libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerEndReached, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerStopped, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerPlaying, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerPaused, vlcEvents, this);libvlc_media_player_set_hwnd(vlc_mediaPlayer, (void *)ui.widgetShow->winId());QTimer::singleShot(1000, this, &showWidget::slotPlay);}void showWidget::slotPlay()
{if (vlc_mediaPlayer)libvlc_media_player_play(vlc_mediaPlayer);
}void showWidget::slotPause()
{if (vlc_mediaPlayer)libvlc_media_player_pause(vlc_mediaPlayer);
}void showWidget::slotStop()
{if (vlc_mediaPlayer)libvlc_media_player_stop(vlc_mediaPlayer);
}void showWidget::slotValueChanged(int value)
{if (vlc_mediaPlayer)libvlc_audio_set_volume(vlc_mediaPlayer, value);
}void showWidget::slotCurrentIndexChanged(int index)
{if (vlc_mediaPlayer)libvlc_media_player_set_rate(vlc_mediaPlayer, m_lstRate[index]);
}void showWidget::slotSnap()
{if (vlc_mediaPlayer){QString path = "./snap.png";libvlc_video_take_snapshot(vlc_mediaPlayer, 0, path.toUtf8().data(), 0, 0);}
}void showWidget::slotCurrentSplitChanged(int index)
{m_widget->setNum(index);
}//事件回调
void showWidget::vlcEvents(const libvlc_event_t *ev, void *param)
{showWidget *w = (showWidget*)param;//处理不同的事件switch (ev->type) {case libvlc_MediaPlayerTimeChanged:{//qDebug() << "VLC媒体播放器时间已更改";qint64 len = libvlc_media_player_get_time(w->vlc_mediaPlayer);libvlc_time_t lenSec = len / 1000;libvlc_time_t totalLen = libvlc_media_player_get_length(w->vlc_mediaPlayer);libvlc_time_t totalLenSec = totalLen / 1000;int thh, tmm, tss;thh = lenSec / 3600;tmm = (lenSec % 3600) / 60;tss = (lenSec % 60);QTime time(thh, tmm, tss);w->ui.lbCurTime->setText(time.toString("hh:mm:ss"));thh = totalLenSec / 3600;tmm = (totalLenSec % 3600) / 60;tss = (totalLenSec % 60);QTime TotalTime(thh, tmm, tss);w->ui.lbTotalTime->setText(TotalTime.toString("hh:mm:ss"));double pos = (double)lenSec / totalLenSec * 100;w->ui.horizontalSlider->setValue(pos);}break;case libvlc_MediaPlayerEndReached:qDebug() << "VLC播放完毕.";break;case libvlc_MediaPlayerStopped:qDebug() << "VLC停止播放";break;case libvlc_MediaPlayerPlaying:qDebug() << "VLC开始播放";break;case libvlc_MediaPlayerPaused:qDebug() << "VLC暂停播放";break;}
}

以下是ui原型图:

Qt+FFmpeg+opengl从零制作视频播放器-1.项目介绍_qt opengl视频播放器-CSDN博客

libVLC 提取视频帧使用OpenGL渲染-CSDN博客

QT UDP通信(单播、广播、组播)-CSDN博客

QCharts -1.概述-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/611172.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

什么是智能组网?

智能组网已经成为现代社会中网络通信的重要技术。智能组网通过建立高效的网络连接&#xff0c;实现设备之间的信息传输和远程通信。它在不同地区的电脑与电脑、设备与设备、电脑与设备之间搭建了桥梁&#xff0c;极大地提升了信息传递的效率和便捷性。 天联是一种智能组网技术&…

一起学习python——基础篇(15)

今天讲一下python的网络请求方法。 如果使用python语言做接口测试&#xff0c;可以安装Requests模块。 安装步骤&#xff1a;打开编程软件&#xff0c;然后点击settings——>Project:System——>Python Interpreter——>加号——>搜索栏里面输入“Requests”——&…

EEPROM读写案例(以AT24C02为例)

本篇文章主要是在学习单片机串行接口时的学习经历&#xff0c;主要侧重于驱动程序的讲解。下文将通过ESP32S3、STM32两款MCU进行编写驱动案例。 1、AT24C02简要说明 AT24C02是美国微芯科技公司生产的电擦写式只读存储器系列中的一款&#xff0c;其容量为2K位&#xff08;即256字…

复合数据类型

在C语言中&#xff0c;复合数据类型是指那些可以包含多个简单数据类型的数据类型。以下是一些常见的C语言复合数据类型以及相关的例子&#xff1a; 1. 数组&#xff08;Arrays&#xff09;&#xff1a; 数组是一种可以存储多个相同类型数据的数据结构。例如&#xff1a; #in…

冯喜运:4.12黄金剑指2400关口,黄金原油走势分析及操作建议

【黄金消息面分析 】美债收益率集体狂飙。10年期美债收益率上破4.5%的关键门槛&#xff0c;收报4.514%。对美联储政策利率最敏感的2年期美债收益率大涨近20个基点&#xff0c;收报4.941%。5/30年期美债收益率自9月份以来首次倒挂。现货黄金从历史高位回落&#xff0c;盘中一度较…

Sketch3D:用于草图到3D生成的样式一致性指南

Sketch3D: Style-Consistent Guidance for Sketch-to-3D Generation Sketch3D&#xff1a;用于草图到3D生成的样式一致性指南 Wangguandong Zheng 重试 错误原因 Southeast UniversityChina 重试 错误原因 wgdzhengseu.edu.cnHaifeng Xia 重试 错误原因 Southeast Universit…

嵌入式单片机入职第二天-EEPROM与IIC

上午&#xff1a; 1.安装Jlink驱动&#xff0c;死活没反应&#xff0c;因为昨天才装完系统&#xff0c;领导让我装电脑主板驱动 领导方法进惠普官网通过查询电脑型号&#xff0c;里面几十个驱动搞得我眼花&#xff0c;领导告诉我进官网就去开会了&#xff0c;可能因为是外网&…

Python数据可视化:同时展示两个变量之间的双变量(联合)关系以及每个变量的单变量(边缘)分布seaborn.jointplot

【小白从小学Python、C、Java】 【计算机等考500强证书考研】 【Python-数据分析】 Python数据可视化&#xff1a; 同时展示两个变量之间的 双变量&#xff08;联合&#xff09;关系以及 每个变量的单变量(边缘)分布 seaborn.jointplot 选择题 jointplot可以画出什么样的图标?…

Leetcode算法训练日记 | day23

一、修剪二叉搜索树 1.题目 Leetcode&#xff1a;第 669 题 给你二叉搜索树的根节点 root &#xff0c;同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树&#xff0c;使得所有节点的值在[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即&#xff…

nvm安装详细教程(安装nvm、node、npm、cnpm、yarn及环境变量配置)

一、安装nvm 1. 下载nvm 点击 网盘下载 进行下载 2、双击下载好的 nvm-1.1.12-setup.zip 文件 3.双击 nvm-setup.exe 开始安装 4. 选择我接受&#xff0c;然后点击next 5.选择nvm安装路径&#xff0c;路径名称不要有空格&#xff0c;然后点击next 6.node.js安装路径&#…

【网安小白成长之路】6.pikachu、sql-labs、upload-labs靶场搭建

&#x1f42e;博主syst1m 带你 acquire knowledge&#xff01; ✨博客首页——syst1m的博客&#x1f498; &#x1f51e; 《网安小白成长之路(我要变成大佬&#x1f60e;&#xff01;&#xff01;)》真实小白学习历程&#xff0c;手把手带你一起从入门到入狱&#x1f6ad; &…

【C++题解】1605. 求一个两位数的个位和十位的和

问题&#xff1a;1605. 求一个两位数的个位和十位的和 类型&#xff1a;基本运算、拆位求解。 题目描述&#xff1a; 从键盘读入一个两位的整数 n &#xff0c;请求出这个两位整数个位和十位的和是多少&#xff1f; 输入&#xff1a; 一个两位的整数 n 。 输出&#xff1a…