• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

PyQt5学习笔记--Qt Designer加载、播放和保存视频

武飞扬头像
布吉岛呀~
帮助1

目录

1--基于Qt Designer设计ui文件

2--代码

3--结果

4--补充

5--加载、播放、转换和保存视频的实例


1--基于Qt Designer设计ui文件

学新通

2--代码

  1.  
    from PyQt5.QtWidgets import *
  2.  
    from PyQt5.QtMultimedia import *
  3.  
    from PyQt5.QtMultimediaWidgets import QVideoWidget
  4.  
    from PyQt5 import uic
  5.  
    import sys
  6.  
     
  7.  
    class MyWindow(QWidget):
  8.  
    def __init__(self):
  9.  
    super().__init__()
  10.  
    self.init_ui()
  11.  
     
  12.  
    def init_ui(self):
  13.  
    self.ui = uic.loadUi("./video.ui") # 加载由Qt Designer设计的ui文件
  14.  
     
  15.  
    # 加载自定义ui属性
  16.  
    self.openGLWidget = self.ui.openGLWidget
  17.  
    self.video_btn1 = self.ui.pushButton
  18.  
    self.video_btn2 = self.ui.pushButton_2
  19.  
     
  20.  
    # 创建一个布局将 QVideoWidget 内嵌到 自定义ui的Widget中
  21.  
    layout = QHBoxLayout()
  22.  
    self.vw = QVideoWidget()
  23.  
    layout.addWidget(self.vw)
  24.  
    self.openGLWidget.setLayout(layout)
  25.  
     
  26.  
    # img_btn1 绑定槽函数 loadVideo()
  27.  
    self.video_btn1.clicked.connect(self.loadVideo)
  28.  
     
  29.  
    # img_btn2 绑定槽函数 playVideo()
  30.  
    self.video_btn2.clicked.connect(self.playVideo)
  31.  
     
  32.  
    def loadVideo(self):
  33.  
    self.player = QMediaPlayer()
  34.  
    self.player.setVideoOutput(self.vw) # 视频播放的widget
  35.  
    self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) # 选取视频文件
  36.  
     
  37.  
    def playVideo(self):
  38.  
    self.player.play() # 播放视频
  39.  
    self.vw.show()
  40.  
     
  41.  
    if __name__ == '__main__':
  42.  
     
  43.  
    app = QApplication(sys.argv)
  44.  
    w = MyWindow()
  45.  
    w.ui.show()
  46.  
    sys.exit(app.exec_())
学新通

3--结果

学新通

学新通

学新通

4--补充

① 上述代码只实现了加载视频和播放视频的功能,缺少暂停、进度移动等常见功能。

② 上述代码将 QVideoWidget 通过一个layout布局的形式内嵌到自定义的 QOpenGLWidget 中,但播放测试视频的时候存在视频无法覆盖 Widget 的问题。

5--加载、播放、转换和保存视频的实例

① 基于Qt Designer 设计 ui 文件

学新通

② 代码

注:代码具有保存视频的功能,转换视频则采取了最简单的灰度化处理操作作为功能展示。

  1.  
    from PyQt5.QtGui import QImage, QPixmap
  2.  
    from PyQt5.QtWidgets import *
  3.  
    from PyQt5.QtMultimedia import *
  4.  
    from PyQt5.QtMultimediaWidgets import QVideoWidget
  5.  
    from PyQt5 import uic
  6.  
    import sys
  7.  
    import cv2
  8.  
     
  9.  
    class MyWindow(QWidget):
  10.  
    def __init__(self):
  11.  
    super().__init__()
  12.  
    self.init_ui()
  13.  
     
  14.  
    def init_ui(self):
  15.  
    self.ui = uic.loadUi("./video.ui") # 加载由Qt Designer设计的ui文件
  16.  
     
  17.  
    # 加载自定义ui属性
  18.  
    self.openGLWidget1 = self.ui.openGLWidget
  19.  
    self.QGraphView = self.ui.graphicsView
  20.  
    self.video_btn1 = self.ui.pushButton
  21.  
    self.video_btn2 = self.ui.pushButton_2
  22.  
    self.gray_btn3 = self.ui.pushButton_3
  23.  
    self.gray_btn4 = self.ui.pushButton_4
  24.  
     
  25.  
    # 创建一个布局将 QVideoWidget 内嵌到 自定义ui的Widget1中
  26.  
    layout1 = QHBoxLayout()
  27.  
    self.vw1 = QVideoWidget()
  28.  
    layout1.addWidget(self.vw1)
  29.  
    self.openGLWidget1.setLayout(layout1)
  30.  
     
  31.  
    # video_btn1 绑定槽函数 loadVideo()
  32.  
    self.video_btn1.clicked.connect(self.loadVideo)
  33.  
     
  34.  
    # video_btn2 绑定槽函数 playVideo()
  35.  
    self.video_btn2.clicked.connect(self.playVideo)
  36.  
     
  37.  
    # gray_btn3 绑定槽函数 convert_gray()
  38.  
    self.gray_btn3.clicked.connect(self.convert_gray)
  39.  
     
  40.  
    # gray_btn4 绑定槽函数 save_gray()
  41.  
    self.gray_btn4.clicked.connect(self.save_gray)
  42.  
     
  43.  
    def loadVideo(self):
  44.  
    self.player = QMediaPlayer()
  45.  
    self.player.setVideoOutput(self.vw1) # 视频播放的widget
  46.  
    self.video_file = QFileDialog.getOpenFileUrl()[0]
  47.  
    self.video_path = self.video_file.toString()[8:]
  48.  
    print(self.video_path)
  49.  
    self.player.setMedia(QMediaContent(self.video_file))# 选取视频文件
  50.  
     
  51.  
    def playVideo(self):
  52.  
    self.player.play() # 播放视频
  53.  
    self.vw1.show()
  54.  
     
  55.  
    def convert_gray(self):
  56.  
     
  57.  
    cap = cv2.VideoCapture(self.video_path)
  58.  
    fps = cap.get(cv2.CAP_PROP_FPS)
  59.  
     
  60.  
    while True:
  61.  
    ret, frame = cap.read()
  62.  
    if not ret:
  63.  
    break
  64.  
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 灰度图转换
  65.  
    frame = QImage(gray.data, gray.shape[1], gray.shape[0], gray.strides[0], QImage.Format_Indexed8)
  66.  
    pix = QPixmap.fromImage(frame)
  67.  
    item = QGraphicsPixmapItem(pix) # 创建像素图元
  68.  
    scene = QGraphicsScene() # 创建场景
  69.  
    scene.addItem(item)
  70.  
    self.QGraphView.setScene(scene) # 将场景添加至视图
  71.  
    self.QGraphView.fitInView(item) # 自适应大小
  72.  
    cv2.waitKey(int((1/fps)*1000))
  73.  
     
  74.  
    cap.release()
  75.  
     
  76.  
    def save_gray(self):
  77.  
    self.output_path = self.video_path.rsplit("/", 1)[0] '/gray_' self.video_path.rsplit("/", 1)[1]
  78.  
    cap = cv2.VideoCapture(self.video_path)
  79.  
    h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
  80.  
    w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
  81.  
    fps = cap.get(cv2.CAP_PROP_FPS)
  82.  
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
  83.  
    writer = cv2.VideoWriter(filename = self.output_path, fourcc = fourcc, fps = fps,
  84.  
    frameSize = (int(w), int(h)), isColor = 0)
  85.  
    while True:
  86.  
    ret, frame = cap.read()
  87.  
    if not ret:
  88.  
    break
  89.  
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 灰度图转换
  90.  
    writer.write(gray) # 保存灰度图
  91.  
     
  92.  
    cap.release()
  93.  
     
  94.  
    if __name__ == '__main__':
  95.  
     
  96.  
    app = QApplication(sys.argv)
  97.  
    w = MyWindow()
  98.  
    w.ui.show()
  99.  
    sys.exit(app.exec_())
学新通

 ③ 结果展示:

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhibg
系列文章
更多 icon
同类精品
更多 icon
继续加载