pyqt5 video player

Solutions on MaxInterview for pyqt5 video player by the best coders in the world

showing results for - "pyqt5 video player"
Vicente
07 Nov 2017
1from PyQt5.QtCore import QDir, Qt, QUrl
2from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
3from PyQt5.QtMultimediaWidgets import QVideoWidget
4from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
5        QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
6from PyQt5.QtWidgets import QMainWindow,QWidget, QPushButton, QAction
7from PyQt5.QtGui import QIcon
8import sys
9 
10class VideoPlayer(QMainWindow):
11    def __init__(self):
12        super().__init__()
13        self.setWindowTitle("PyQt5 Video Player") 
14 
15        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
16 
17        videoWidget = QVideoWidget()
18 
19        self.playButton = QPushButton()
20        self.playButton.setEnabled(False)
21        self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
22        self.playButton.clicked.connect(self.play)
23 
24        self.positionSlider = QSlider(Qt.Horizontal)
25        self.positionSlider.setRange(0, 0)
26        self.positionSlider.sliderMoved.connect(self.setPosition)
27 
28        self.error = QLabel()
29        self.error.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum)
30 
31        openButton = QPushButton("Open Video")   
32        openButton.setToolTip("Open Video File")
33        openButton.setStatusTip("Open Video File")
34        openButton.setFixedHeight(24)
35        openButton.clicked.connect(self.openFile)
36 
37 
38        # Create a widget for window contents
39        wid = QWidget(self)
40        self.setCentralWidget(wid)
41 
42        # Create layouts to place inside widget
43        controlLayout = QHBoxLayout()
44        controlLayout.setContentsMargins(0, 0, 0, 0)
45        controlLayout.addWidget(self.playButton)
46        controlLayout.addWidget(self.positionSlider)
47 
48        layout = QVBoxLayout()
49        layout.addWidget(videoWidget)
50        layout.addLayout(controlLayout)
51        layout.addWidget(self.error)
52        layout.addWidget(openButton)
53 
54        # Set widget to contain window contents
55        wid.setLayout(layout)
56 
57        self.mediaPlayer.setVideoOutput(videoWidget)
58        self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
59        self.mediaPlayer.positionChanged.connect(self.positionChanged)
60        self.mediaPlayer.durationChanged.connect(self.durationChanged)
61        self.mediaPlayer.error.connect(self.handleError)
62 
63    def openFile(self):
64        fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie",
65                QDir.homePath())
66 
67        if fileName != '':
68            self.mediaPlayer.setMedia(
69                    QMediaContent(QUrl.fromLocalFile(fileName)))
70            self.playButton.setEnabled(True)
71 
72    def exitCall(self):
73        sys.exit(app.exec_())
74 
75    def play(self):
76        if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
77            self.mediaPlayer.pause()
78        else:
79            self.mediaPlayer.play()
80 
81    def mediaStateChanged(self, state):
82        if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
83            self.playButton.setIcon(
84                    self.style().standardIcon(QStyle.SP_MediaPause))
85        else:
86            self.playButton.setIcon(
87                    self.style().standardIcon(QStyle.SP_MediaPlay))
88 
89    def positionChanged(self, position):
90        self.positionSlider.setValue(position)
91 
92    def durationChanged(self, duration):
93        self.positionSlider.setRange(0, duration)
94 
95    def setPosition(self, position):
96        self.mediaPlayer.setPosition(position)
97 
98    def handleError(self):
99        self.playButton.setEnabled(False)
100        self.error.setText("Error: " + self.mediaPlayer.errorString())
101 
102 
103app = QApplication(sys.argv)
104videoplayer = VideoPlayer()
105videoplayer.resize(640, 480)
106videoplayer.show()
107sys.exit(app.exec_())
108