python编写视频播放器
时间: 2023-10-17 20:05:10 浏览: 168
Python可以通过使用第三方库来实现视频播放器的功能,其中比较常用的库包括PyQt5、Tkinter和wxPython等。下面以PyQt5为例,给出一个简单的视频播放器实现:
```python
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QLabel, QPushButton, QSlider, QHBoxLayout, QVBoxLayout, QWidget, QStyle, QStyleFactory, QSizePolicy, QSpacerItem
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtMultimediaWidgets import QVideoWidget
class VideoPlayer(QMainWindow):
def __init__(self):
super().__init__()
# 设置窗口标题
self.setWindowTitle("视频播放器")
# 设置窗口图标
self.setWindowIcon(QIcon("video.png"))
# 创建主窗口
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
# 创建媒体播放器
self.media_player = QMediaPlayer(self)
# 创建视频窗口
self.video_widget = QVideoWidget(self)
self.media_player.setVideoOutput(self.video_widget)
# 创建控制栏
self.play_button = QPushButton()
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.play_button.clicked.connect(self.play_clicked)
self.stop_button = QPushButton()
self.stop_button.setIcon(self.style().standardIcon(QStyle.SP_MediaStop))
self.stop_button.clicked.connect(self.stop_clicked)
self.volume_slider = QSlider(Qt.Horizontal)
self.volume_slider.setRange(0, 100)
self.volume_slider.setValue(self.media_player.volume())
self.volume_slider.valueChanged.connect(self.set_volume)
self.position_slider = QSlider(Qt.Horizontal)
self.position_slider.setRange(0, 0)
self.position_slider.sliderMoved.connect(self.set_position)
self.time_label = QLabel("00:00:00 / 00:00:00")
control_layout = QHBoxLayout()
control_layout.addWidget(self.play_button)
control_layout.addWidget(self.stop_button)
control_layout.addWidget(self.volume_slider)
control_layout.addWidget(self.position_slider)
control_layout.addWidget(self.time_label)
control_widget = QWidget(self)
control_widget.setLayout(control_layout)
# 创建布局
layout = QVBoxLayout()
layout.addWidget(self.video_widget)
layout.addWidget(control_widget)
self.central_widget.setLayout(layout)
# 设置窗口大小
self.resize(800, 600)
def play_clicked(self):
# 判断是否正在播放
if self.media_player.state() == QMediaPlayer.PlayingState:
self.media_player.pause()
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
else:
self.media_player.play()
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
def stop_clicked(self):
self.media_player.stop()
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.position_slider.setValue(0)
def set_volume(self, value):
self.media_player.setVolume(value)
def set_position(self, value):
self.media_player.setPosition(value)
def update_position(self, position):
self.position_slider.setValue(position)
def update_duration(self, duration):
self.position_slider.setRange(0, duration)
# 更新时间标签
hours = duration // 3600
minutes = (duration % 3600) // 60
seconds = duration % 60
self.time_label.setText("00:00:00 / {:02d}:{:02d}:{:02d}".format(hours, minutes, seconds))
def update_time(self, position):
# 更新时间标签
hours = position // 3600
minutes = (position % 3600) // 60
seconds = position % 60
self.time_label.setText("{:02d}:{:02d}:{:02d} / ".format(hours, minutes, seconds) +
self.time_label.text().split(" / ")[1])
def open_file(self):
# 打开文件对话框
file_name, _ = QFileDialog.getOpenFileName(self, "打开视频文件", "", "视频文件 (*.mp4 *.avi *.mov)")
if file_name != "":
# 设置媒体内容
media_content = QMediaContent()
media_content.setUrl(file_name)
self.media_player.setMedia(media_content)
# 更新窗口标题
self.setWindowTitle("视频播放器 - " + file_name.split("/")[-1])
# 更新时间标签
self.time_label.setText("00:00:00 / 00:00:00")
# 更新控制栏
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.position_slider.setValue(0)
# 开始播放
self.media_player.play()
# 更新视频时长
self.media_player.durationChanged.connect(self.update_duration)
# 更新播放进度
self.media_player.positionChanged.connect(self.update_position)
# 更新时间标签
self.media_player.positionChanged.connect(self.update_time)
def keyPressEvent(self, event):
# 按下空格键时暂停/播放
if event.key() == Qt.Key_Space:
self.play_clicked()
def closeEvent(self, event):
# 释放媒体播放器
self.media_player.stop()
self.media_player.setMedia(QMediaContent())
self.media_player = None
if __name__ == "__main__":
app = QApplication(sys.argv)
# 设置应用程序风格
app.setStyle(QStyleFactory.create("Fusion"))
# 创建视频播放器
video_player = VideoPlayer()
# 打开视频文件
video_player.open_file()
# 显示窗口
video_player.show()
# 运行应用程序
sys.exit(app.exec_())
```
这个视频播放器具有基本的播放、暂停、停止、音量调节、进度条、时间标签等功能,可以打开多种格式的视频文件进行播放。当然,这只是一个简单的实现,如果需要更多的功能,需要在代码中做更多的修改。
阅读全文