Qt5 MadiaPlayer
環境 ubuntu 22.04 LTS & Qt5
Qt5Player.py
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QSlider, QFileDialog,QLabel, QMessageBox
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent,QMediaPlaylist
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import Qt, QUrl,QTime
class MediaPlayer(QWidget):
def __init__(self, parent=None):
super(MediaPlayer, self).__init__(parent)
self.setWindowTitle("ssksvr.com MediaPlayer")
self.resize(960, 240)
# changing the background color to ghostwhite
self.setStyleSheet("background-color: ghostwhite;")
self.myinfo = ""
# Create the media player object
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.mediaPlayer.setVolume(70)
self.playlist = QMediaPlaylist(self.mediaPlayer)
# Create label
self.playLabel = QLabel()
self.playLabel.setStyleSheet("background-color: honeydew; border: 0.5px solid black;")
self.lbl = QLabel()
self.elbl = QLabel()
# Create the prev button
self.prevButton = QPushButton("Prev")
self.prevButton.setEnabled(False)
self.prevButton.clicked.connect(self.prev)
self.prevButton.setStyleSheet("background-color: peachpuff;")
# Create the play button
self.playButton = QPushButton("Play")
self.playButton.setEnabled(False)
self.playButton.clicked.connect(self.play)
self.playButton.setStyleSheet("background-color: peachpuff;")
# Create the next button
self.nextButton = QPushButton("Next")
self.nextButton.setEnabled(False)
self.nextButton.clicked.connect(self.next)
self.nextButton.setStyleSheet("background-color: peachpuff;")
# Create the stop button
self.stopButton = QPushButton("Stop")
self.stopButton.setEnabled(False)
self.stopButton.clicked.connect(self.stop)
self.stopButton.setStyleSheet("background-color: peachpuff;")
# Create the slider for seeking
self.seekSlider = QSlider(Qt.Horizontal)
self.seekSlider.setRange(0, 0)
self.seekSlider.sliderMoved.connect(self.setPosition)
self.seekSlider.setStyleSheet("background-color: aquamarine;")
# Create the open button
self.openButton = QPushButton("Open")
self.openButton.clicked.connect(self.openFile)
self.openButton.setStyleSheet("background-color: peachpuff;")
# Create the List button
self.listButton = QPushButton("PlayList")
self.listButton.clicked.connect(self.list)
self.listButton.setStyleSheet("background-color: peachpuff;")
# Create the End button
self.endButton = QPushButton("End")
self.endButton.clicked.connect(self.end)
self.endButton.setStyleSheet("background-color: plum;")
# Create the horizontal layout1 for label
controlLayout1 = QHBoxLayout()
controlLayout1.addWidget(self.playLabel)
# Create the horizontal layout2 for the buttons and slider
controlLayout2 = QHBoxLayout()
controlLayout2.setContentsMargins(0, 0, 0, 0)
controlLayout2.addWidget(self.prevButton)
controlLayout2.addWidget(self.playButton)
controlLayout2.addWidget(self.nextButton)
controlLayout2.addWidget(self.stopButton)
controlLayout2.addWidget(self.lbl)
controlLayout2.addWidget(self.seekSlider)
controlLayout2.addWidget(self.elbl)
controlLayout2.addWidget(self.openButton)
controlLayout2.addWidget(self.listButton)
controlLayout2.addWidget(self.endButton)
# Create the video widget
self.videoWidget = QVideoWidget()
self.videoWidget.setStyleSheet("background-color: honeydew; border: 1px solid blue;")
# Create the vertical layout for the window
mainLayout = QVBoxLayout()
#mainLayout.addLayout(controlLayout1)
mainLayout.addWidget(self.videoWidget)
mainLayout.addLayout(controlLayout2)
# Set the main layout of the window
self.setLayout(mainLayout)
# Connect the media player signals to the appropriate methods
self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
self.mediaPlayer.positionChanged.connect(self.positionChanged)
self.mediaPlayer.durationChanged.connect(self.durationChanged)
self.seekSlider.mousePressEvent = self.__direct_slider_click
self.seekSlider.sliderMoved.connect(self.set_position)
self.videoWidget.mousePressEvent = self.videowidget_click
def openFile(self):
#fileName, _ = QFileDialog.getOpenFileName(self, "Open Media", "", "Video Files (*.mp4 *.avi *.mkv *.ts *.mp3 *.wav *.flac)")
#if fileName != '':
# self.setMedia(fileName)
fileName, _ = QFileDialog.getOpenFileNames(self, "Open Media", "", "Video Files (*.mp4 *.avi *.mkv *.ts *.mp3 *.wav *.flac)")
#self.playLabel.setText("")
self.setWindowTitle("ssksvr.com MediaPlayer")
item2 = ""
self.playlist.clear()
self.mediaPlayer.setPlaylist(self.playlist)
for item in fileName:
if item != '':
# コロンで分割(maxsplit で最大分割数を指定)
split_item = item.split('/', maxsplit=20)
# 2番目の要素、というか最後の要素を取得
print(split_item[-1])
item2 = item2 + "▶[ " + split_item[-1] + " ]" + "\n"
self.myinfo=item2
url = QUrl.fromLocalFile(item)
self.playlist.addMedia(QMediaContent(url))
self.mediaPlayer.setPlaylist(self.playlist)
self.mediaPlayer.playlist().setCurrentIndex(0)
self.playButton.setEnabled(True)
self.stopButton.setEnabled(True)
self.prevButton.setEnabled(True)
self.nextButton.setEnabled(True)
self.mediaPlayer.play()
self.videoWidget.show()
self.mediaPlayer.setVideoOutput(self.videoWidget)
def prev(self):
current = self.mediaPlayer.playlist().currentIndex()
self.mediaPlayer.playlist().setCurrentIndex(current - 1)
self.mediaPlayer.play()
self.videoWidget.show()
self.mediaPlayer.setVideoOutput(self.videoWidget)
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
def next(self):
current = self.mediaPlayer.playlist().currentIndex()
self.mediaPlayer.playlist().setCurrentIndex(current + 1)
self.mediaPlayer.play()
self.videoWidget.show()
self.mediaPlayer.setVideoOutput(self.videoWidget)
def stop(self):
self.mediaPlayer.stop()
def setPosition(self, position):
self.mediaPlayer.setPosition(position)
def mediaStateChanged(self, state):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.playButton.setText("Pause")
else:
self.playButton.setText("Play")
def positionChanged(self, position):
self.seekSlider.setValue(position)
mtime = QTime(0,0,0,0)
mtime = mtime.addMSecs(self.mediaPlayer.position())
self.lbl.setText(mtime.toString())
if self.mediaPlayer.state() == QMediaPlayer.StoppedState:
self.setWindowTitle("ssksvr.com MediaPlayer")
def durationChanged(self, duration):
self.seekSlider.setRange(0, duration)
self.seekSlider.setEnabled(duration > 0)
self.playButton.setEnabled(duration > 0)
self.stopButton.setEnabled(duration > 0)
mtime = QTime(0,0,0,0)
mtime = mtime.addMSecs(self.mediaPlayer.duration())
self.elbl.setText(mtime.toString())
#self.playLabel.setText("[ " + self.mediaPlayer.currentMedia().canonicalUrl().toString() + " ]")
self.setWindowTitle("[ " + self.mediaPlayer.currentMedia().canonicalUrl().toString() + " ]")
if self.mediaPlayer.state() == QMediaPlayer.StoppedState:
self.setWindowTitle("ssksvr.com MediaPlayer")
def __direct_slider_click(self, event):
if event.button() == Qt.LeftButton:
calc_value = self.seekSlider.minimum() + (
(self.seekSlider.maximum() - self.seekSlider.minimum()) *
event.x()) / self.seekSlider.width()
self.seekSlider.setValue(int(calc_value))
self.set_position(calc_value)
event.accept()
QSlider.mousePressEvent(self.seekSlider, event)
def set_position(self, position):
self.mediaPlayer.setPosition(int(position))
self.mediaPlayer.play()
def videowidget_click(self,event):
if event.button() == Qt.LeftButton:
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
QVideoWidget.mousePressEvent(self.videoWidget, event)
def setMedia(self, url):
mediaContent = QMediaContent(QUrl.fromLocalFile(url))
self.mediaPlayer.setMedia(mediaContent)
self.playButton.setEnabled(True)
self.stopButton.setEnabled(True)
self.mediaPlayer.play()
self.videoWidget.show()
self.mediaPlayer.setVideoOutput(self.videoWidget)
def list(self):
msg = QMessageBox()
msg.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SplashScreen)
msg.setGeometry(self.frameGeometry().left() + 30, self.frameGeometry().top() + 30, 300, 400)
msg.setIcon(QMessageBox.Information)
msg.setText("MediaPlayer --- PlayList")
msg.setInformativeText(self.myinfo)
msg.setStandardButtons(QMessageBox.Close)
msg.exec()
def end(self):
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
player = MediaPlayer()
player.show()
sys.exit(app.exec_())
起動画面
Openボタンでファイル選択(複数可能)
Media再生中
PlayListボタンでプレイリスト表示
Prevボタンで前へ移動し再生
Nextボタンで次へ移動し再生
スライダーのマウス移動でその時点から再生
画面の左クリックで再生、一時停止