python pyqt5 视频播放器代码
时间: 2023-04-12 15:02:05 浏览: 222
我可以回答这个问题。以下是一个简单的 Python PyQT5 视频播放器代码示例:
```python
import sys
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QLabel, QSlider, QStyle, QSizePolicy, QFileDialog, QErrorMessage
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
class VideoPlayer(QWidget):
def __init__(self):
super().__init__()
# 创建媒体播放器和视频窗口
self.media_player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.video_widget = QVideoWidget()
# 创建播放、暂停、停止、打开文件按钮
self.play_button = QPushButton()
self.play_button.setEnabled(False)
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.play_button.clicked.connect(self.play)
self.pause_button = QPushButton()
self.pause_button.setEnabled(False)
self.pause_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
self.pause_button.clicked.connect(self.pause)
self.stop_button = QPushButton()
self.stop_button.setEnabled(False)
self.stop_button.setIcon(self.style().standardIcon(QStyle.SP_MediaStop))
self.stop_button.clicked.connect(self.stop)
self.open_button = QPushButton()
self.open_button.setIcon(self.style().standardIcon(QStyle.SP_DialogOpenButton))
self.open_button.clicked.connect(self.open_file)
# 创建音量滑块
self.volume_slider = QSlider(Qt.Horizontal)
self.volume_slider.setRange(0, 100)
self.volume_slider.setValue(100)
self.volume_slider.setSingleStep(1)
self.volume_slider.setPageStep(5)
self.volume_slider.valueChanged.connect(self.set_volume)
# 创建时间标签
self.time_label = QLabel()
self.time_label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum)
# 创建布局
control_layout = QHBoxLayout()
control_layout.setContentsMargins(0, 0, 0, 0)
control_layout.addWidget(self.play_button)
control_layout.addWidget(self.pause_button)
control_layout.addWidget(self.stop_button)
control_layout.addWidget(self.open_button)
control_layout.addWidget(self.volume_slider)
control_layout.addWidget(self.time_label)
layout = QVBoxLayout()
layout.addWidget(self.video_widget)
layout.addLayout(control_layout)
self.setLayout(layout)
# 设置媒体播放器和视频窗口
self.media_player.setVideoOutput(self.video_widget)
self.media_player.stateChanged.connect(self.media_state_changed)
self.media_player.positionChanged.connect(self.position_changed)
self.media_player.durationChanged.connect(self.duration_changed)
def play(self):
if self.media_player.state() == QMediaPlayer.PlayingState:
self.media_player.pause()
else:
self.media_player.play()
def pause(self):
self.media_player.pause()
def stop(self):
self.media_player.stop()
def open_file(self):
file_name, _ = QFileDialog.getOpenFileName(self, "Open Video", "", "Video Files (*.mp4 *.avi *.mkv)")
if file_name != '':
self.media_player.setMedia(QMediaContent(QUrl.fromLocalFile(file_name)))
self.play_button.setEnabled(True)
self.pause_button.setEnabled(True)
self.stop_button.setEnabled(True)
def set_volume(self, value):
self.media_player.setVolume(value)
def media_state_changed(self, state):
if state == QMediaPlayer.PlayingState:
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
def position_changed(self, position):
self.time_label.setText("{:02d}:{:02d}".format(position // 60000, (position // 1000) % 60))
def duration_changed(self, duration):
self.time_label.setText("{:02d}:{:02d}".format(duration // 60000, (duration // 1000) % 60))
if __name__ == '__main__':
app = QApplication(sys.argv)
player = VideoPlayer()
player.show()
sys.exit(app.exec_())
```
希望这个代码能够帮到你!
阅读全文