当前位置:   article > 正文

【简便的PyQt5】制作一个极具特色的视频播放器_制作复杂video pyqt5

制作复杂video pyqt5

【简便的PyQt5】制作一个极具特色的视频播放器

写在前面

效果展示

开源代码

loadInputVideo.py

demo.py


写在前面

        实现了以下功能 :

        ❤  PyQt5本地上传视频

        ❤  PyQt5配合OpenCv播放所有类型的视频

        ❤  PyQt5标签里文本居中

        ❤  PyQt5标签里文本重写

        ❤  视频可多次上传播放

效果展示

 

 

 

开源代码

loadInputVideo.py

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'loadInputVideo.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.15.4
  5. #
  6. # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  7. # run again. Do not edit this file unless you know what you are doing.
  8. from PyQt5 import QtCore, QtGui, QtWidgets
  9. class Ui_MainWindow(object):
  10. def setupUi(self, MainWindow):
  11. MainWindow.setObjectName("MainWindow")
  12. MainWindow.resize(872, 638)
  13. MainWindow.setStyleSheet("background-color:black;")
  14. self.centralwidget = QtWidgets.QWidget(MainWindow)
  15. self.centralwidget.setObjectName("centralwidget")
  16. self.pushButton = QtWidgets.QPushButton(self.centralwidget)
  17. self.pushButton.setGeometry(QtCore.QRect(280, 550, 60, 60))
  18. self.pushButton.setMinimumSize(QtCore.QSize(60, 60))
  19. self.pushButton.setMaximumSize(QtCore.QSize(60, 60))
  20. self.pushButton.setStyleSheet("font: 11pt \"楷体\";\n"
  21. "background-color:black;\n"
  22. "color:red;\n"
  23. "border-radius:30px;\n"
  24. "border:2px groove blue;\n"
  25. "border-style:outset;\n"
  26. "")
  27. self.pushButton.setObjectName("pushButton")
  28. self.label_4 = QtWidgets.QLabel(self.centralwidget)
  29. self.label_4.setGeometry(QtCore.QRect(30, 40, 800, 450))
  30. self.label_4.setMaximumSize(QtCore.QSize(905, 650))
  31. self.label_4.setStyleSheet("color:red;\n"
  32. "border-width:1px;\n"
  33. "border-style:solid;\n"
  34. "border-color:red;\n"
  35. "background-color:black;\n"
  36. "font: 18pt \"华文行楷\";")
  37. self.label_4.setObjectName("label_4")
  38. self.label_8 = QtWidgets.QLabel(self.centralwidget)
  39. self.label_8.setGeometry(QtCore.QRect(30, 500, 801, 41))
  40. font = QtGui.QFont()
  41. font.setFamily("楷体")
  42. font.setPointSize(13)
  43. font.setBold(False)
  44. font.setItalic(False)
  45. font.setWeight(50)
  46. self.label_8.setFont(font)
  47. self.label_8.setStyleSheet("font: 13pt \"楷体\";\n"
  48. "color:red;\n"
  49. "border-width:1px;\n"
  50. "border-style:solid;\n"
  51. "border-color:red;\n"
  52. "background-color:black;")
  53. self.label_8.setText("")
  54. self.label_8.setObjectName("label_8")
  55. self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
  56. self.pushButton_2.setGeometry(QtCore.QRect(500, 550, 60, 60))
  57. self.pushButton_2.setMinimumSize(QtCore.QSize(60, 60))
  58. self.pushButton_2.setMaximumSize(QtCore.QSize(60, 60))
  59. self.pushButton_2.setStyleSheet("font: 11pt \"楷体\";\n"
  60. "background-color:black;\n"
  61. "color:red;\n"
  62. "border-radius:30px;\n"
  63. "border:2px groove blue;\n"
  64. "border-style:outset;\n"
  65. "text-align:centet;")
  66. self.pushButton_2.setObjectName("pushButton_2")
  67. MainWindow.setCentralWidget(self.centralwidget)
  68. self.menubar = QtWidgets.QMenuBar(MainWindow)
  69. self.menubar.setGeometry(QtCore.QRect(0, 0, 872, 23))
  70. self.menubar.setObjectName("menubar")
  71. MainWindow.setMenuBar(self.menubar)
  72. self.statusbar = QtWidgets.QStatusBar(MainWindow)
  73. self.statusbar.setObjectName("statusbar")
  74. MainWindow.setStatusBar(self.statusbar)
  75. self.retranslateUi(MainWindow)
  76. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  77. def retranslateUi(self, MainWindow):
  78. _translate = QtCore.QCoreApplication.translate
  79. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  80. self.pushButton.setText(_translate("MainWindow", "Load"))
  81. self.label_4.setText(_translate("MainWindow", " Waiting for video..."))
  82. self.pushButton_2.setText(_translate("MainWindow", "Play"))

demo.py

  1. import cv2
  2. import shutil
  3. import time
  4. from scipy import signal
  5. from PIL import Image
  6. import matplotlib.pyplot as plt
  7. import numpy as np
  8. import sys
  9. import os
  10. from GuiData.loadInputVideo import Ui_MainWindow as playVideo
  11. from PyQt5 import QtCore, QtGui, QtWidgets
  12. from PyQt5.QtCore import Qt
  13. class playVideoWindow(QtWidgets.QMainWindow, playVideo):
  14. def __init__(self):
  15. super(playVideoWindow, self).__init__()
  16. self.setupUi(self)
  17. self.cap = []
  18. self.videoName = ''
  19. # 居中
  20. self.label_8.setAlignment(Qt.AlignCenter)
  21. self.reSet_label('please load video...')
  22. self.timer_camera = QtCore.QTimer()
  23. self.timer_camera.setTimerType(Qt.PreciseTimer)
  24. self.pushButton.clicked.connect(self.load_video)
  25. self.pushButton_2.clicked.connect(self.play_video)
  26. def load_video(self):
  27. """ Slot function to start the progamme
  28. """
  29. self.videoName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open", "", "*.MOV;;*.avi;;*.mp4;;All Files(*)")
  30. self.label_4.repaint()
  31. self.label_4.setText('It is ready...')
  32. self.label_4.repaint()
  33. self.reSet_label('please click play button')
  34. def play_video(self):
  35. if self.videoName != "":
  36. self.cap = cv2.VideoCapture(self.videoName)
  37. self.timer_camera.start(1)
  38. self.timer_camera.timeout.connect(self.openFrame)
  39. def openFrame(self):
  40. """ Slot function to capture frame and process it
  41. """
  42. if (self.cap.isOpened()):
  43. self.reSet_label('playing...')
  44. ret, self.frame = self.cap.read()
  45. if ret:
  46. frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
  47. height, width, bytesPerComponent = frame.shape
  48. bytesPerLine = bytesPerComponent * width
  49. q_image = QtGui.QImage(frame.data, width, height, bytesPerLine,
  50. QtGui.QImage.Format_RGB888).scaled(self.label_4.width(), self.label_4.height())
  51. self.label_4.setPixmap(QtGui.QPixmap.fromImage(q_image))
  52. else:
  53. self.reSet_label('over!')
  54. self.label_4.repaint()
  55. self.label_4.setText('Waiting for video...')
  56. self.label_4.repaint()
  57. self.cap.release()
  58. self.timer_camera.stop()
  59. def reSet_label(self, text):
  60. self.label_8.repaint()
  61. self.label_8.setText(text)
  62. self.label_8.repaint()
  63. # 使用案例
  64. if __name__ == '__main__':
  65. app = QtWidgets.QApplication(sys.argv)
  66. contorller = playVideoWindow()
  67. contorller.show()
  68. sys.exit(app.exec_())

完毕!

如果觉得有帮助,麻烦点个赞+收藏喔! 

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

闽ICP备14008679号