preface

The text and pictures in this article come from the network, only for learning, communication, do not have any commercial purposes, if you have any questions, please contact us to deal with.

Python crawler, data analysis, website development, etc

https://space.bilibili.com/523606542
Copy the code

The following text is from Charles’ pikachu, written by the man who had dew and shewed me

Here we mainly use PyQt5 to take you to write a simple video player. First, let’s initialize the video player window:

Self.setwindowtitle (' video player ') self.setwindowIcon (QIcon(os.path.join(os.getcwd()), 'images/icon.png'))) self.setGeometry(300, 50, 810, 600) self.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint) palette = QPalette() palette.setColor(QPalette.Background, Qt.gray) self.setPalette(palette)Copy the code

The setWindowTitle function is used to set the window title, and the setWindowIcon function is used to set the window icon:

Next, let’s define some of the necessary components, namely the video player plug-in:

self.video_widget = QVideoWidget(self)
self.video_widget.setGeometry(QRect(5, 5, 800, 520))
palette = QPalette()
palette.setColor(QPalette.Background, Qt.black)
self.video_widget.setPalette(palette)
self.video_widget.setStyleSheet('background-color:#000000')
self.player = QMediaPlayer(self)
self.player.setVideoOutput(self.video_widget)
self.player.setVolume(50)
Copy the code

Components related to selecting a video:

Self.video_line_edit = QLineEdit(" ") # -- select the video button self.select__bTN = QPushButton(' select ')Copy the code

Play and pause buttons (it should be noted that the play and pause buttons cannot be displayed at the same time, that is, by default, the play button is displayed, the pause button is hidden, the play button is hidden after pressing the play button, and the pause button is displayed, and so on) :

Self.play_btn = QPushButton(self) self.play_btn.seticon (QIcon(os.path.join(os.getcwd()), 'images/play.png'))) self.play_btn.setIconSize(QSize(25, 25)) self.play_btn.setStyleSheet('''QPushButton{border:none; }QPushButton:hover{border:none; border-radius:35px; } "") self.play_btn.setcursor (QCursor(qt.pointinghandcursor)) self.play_btn.settooltip (' Play ') self.play_btn.setflat (True) # -- pause self.pause_btn = QPushButton(" ") self.pause_btn.seticon (QIcon(os.path.join(os.getcwd()), 'images/pause.png'))) self.pause_btn.setIconSize(QSize(25, 25)) self.pause_btn.setStyleSheet('''QPushButton{border:none; }QPushButton:hover{border:none; } "") self.pause_btn.setcursor (QCursor(qt.pointinghandcursor)) self.pause_btn.setTooltip (' pause ') self.pause_btn.setFlat(True) self.pause_btn.hide()Copy the code

Components related to playback progress:

Self.play_progress_label = QLabel(' 00:00/00: ') 00') self.play_progress_slider = QSlider(Qt.Horizontal, self) self.play_progress_slider.setMinimum(0) self.play_progress_slider.setSingleStep(1) self.play_progress_slider.setGeometry(QRect(0, 0, 200, 10))Copy the code

Volume control related components:

# -- volume control self.volume_slider = QSlider(qt.horizontal) self.volume_slider. SetMinimum (0) self.volume_slider. SetMaximum (100)  self.volume_slider.setValue(50) self.mute_btn = QPushButton('') self.mute_btn.setIcon(QIcon(os.path.join(os.getcwd(), 'images/sound.png'))) self.mute_btn.setIconSize(QSize(25, 25)) self.mute_btn.setStyleSheet('''QPushButton{border:none; }QPushButton:hover{border:none; } "") self.mute_btn.setcursor (QCursor(qt.pointinghandcursor)) self.mute_btn.setcursor (' forbidden ') self.mute_btn.setflat (True)  self.volume_label = QLabel('50')Copy the code

Then simply automate the layout:

# -- v_layout = QVBoxLayout() v_layout.setspacing (0) v_layout.addStretch() h_layout = QHBoxLayout() h_layout.setSpacing(15) h_layout.addWidget(self.video_line_edit, 2, Qt.AlignVCenter | Qt.AlignVCenter) h_layout.addWidget(self.select_video_btn, 0, Qt.AlignCenter | Qt.AlignVCenter) v_layout.addLayout(h_layout) h_layout = QHBoxLayout() h_layout.setSpacing(2) h_layout.addWidget(self.play_btn, 0, Qt.AlignCenter | Qt.AlignVCenter) h_layout.addWidget(self.pause_btn, 0, Qt.AlignCenter | Qt.AlignVCenter) h_layout.addWidget(self.play_progress_label, 0, Qt.AlignCenter | Qt.AlignVCenter) h_layout.addWidget(self.play_progress_slider, 15, Qt.AlignVCenter | Qt.AlignVCenter) h_layout.addWidget(self.mute_btn, 0, Qt.AlignCenter | Qt.AlignVCenter) h_layout.addWidget(self.volume_slider, 0, Qt.AlignCenter | Qt.AlignVCenter) h_layout.addWidget(self.volume_label, 0, Qt.AlignCenter | Qt.AlignVCenter) v_layout.addLayout(h_layout) self.setLayout(v_layout)Copy the code

Next, let’s implement the functions that each control can trigger. The first is the play and pause function, this is very simple, the code is as follows:

Def playvideo(self): if self.player.duration() == 0: Return self.play_btn.hide() self.pause_btn.show() self.player.play() "def pausevideo(self): if self.player.duration() == 0: return self.play_btn.show() self.pause_btn.hide() self.player.pause()Copy the code

It should be noted that when clicking the play and pause buttons, the state of the buttons should be switched at the same time (that is, after clicking the pause button, the play button should be displayed to hide the pause button, and after clicking the play button, the play button should be displayed to hide the play button, so as to achieve the effect of button switching).

During playback, remember to change the display of the progress bar in real time:

Def setPlayProgress(self): _, right = self.play_progress_label.text().split('/') position = self.player.position() + 1 second = int(position / 1000 % 60) minute = int(position / 1000 / 60) left = str(minute).zfill(2) + ':' + str(second).zfill(2) self.play_progress_label.setText(left + ' /' + right) self.play_progress_slider.setValue(position)Copy the code

By the way, before playing the video, we need to open the video file. The function is as follows:

Def openVideo (self): # open and display video path filepath. = QFileDialog getOpenFileName (self, 'please select video', '. ') if filepath [0] : Self.video_line_edit.settext (filepath[0]) # Filepath = self.video_line_edit.text() if not os.path.exists(filepath): return fileurl = QUrl.fromLocalFile(filepath) if fileurl.isValid(): self.player.setMedia(QMediaContent(fileurl)) self.player.setVolume(50)Copy the code

Next is the ability to drag the video playback progress bar:

"' playback progress bar press ing events' 'def playProgressSliderPressed (self) : if the self. The player. The state ()! = 0: self. Player. Pause () ' ' 'playback progress bar press release events'' def playProgressSliderReleased (self) : if the self. The player. The state ()! = 0: self.player.setPosition(self.play_progress_slider.value()) self.player.play()Copy the code

That is, pause the video while dragging the progress bar, and switch the video progress to the corresponding position after dragging the progress bar.

Volume control function (this principle and drag video playback progress bar implementation is similar) :

Def setVolume(self): value = self.volume_slider.value() if value: self.player.setMuted(False) self.player.setVolume(value) self.volume_label.setText(str(value)) self.volume_slider.setValue(value) self.mute_btn.setIcon(QIcon(os.path.join(os.getcwd(), 'images/sound.png'))) else: self.player.setMuted(True) self.volume_label.setText('0') self.volume_slider.setValue(0) self.mute_btn.setIcon(QIcon(os.path.join(os.getcwd(), 'images/mute.png')))Copy the code

One last small detail, when the video player’s window size changes, we adaptively change the player plug-in’s window size:

Def resizeEvent(self, event): size = event.size() self.video_widget.setGeometry(5, 5, size.width() - 5, size.height() - 80)Copy the code