PyQt5: simple video player

This article aims to introduce how to use PyQt5 to make a simple video player ...

This article aims to introduce how to use PyQt5 to make a simple video player

ps: some netizens may encounter the problem that they can only play video in avi format, but can't play video in mp4 format (directshowplayerservice:: dorender: unresolved error code 0x8040266 (IDispatch error ා 102)). This requires downloading a decoder. Please refer to This blog.



Version 1: the easiest player

  directly select QT's own player and components~

from PyQt5.QtWidgets import QApplication, QFileDialog from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer from PyQt5.QtMultimediaWidgets import QVideoWidget if __name__ == '__main__': app = QApplication([]) player = QMediaPlayer() wgt_video = QVideoWidget() # Video display widget wgt_video.show() player.setVideoOutput(wgt_video) # widget for video output player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) # Select video file player.play() app.exec_()

  of course, you can also directly specify the video directory in the program, just replace the following statement:

from PyQt5.Qt import QUrl # player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) # Select video file player.setMedia(QMediaContent(QUrl.fromLocalFile(r'C:\Users\lenovo\Desktop\crawler.mp4')))





Version 2: self made player interface

  here, Qt Designer is used to add an interface to our simple player and provide buttons for selecting videos~

Firstly, we need to design our simple interface layout in Qt Designer:

Note here that the Widget used for video playback is not directly given in the toolbox on the left. It needs to be added by yourself. Please refer to Here , you can also refer to the following GIF demonstration. (where is the Widget promotion setting, the promoted class name is QVideoWidget, and the header file is pyqt5. Qtmultimedia widgets)

  the source code is given below. Note that BTN [Select] and WGT [player] are the names given to the two controls in the designer. Video [1. UI] is the file saved by the designer above~

from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer from PyQt5.QtWidgets import QFileDialog, QApplication from PyQt5 import uic class videoPlayer: def __init__(self): self.ui = uic.loadUi('video_1.ui') # Loading the ui program designed by designer self.player = QMediaPlayer() self.player.setVideoOutput(self.ui.wgt_player) self.ui.btn_select.clicked.connect(self.openVideoFile) # Open video file and play def openVideoFile(self): self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) self.player.play() if __name__ == "__main__": app = QApplication([]) myPlayer = videoPlayer() myPlayer.ui.show() app.exec()


Version 3: add progress control

  next, add progress control to our player, which can perform "play / pause", "progress bar display and control", "remaining time display" and other functions on video~
  the designer is designed as follows:

  the implementation effect is as follows: (play/pause button can play pause video, progress bar can change the video progress, and there is time left to display)

  this mainly involves the simple use of the signal / signal slot of some controls. The source code is as follows:

from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer from PyQt5.QtWidgets import QFileDialog, QApplication from PyQt5 import uic class videoPlayer: def __init__(self): # Initialization self.ui = uic.loadUi('video_1.ui') # Loading the ui program designed by designer # player self.player = QMediaPlayer() self.player.setVideoOutput(self.ui.wgt_player) # Button self.ui.btn_select.clicked.connect(self.open) self.ui.btn_play_pause.clicked.connect(self.playPause) # Progress bar self.player.durationChanged.connect(self.getDuration) self.player.positionChanged.connect(self.getPosition) self.ui.sld_duration.sliderMoved.connect(self.updatePosition) # Open video file def open(self): self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) self.player.play() # Play video def playPause(self): if self.player.state()==1: self.player.pause() else: self.player.play() # Total video time acquisition def getDuration(self, d): '''d Is the total length of video captured( ms)''' self.ui.sld_duration.setRange(0, d) self.ui.sld_duration.setEnabled(True) self.displayTime(d) # Video real-time location acquisition def getPosition(self, p): self.ui.sld_duration.setValue(p) self.displayTime(self.ui.sld_duration.maximum()-p) # Show time remaining def displayTime(self, ms): minutes = int(ms/60000) seconds = int((ms-minutes*60000)/1000) self.ui.lab_duration.setText('{}:{}'.format(minutes, seconds)) # Update video location with progress bar def updatePosition(self, v): self.player.setPosition(v) self.displayTime(self.ui.sld_duration.maximum()-v) if __name__ == "__main__": app = QApplication([]) myPlayer = videoPlayer() myPlayer.ui.show() app.exec()


Version 4: Design

  after the basic functions are completed, some face-to-face projects are carried out, such as setting window icons and titles, setting button icons, layout design, setting background, etc

MCPRL_Iris Published 37 original articles, won praise 1, visited 781 Private letter follow

12 February 2020, 12:21 | Views: 6459

Add new comment

For adding a comment, please log in
or create account

0 comments