当前位置:   article > 正文

PyQt5 视频播放器_qvideowidget pyqt5

qvideowidget pyqt5

PyQt5 视频播放器

环境

Python 3.7.4
PyQt5==5.15.0
PyCharm 2020.2.4 (Professional Edition)
  • 1
  • 2
  • 3

实现代码

视频文件存放在./res目录下

import ctypes

import qtawesome
from PyQt5 import QtCore, QtWidgets
import os
import sys

from PyQt5.QtGui import *
from PyQt5.QtCore import *

from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import QVideoWidget


class VideoPlayer(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        self.setWindowTitle("MediaPlayer")
        self.setWindowIcon(qtawesome.icon('fa.music', color='#F76677'))
        self.setWindowOpacity(0.95)

        self.setFixedSize(600, 500)

        self.widget = QWidget()
        self.layout = QGridLayout()
        self.widget.setLayout(self.layout)
        self.setCentralWidget(self.widget)

        self.video_list = os.listdir('./res/video')

        self.player = QMediaPlayer()
        self.video_widget = QVideoWidget()
        self.player.setVideoOutput(self.video_widget)

        current_video = self.video_list[0]
        del self.video_list[0]

        self.video_url = QUrl('file:.///res/video/%s' % current_video)
        self.video_list.insert(0, current_video)
        self.player.setMedia(QMediaContent(self.video_url))
        self.layout.addWidget(self.video_widget, 0, 0)

        self.player.positionChanged.connect(self.change_progress)
        self.slider = QSlider(Qt.Horizontal, self)

        self.slider.setStyle(QStyleFactory.create('Fusion'))

        self.layout.addWidget(self.slider, 1, 0)

        self.backward_button = QtWidgets.QPushButton(qtawesome.icon('fa.backward', color='#F76677'), "")
        self.backward_button.clicked.connect(self.backward)
        self.backward_button.setIconSize(QtCore.QSize(40, 40))

        self.forward_button = QtWidgets.QPushButton(qtawesome.icon('fa.forward', color='#F76677'), "")
        self.forward_button.clicked.connect(self.forward)
        self.forward_button.setIconSize(QtCore.QSize(40, 40))

        self.play_button = QtWidgets.QPushButton(qtawesome.icon('fa.play', color='#F76677', font=18), "")
        self.play_button.clicked.connect(self.play_video)
        self.play_button.setIconSize(QtCore.QSize(50, 50))

        self.play_console_widget = QtWidgets.QWidget()

        self.play_console_layout = QtWidgets.QGridLayout()
        self.play_console_layout = QtWidgets.QHBoxLayout()
        self.play_console_widget.setLayout(self.play_console_layout)

        self.play_console_layout.addWidget(self.backward_button)
        self.play_console_layout.addWidget(self.play_button)
        self.play_console_layout.addWidget(self.forward_button)

        self.play_console_widget.setStyleSheet('QPushButton{border:none;}')
        self.play_console_widget.setFixedSize(600, 100)

        self.layout.addWidget(self.play_console_widget, 2, 0)

    def change_progress(self, position):
        video_length = self.player.duration() + 0.1
        self.slider.setValue(round((position / video_length) * 100))

    def backward(self):
        self.slider.setValue(0)
        current_video = self.video_list[0]
        del self.video_list[0]
        print(current_video)
        self.player.setMedia(QMediaContent(QUrl('file:.///res/video/%s' % current_video)))
        self.video_list.append(current_video)
        self.play_video()

    def play_video(self):
        self.player.play()
        self.play_button.setIcon(qtawesome.icon('fa.pause', color='#F76677', font=18))
        self.play_button.clicked.connect(self.pause_video)

    def pause_video(self):
        self.player.pause()
        self.play_button.setIcon(qtawesome.icon('fa.play', color='#F76677', font=18))
        self.play_button.clicked.connect(self.play_video)

    def forward(self):
        self.slider.setValue(0)
        current_video = self.video_list.pop()
        self.player.setMedia(QMediaContent(QUrl('file:.///res/video/%s' % current_video)))
        self.video_list.insert(0, current_video)
        self.play_video()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("app")
    video_player = VideoPlayer()
    video_player.show()

    sys.exit(app.exec_())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

实现效果

最后

  • 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/114953
推荐阅读
相关标签
  

闽ICP备14008679号