当前位置:   article > 正文

libVLC 视频界面分割

libVLC 视频界面分割

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

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

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

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

paintEvent绘制线条,很简单吧。

  1. void TopWidget::paintEvent(QPaintEvent *pEvent)
  2. {
  3. //创建QPainter对象,指明父对象,否则不能看见
  4. QPainter painter(this);
  5. //先画一条线
  6. QPen pen;
  7. //设置线宽
  8. pen.setWidth(10);
  9. //设置颜色
  10. pen.setColor(Qt::white);
  11. //设置线的样式 实线、虚线
  12. pen.setStyle(Qt::SolidLine);
  13. //设置线端点样式
  14. pen.setCapStyle(Qt::RoundCap);
  15. //设置画笔
  16. painter.setPen(pen);
  17. for (int i = 0; i < m_num; i++)
  18. {
  19. //画竖线
  20. painter.drawLine(QPoint(width() * (i+1)/ (m_num+1), 0), QPoint(width() * (i + 1) / (m_num + 1), height()));
  21. //画横线
  22. painter.drawLine(QPoint(0, height() * (i + 1) / (m_num + 1)), QPoint(width(), height() * (i + 1) / (m_num + 1)));
  23. }
  24. }

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

Qt QPainter绘图_qt painter-CSDN博客

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

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

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

  1. #pragma once
  2. #include <QWidget>
  3. #include <QPaintEvent>
  4. #include <QPainter>
  5. #include "ui_TopWidget.h"
  6. class TopWidget : public QWidget
  7. {
  8. Q_OBJECT
  9. public:
  10. TopWidget(QWidget *parent = nullptr);
  11. ~TopWidget();
  12. void setNum(int num);
  13. protected:
  14. void paintEvent(QPaintEvent *pEvent);
  15. private:
  16. Ui::TopWidgetClass ui;
  17. int m_num = 0;
  18. };
  19. #include "TopWidget.h"
  20. TopWidget::TopWidget(QWidget *parent)
  21. : QWidget(parent)
  22. {
  23. ui.setupUi(this);
  24. //窗口无边框
  25. setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
  26. // 窗口透明
  27. this->setAttribute(Qt::WA_TranslucentBackground);
  28. }
  29. TopWidget::~TopWidget()
  30. {}
  31. void TopWidget::setNum(int num)
  32. {
  33. m_num = num;
  34. update();
  35. }
  36. void TopWidget::paintEvent(QPaintEvent *pEvent)
  37. {
  38. //创建QPainter对象,指明父对象,否则不能看见
  39. QPainter painter(this);
  40. //先画一条线
  41. QPen pen;
  42. //设置线宽
  43. pen.setWidth(10);
  44. //设置颜色
  45. pen.setColor(Qt::white);
  46. //设置线的样式 实线、虚线
  47. pen.setStyle(Qt::SolidLine);
  48. //设置线端点样式
  49. pen.setCapStyle(Qt::RoundCap);
  50. //设置画笔
  51. painter.setPen(pen);
  52. for (int i = 0; i < m_num; i++)
  53. {
  54. //画竖线
  55. painter.drawLine(QPoint(width() * (i+1)/ (m_num+1), 0), QPoint(width() * (i + 1) / (m_num + 1), height()));
  56. //画横线
  57. painter.drawLine(QPoint(0, height() * (i + 1) / (m_num + 1)), QPoint(width(), height() * (i + 1) / (m_num + 1)));
  58. }
  59. }

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

  1. void showWidget::paintEvent(QPaintEvent *event)
  2. {
  3. QPoint pos_form = ui.widgetShow->mapToGlobal(ui.widgetShow->pos());
  4. m_widget->move(pos_form);
  5. m_widget->setGeometry(pos_form.x(), pos_form.y(), ui.widgetShow->width(), ui.widgetShow->height());
  6. }
  7. void showWidget::moveEvent(QMoveEvent *event)
  8. {
  9. QPoint pos_form = ui.widgetShow->mapToGlobal(ui.widgetShow->pos());
  10. m_widget->move(pos_form);
  11. m_widget->setGeometry(pos_form.x(), pos_form.y(), ui.widgetShow->width(), ui.widgetShow->height());
  12. }

完整源码:

  1. #pragma once
  2. #define LIBVLC_USE_PTHREAD_CANCEL 1
  3. #include <QtWidgets/QWidget>
  4. #include "ui_showWidget.h"
  5. #include <QMenu>
  6. #include <QActionGroup>
  7. #include <vlc/vlc.h>
  8. #include <QDebug>
  9. #include <QFileDialog>
  10. #include <QThread>
  11. #include <QMouseEvent>
  12. #include <QKeyEvent>
  13. #include <QPainter>
  14. #include "TopWidget.h"
  15. enum Rate
  16. {
  17. Rate2X,
  18. Rate1_5X,
  19. Rate1_25X,
  20. Rate1_0X,
  21. Rate0_75X,
  22. Rate0_5X
  23. };
  24. class showWidget : public QWidget
  25. {
  26. Q_OBJECT
  27. public:
  28. showWidget(QWidget *parent = nullptr);
  29. ~showWidget();
  30. protected:
  31. virtual void mouseDoubleClickEvent(QMouseEvent *event);
  32. virtual void keyPressEvent(QKeyEvent *event);
  33. virtual bool eventFilter(QObject *watched, QEvent *event);
  34. virtual void paintEvent(QPaintEvent *event);
  35. virtual void moveEvent(QMoveEvent *event);
  36. private slots:
  37. void slotOpenFile();
  38. void slotPlay();
  39. void slotPause();
  40. void slotStop();
  41. void slotValueChanged(int value);
  42. void slotCurrentIndexChanged(int index);
  43. void slotSnap();
  44. void slotCurrentSplitChanged(int index);
  45. private:
  46. //事件处理回调
  47. static void vlcEvents(const libvlc_event_t *ev, void *param);
  48. private:
  49. Ui::showWidgetClass ui;
  50. private:
  51. libvlc_instance_t *vlc_base = nullptr;
  52. libvlc_media_t *vlc_media = nullptr;
  53. libvlc_media_player_t *vlc_mediaPlayer = nullptr;
  54. QList<float> m_lstRate;
  55. TopWidget *m_widget = nullptr;
  56. };
  57. //=========================================================
  58. #include "showWidget.h"
  59. #include <QTimer>
  60. #include <QTime>
  61. #include <QFileDialog>
  62. #pragma execution_character_set("utf-8")
  63. showWidget::showWidget(QWidget *parent)
  64. : QWidget(parent)
  65. {
  66. ui.setupUi(this);
  67. this->setWindowTitle("视频播放器");
  68. m_widget = new TopWidget(this);
  69. //m_widget->setStyleSheet(QString("background-color: rgba(255, 0, 0, 0%);"));
  70. m_widget->show();
  71. ui.cbxRate->setCurrentIndex(Rate1_0X);
  72. m_lstRate << 2.0 << 1.5 << 1.25 << 1.0 << 0.75 << 0.5;
  73. vlc_base = libvlc_new(0, NULL);
  74. ui.btnOpen->setFocusPolicy(Qt::NoFocus);
  75. ui.btnPlay->setFocusPolicy(Qt::NoFocus);
  76. ui.btnPause->setFocusPolicy(Qt::NoFocus);
  77. ui.btnStop->setFocusPolicy(Qt::NoFocus);
  78. ui.hSliderVolumn->setFocusPolicy(Qt::NoFocus);
  79. ui.cbxRate->setFocusPolicy(Qt::NoFocus);
  80. ui.horizontalSlider->installEventFilter(this);
  81. connect(ui.btnOpen, &QPushButton::clicked, this, &showWidget::slotOpenFile);
  82. connect(ui.btnPlay, &QPushButton::clicked, this, &showWidget::slotPlay);
  83. connect(ui.btnPause, &QPushButton::clicked, this, &showWidget::slotPause);
  84. connect(ui.btnStop, &QPushButton::clicked, this, &showWidget::slotStop);
  85. connect(ui.btnSnap, &QPushButton::clicked, this, &showWidget::slotSnap);
  86. connect(ui.hSliderVolumn, &QSlider::valueChanged, this, &showWidget::slotValueChanged);
  87. connect(ui.cbxRate,SIGNAL(currentIndexChanged(int)), this, SLOT(slotCurrentIndexChanged(int)));
  88. connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCurrentSplitChanged(int)));
  89. }
  90. showWidget::~showWidget()
  91. {
  92. libvlc_release(vlc_base); //减少libvlc实例的引用计数,并销毁
  93. }
  94. void showWidget::mouseDoubleClickEvent(QMouseEvent *event)
  95. {
  96. if (this->isFullScreen())
  97. {
  98. this->showNormal();
  99. }
  100. else
  101. {
  102. this->showFullScreen();
  103. }
  104. }
  105. void showWidget::keyPressEvent(QKeyEvent *event)
  106. {
  107. if (!vlc_mediaPlayer)
  108. return;
  109. int value = ui.hSliderVolumn->value();
  110. if (event->key() == Qt::Key_W) //添加音量
  111. {
  112. qDebug() << "up";
  113. slotValueChanged(value+10);
  114. }
  115. else if (event->key() == Qt::Key_S) //减小音量
  116. {
  117. slotValueChanged(value - 10);
  118. }
  119. else if (event->key() == Qt::Key_Space)
  120. {
  121. if (vlc_mediaPlayer && libvlc_media_player_get_state(vlc_mediaPlayer) == libvlc_Playing)
  122. {
  123. libvlc_media_player_pause(vlc_mediaPlayer);
  124. }
  125. else if (vlc_mediaPlayer && libvlc_media_player_get_state(vlc_mediaPlayer) == libvlc_Paused)
  126. {
  127. libvlc_media_player_play(vlc_mediaPlayer);
  128. }
  129. }
  130. }
  131. bool showWidget::eventFilter(QObject *watched, QEvent *event)
  132. {
  133. if (watched == ui.horizontalSlider) {
  134. if (event->type() == QEvent::MouseButtonPress) {
  135. if (!vlc_mediaPlayer)
  136. return false;
  137. QMouseEvent *mouse = static_cast<QMouseEvent*>(event);
  138. libvlc_time_t totalLen = libvlc_media_player_get_length(vlc_mediaPlayer);
  139. QPoint pos = mouse->pos();
  140. libvlc_time_t time = 0;
  141. time = (double)pos.x() / ui.horizontalSlider->width() * totalLen;
  142. libvlc_media_player_set_time(vlc_mediaPlayer, time);
  143. return true;
  144. }
  145. else {
  146. return false;
  147. }
  148. }
  149. else {
  150. // pass the event on to the parent class
  151. return QWidget::eventFilter(watched, event);
  152. }
  153. }
  154. void showWidget::paintEvent(QPaintEvent *event)
  155. {
  156. QPoint pos_form = ui.widgetShow->mapToGlobal(ui.widgetShow->pos());
  157. m_widget->move(pos_form);
  158. m_widget->setGeometry(pos_form.x(), pos_form.y(), ui.widgetShow->width(), ui.widgetShow->height());
  159. }
  160. void showWidget::moveEvent(QMoveEvent *event)
  161. {
  162. QPoint pos_form = ui.widgetShow->mapToGlobal(ui.widgetShow->pos());
  163. m_widget->move(pos_form);
  164. m_widget->setGeometry(pos_form.x(), pos_form.y(), ui.widgetShow->width(), ui.widgetShow->height());
  165. }
  166. void showWidget::slotOpenFile()
  167. {
  168. /*选择文件*/
  169. QString filename = QFileDialog::getOpenFileName(this, "选择打开的文件", "D:/", tr("*.*"));
  170. std::replace(filename.begin(), filename.end(), QChar('/'), QChar('\\'));
  171. vlc_media = libvlc_media_new_path(vlc_base, filename.toUtf8().data());
  172. if (!vlc_media) {
  173. return;
  174. }
  175. // 创建libvlc实例和媒体播放器
  176. vlc_mediaPlayer = libvlc_media_player_new_from_media(vlc_media);
  177. if (!vlc_mediaPlayer) {
  178. return;
  179. }
  180. // 等待元数据加载完成
  181. libvlc_media_parse(vlc_media);
  182. libvlc_video_set_mouse_input(vlc_mediaPlayer, 0);
  183. libvlc_video_set_key_input(vlc_mediaPlayer, 0);
  184. // 获取各种元数据
  185. const char *title = libvlc_media_get_meta(vlc_media, libvlc_meta_Title);
  186. const char *artist = libvlc_media_get_meta(vlc_media, libvlc_meta_Artist);
  187. const char *album = libvlc_media_get_meta(vlc_media, libvlc_meta_Album);
  188. const char *url = libvlc_media_get_meta(vlc_media, libvlc_meta_URL);
  189. const char *date = libvlc_media_get_meta(vlc_media, libvlc_meta_Date);
  190. const char *lang = libvlc_media_get_meta(vlc_media, libvlc_meta_Language);
  191. int duration = libvlc_media_get_duration(vlc_media); // 获取时长(单位:毫秒)
  192. qDebug("Title: %s", title ? title : "N/A");
  193. qDebug("Artist: %s", artist ? artist : "N/A");
  194. qDebug("Album: %s", album ? album : "N/A");
  195. qDebug("Duration: %d ms", duration);
  196. qDebug("url: %s", url ? url : "N/A");
  197. qDebug("date: %s", date ? date : "N/A");
  198. qDebug("lang: %s", lang ? lang : "N/A");
  199. libvlc_media_track_t **tracks;
  200. int track_count = libvlc_media_tracks_get(vlc_media,&tracks);
  201. for (unsigned i = 0; i < track_count; i++)
  202. {
  203. libvlc_media_track_t* track = tracks[i];
  204. // 显示轨道信息
  205. printf("Track #%u: %s\n", i, track->psz_description);
  206. // 这里可以获取到每一个轨道的信息,比如轨道类型 track->i_type
  207. // 可能是 libvlc_track_video, libvlc_track_audio 或者 libvlc_track_text (字幕)
  208. if (track->i_type == libvlc_track_video) {
  209. // 处理视频轨道信息
  210. qDebug("width = %d",track->video->i_width);
  211. qDebug("height = %d", track->video->i_height);
  212. qDebug("rate_num = %d", track->video->i_frame_rate_num);
  213. qDebug("rate_den = %d", track->video->i_frame_rate_den);
  214. }
  215. else if (track->i_type == libvlc_track_audio) {
  216. // 处理音频轨道信息
  217. qDebug("channels = %d", track->audio->i_channels);
  218. qDebug("rate = %d", track->audio->i_rate);
  219. }
  220. else if (track->i_type == libvlc_track_text) {
  221. // 处理字幕轨道信息
  222. qDebug("psz_encoding = %s", track->subtitle->psz_encoding);
  223. }
  224. }
  225. //获取事件管理器
  226. libvlc_event_manager_t *em = libvlc_media_player_event_manager(vlc_mediaPlayer);
  227. // 注册事件监听器
  228. libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this);
  229. libvlc_event_attach(em, libvlc_MediaPlayerEndReached, vlcEvents, this);
  230. libvlc_event_attach(em, libvlc_MediaPlayerStopped, vlcEvents, this);
  231. libvlc_event_attach(em, libvlc_MediaPlayerPlaying, vlcEvents, this);
  232. libvlc_event_attach(em, libvlc_MediaPlayerPaused, vlcEvents, this);
  233. libvlc_media_player_set_hwnd(vlc_mediaPlayer, (void *)ui.widgetShow->winId());
  234. QTimer::singleShot(1000, this, &showWidget::slotPlay);
  235. }
  236. void showWidget::slotPlay()
  237. {
  238. if (vlc_mediaPlayer)
  239. libvlc_media_player_play(vlc_mediaPlayer);
  240. }
  241. void showWidget::slotPause()
  242. {
  243. if (vlc_mediaPlayer)
  244. libvlc_media_player_pause(vlc_mediaPlayer);
  245. }
  246. void showWidget::slotStop()
  247. {
  248. if (vlc_mediaPlayer)
  249. libvlc_media_player_stop(vlc_mediaPlayer);
  250. }
  251. void showWidget::slotValueChanged(int value)
  252. {
  253. if (vlc_mediaPlayer)
  254. libvlc_audio_set_volume(vlc_mediaPlayer, value);
  255. }
  256. void showWidget::slotCurrentIndexChanged(int index)
  257. {
  258. if (vlc_mediaPlayer)
  259. libvlc_media_player_set_rate(vlc_mediaPlayer, m_lstRate[index]);
  260. }
  261. void showWidget::slotSnap()
  262. {
  263. if (vlc_mediaPlayer)
  264. {
  265. QString path = "./snap.png";
  266. libvlc_video_take_snapshot(vlc_mediaPlayer, 0, path.toUtf8().data(), 0, 0);
  267. }
  268. }
  269. void showWidget::slotCurrentSplitChanged(int index)
  270. {
  271. m_widget->setNum(index);
  272. }
  273. //事件回调
  274. void showWidget::vlcEvents(const libvlc_event_t *ev, void *param)
  275. {
  276. showWidget *w = (showWidget*)param;
  277. //处理不同的事件
  278. switch (ev->type) {
  279. case libvlc_MediaPlayerTimeChanged:
  280. {
  281. //qDebug() << "VLC媒体播放器时间已更改";
  282. qint64 len = libvlc_media_player_get_time(w->vlc_mediaPlayer);
  283. libvlc_time_t lenSec = len / 1000;
  284. libvlc_time_t totalLen = libvlc_media_player_get_length(w->vlc_mediaPlayer);
  285. libvlc_time_t totalLenSec = totalLen / 1000;
  286. int thh, tmm, tss;
  287. thh = lenSec / 3600;
  288. tmm = (lenSec % 3600) / 60;
  289. tss = (lenSec % 60);
  290. QTime time(thh, tmm, tss);
  291. w->ui.lbCurTime->setText(time.toString("hh:mm:ss"));
  292. thh = totalLenSec / 3600;
  293. tmm = (totalLenSec % 3600) / 60;
  294. tss = (totalLenSec % 60);
  295. QTime TotalTime(thh, tmm, tss);
  296. w->ui.lbTotalTime->setText(TotalTime.toString("hh:mm:ss"));
  297. double pos = (double)lenSec / totalLenSec * 100;
  298. w->ui.horizontalSlider->setValue(pos);
  299. }
  300. break;
  301. case libvlc_MediaPlayerEndReached:
  302. qDebug() << "VLC播放完毕.";
  303. break;
  304. case libvlc_MediaPlayerStopped:
  305. qDebug() << "VLC停止播放";
  306. break;
  307. case libvlc_MediaPlayerPlaying:
  308. qDebug() << "VLC开始播放";
  309. break;
  310. case libvlc_MediaPlayerPaused:
  311. qDebug() << "VLC暂停播放";
  312. break;
  313. }
  314. }

以下是ui原型图:

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

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

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

QCharts -1.概述-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/428491
推荐阅读
相关标签
  

闽ICP备14008679号