PyQt5 视频播放器

PyQt5 视频播放器

环境

Python 3.7.4
PyQt5==5.15.0
PyCharm 2020.2.4 (Professional Edition)

实现代码

视频文件存放在./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_())

实现效果

最后

  • 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!

猜你喜欢

转载自blog.csdn.net/qq_44486439/article/details/113106526