-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.py
162 lines (132 loc) · 5.99 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget, QMainWindow,
QAction, QShortcut, QCheckBox, QGridLayout)
from PyQt5.QtGui import QIcon, QKeySequence
import sys
class VideoWindow(QMainWindow):
def __init__(self, parent=None):
super(VideoWindow, self).__init__(parent)
self.setWindowTitle("TILU Media Player")
self.setWindowIcon(QIcon("icon.png"))
self.mediaPlayers = []
self.videoWidgets = []
self.selector = []
for i in range(4):
self.mediaPlayers.append(QMediaPlayer(
None, QMediaPlayer.VideoSurface))
self.videoWidgets.append(QVideoWidget())
self.mediaPlayers[i].setVideoOutput(self.videoWidgets[i])
# create video layout
upperLayout = QHBoxLayout()
upperLayout.setContentsMargins(0, 0, 0, 0)
upperLayout.addWidget(self.videoWidgets[0])
upperLayout.addWidget(self.videoWidgets[1])
bottomLayout = QHBoxLayout()
bottomLayout.setContentsMargins(0, 0, 0, 0)
bottomLayout.addWidget(self.videoWidgets[2])
bottomLayout.addWidget(self.videoWidgets[3])
finalLayout = QVBoxLayout()
finalLayout.setContentsMargins(0, 0, 0, 0)
finalLayout.addLayout(upperLayout)
finalLayout.addLayout(bottomLayout)
# Create play button and shortcuts
self.playButton = QPushButton()
self.playButton.setEnabled(False)
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.playButton.clicked.connect(self.play)
playShortcut = QShortcut(QKeySequence("Space"), self)
playShortcut.activated.connect(self.play)
self.positionSlider = QSlider(Qt.Horizontal)
self.positionSlider.setRange(0, 0)
self.positionSlider.sliderMoved.connect(self.setPosition)
self.errorLabel = QLabel()
self.errorLabel.setSizePolicy(QSizePolicy.Preferred,
QSizePolicy.Maximum)
sidebar = QVBoxLayout()
sidebar.insertSpacing(0, 100)
# Create open button
openButton = QPushButton(QIcon('open.png'), "Open Video", self)
openButton.setToolTip("Open movie")
openButton.clicked.connect(self.openFile)
sidebar.addWidget(openButton)
openShortcut = QShortcut(QKeySequence("Ctrl+O"), self)
openShortcut.activated.connect(self.openFile)
# Checkboxes for displaying video
for i in range(4):
boxId = str(i+1)
checkbox = QCheckBox("Video &" + boxId, self)
checkbox.setChecked(True)
shortcut = QShortcut(QKeySequence(boxId), self)
self.selector.append(checkbox)
sidebar.addWidget(self.selector[i])
displayButton = QPushButton("Display Selected", self)
displayButton.setToolTip("Display only selected videos on screen")
displayButton.clicked.connect(self.selectVideo)
sidebar.addWidget(displayButton)
displayShortcut = QShortcut(QKeySequence("Ctrl+D"), self)
displayShortcut.activated.connect(self.selectVideo)
# Create a widget for window contents
wid = QWidget(self)
self.setCentralWidget(wid)
# Create layouts to place inside widget
controlLayout = QHBoxLayout()
controlLayout.setContentsMargins(0, 0, 0, 0)
controlLayout.addWidget(self.playButton)
controlLayout.addWidget(self.positionSlider)
mainScreen = QGridLayout()
mainScreen.addLayout(finalLayout, 0, 0)
mainScreen.addLayout(controlLayout, 3, 0)
mainScreen.addWidget(self.errorLabel, 4, 0)
sidebar.addStretch()
mainScreen.addLayout(sidebar, 0, 1)
# Set widget to contain window contents
wid.setLayout(mainScreen)
for i in self.mediaPlayers:
i.stateChanged.connect(self.mediaStateChanged)
i.positionChanged.connect(self.positionChanged)
i.durationChanged.connect(self.durationChanged)
i.error.connect(self.handleError)
def selectVideo(self):
for s, w in zip(self.selector, self.videoWidgets):
if s.isChecked():
w.show()
else:
w.hide()
def openFile(self):
files, _ = QFileDialog.getOpenFileNames(
self, "Select up to 4 files", QDir.homePath())
for m, f in zip(self.mediaPlayers, files):
m.setMedia(QMediaContent(QUrl.fromLocalFile(f)))
self.playButton.setEnabled(True)
def play(self):
for i in self.mediaPlayers:
if i.state() == QMediaPlayer.PlayingState:
i.pause()
else:
i.play()
def mediaStateChanged(self, state):
if self.mediaPlayers[0].state() == QMediaPlayer.PlayingState:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay))
def positionChanged(self, position):
self.positionSlider.setValue(position)
def durationChanged(self, duration):
self.positionSlider.setRange(0, duration)
def setPosition(self, position):
for i in self.mediaPlayers:
i.setPosition(position)
def handleError(self):
self.playButton.setEnabled(False)
self.errorLabel.setText("Error: " + self.mediaPlayers[0].errorString())
if __name__ == '__main__':
app = QApplication(sys.argv)
player = VideoWindow()
player.showMaximized()
sys.exit(app.exec_())