-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
353 lines (285 loc) · 13.1 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
"""
@author: GitHub@Oscarshu0719
"""
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtGui import QIcon
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QAction, QComboBox, QFileDialog, QHBoxLayout,
QLabel, QMainWindow, QPushButton, QShortcut,
QSlider, QStyle, QVBoxLayout, QWidget)
import sys
from src.thread import Thread
class VideoWindow(QMainWindow):
""" Class:
Video player window.
"""
# Main window size.
WIN_SIZE = [800, 600]
def __init__(self, parent=None):
""" Function:
Setup user interface of Video player window.
"""
super(VideoWindow, self).__init__(parent)
self.setWindowTitle("Video player")
self.resize(VideoWindow.WIN_SIZE[0], VideoWindow.WIN_SIZE[1])
self.setWindowIcon(
self.style().standardIcon(QStyle.SP_DriveDVDIcon))
self.video_player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.is_finished = True
self.thread = None
self.record_start_time = None
self.record_end_time = None
self.video_name = ""
self.widget_video = QVideoWidget()
self.statusbar = QtWidgets.QStatusBar(self)
self.setStatusBar(self.statusbar)
self.button_play = QPushButton()
self.button_play.setEnabled(False)
self.button_play.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.button_play.clicked.connect(self.play_video)
self.video_slider = QSlider(Qt.Horizontal)
self.video_slider.setRange(0, 0)
self.video_slider.sliderMoved.connect(self.set_position)
self.video_duration = 0
# Action 'Open'.
self.action_open = QAction(QIcon('open.png'), '&Open', self)
self.action_open.setShortcut('Ctrl+O')
self.action_open.setStatusTip('Open a video')
self.action_open.triggered.connect(self.open_video)
# Menu bar.
self.menu_bar = self.menuBar()
self.menu_menu = self.menu_bar.addMenu('&Menu')
self.menu_menu.addAction(self.action_open)
# Widget.
self.widget_window = QWidget(self)
self.setCentralWidget(self.widget_window)
self.layout_operation = QHBoxLayout()
self.layout_operation.setContentsMargins(0, 0, 0, 0)
self.label_rotate = QLabel('Degree of rotation (clockwise)')
self.combobox_degree = QComboBox()
degrees = ['0', '90', '180', '270']
self.combobox_degree.addItems(degrees)
self.button_subclip_video = QPushButton('Subclip (Video)')
self.button_subclip_audio = QPushButton('Subclip (Audio)')
self.layout_operation.addWidget(self.label_rotate)
self.layout_operation.addWidget(self.combobox_degree)
self.layout_operation.addWidget(self.button_subclip_video)
self.layout_operation.addWidget(self.button_subclip_audio)
self.layout_record = QHBoxLayout()
self.layout_record.setContentsMargins(0, 0, 0, 0)
self.button_start = QPushButton('Start')
self.button_end = QPushButton('End')
self.button_clear = QPushButton('Clear')
self.layout_record.addWidget(self.button_start)
self.layout_record.addWidget(self.button_end)
self.layout_record.addWidget(self.button_clear)
self.button_start.clicked.connect(self.record_start)
self.button_end.clicked.connect(self.record_end)
self.button_subclip_video.clicked.connect(self.record_subclip_video)
self.button_subclip_audio.clicked.connect(self.record_subclip_audio)
self.button_clear.clicked.connect(self.record_clear)
# Widget layout.
self.layout_widget = QHBoxLayout()
self.layout_widget.setContentsMargins(0, 0, 0, 0)
self.layout_widget.addWidget(self.button_play)
self.layout_widget.addWidget(self.video_slider)
self.layout_window = QVBoxLayout()
self.layout_window.addWidget(self.widget_video)
self.layout_window.addLayout(self.layout_record)
self.layout_window.addLayout(self.layout_operation)
self.layout_window.addLayout(self.layout_widget)
# Window layout.
self.widget_window.setLayout(self.layout_window)
self.video_player.setVideoOutput(self.widget_video)
self.video_player.stateChanged.connect(self.media_state_changed)
self.video_player.positionChanged.connect(self.position_changed)
self.video_player.durationChanged.connect(self.duration_changed)
self.video_player.error.connect(self.error_control)
QShortcut(Qt.Key_Up, self, self.arrow_up)
QShortcut(Qt.Key_Down, self, self.arrow_down)
QShortcut(Qt.Key_Left, self, self.arrow_left_event)
QShortcut(Qt.Key_Right, self, self.arrow_right_event)
QShortcut(Qt.Key_Space, self, self.play_video)
def arrow_up(self):
if self.video_player.state() != QMediaPlayer.StoppedState:
self.video_player.setVolume(min(self.video_player.volume() + 10, 100))
def arrow_down(self):
if self.video_player.state() != QMediaPlayer.StoppedState:
self.video_player.setVolume(max(self.video_player.volume() - 10, 0))
def arrow_left_event(self):
""" Slot function:
Action after the key 'arrow left' is pressed.
Fast-forward to 10 seconds later.
"""
self.set_position(self.video_slider.value() - 10 * 1000)
def arrow_right_event(self):
""" Slot function:
Action after the key 'arrow right' is pressed.
Go back to 10 seconds ago.
"""
self.set_position(self.video_slider.value() + 10 * 1000)
def mousePressEvent(self, event):
""" Slot function:
The starting position of the slider is 50.
Note: This function still can't not accurately move the slider to the
clicked position.
"""
slider_start_pos = self.video_slider.geometry().topLeft().x()
if 42 <= self.height() - event.pos().y() <= 62:
position = self.video_slider.minimum() + (event.pos().x() - slider_start_pos) / self.video_slider.width() * self.video_duration
if position != self.video_slider.sliderPosition():
self.set_position(position)
def open_video(self):
""" Slot function:
Open a video from the file system.
"""
video_name, _ = QFileDialog.getOpenFileName(self, "Open Movie",
QDir.homePath())
self.video_name = video_name
if self.video_name != '':
if self.video_name[-4: ] == '.flv':
self.statusbar.showMessage(self.video_name[-4: ])
self.video_name = self.flv2mp4(self.video_name, self.video_name[: -4] + '.mp4')
self.video_player.setMedia(
QMediaContent(QUrl.fromLocalFile(self.video_name)))
self.button_play.setEnabled(True)
self.video_player.play()
index = self.video_name.rfind('/')
self.statusbar.showMessage(
"Info: Playing the video '" + self.video_name[(index + 1):]
+ "' ...")
def flv2mp4(self, video_name, output_name='tmp.mp4'):
import subprocess
command = [
'ffmpeg', '-y', '-i', video_name, '-c', 'copy', '-copyts', output_name
]
ffmpeg = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
ffmpeg.communicate()
return output_name
def play_video(self):
""" Slot function:
The slot function for the 'play' button.
If the video player is currently paused, then play the video;
otherwise, pause the video.
"""
if self.video_player.state() == QMediaPlayer.PlayingState:
self.video_player.pause()
else:
self.video_player.play()
def media_state_changed(self, state):
""" Slot function:
If the playing state changes, change the icon for the 'play' button.
If the video player is currently playing, change the icon to 'pause';
otherwise, change the icon to 'play'.
"""
if self.video_player.state() == QMediaPlayer.PlayingState:
self.button_play.setIcon(
self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.button_play.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay))
def position_changed(self, position):
""" Slot function:
Change the position of the slider.
"""
self.video_slider.setValue(position)
def duration_changed(self, duration):
""" Slot function:
If the duration of the video changed, change the range of the slider.
This slot function is called after opening a video.
"""
self.video_slider.setRange(0, duration)
self.video_duration = duration
self.record_start_time = 0
self.record_end_time = 0
def set_position(self, position):
""" Slot function:
Change the progress of the video.
"""
self.video_player.setPosition(position)
def error_control(self):
""" Slot function:
If an error occurs while opening the video, this slot function is
called.
"""
self.button_play.setEnabled(False)
self.statusbar.showMessage(
"Error: An error occurs while opening the video.")
def record_start(self):
self.record_start_time = self.video_slider.sliderPosition()
if self.record_end_time is not None and self.record_end_time != 0 and self.record_start_time > self.record_end_time:
self.record_start_time, self.record_end_time = self.record_end_time, self.record_start_time
self._show_record_time()
def record_end(self):
self.record_end_time = self.video_slider.sliderPosition()
if self.record_start_time is not None and self.record_start_time > self.record_end_time:
self.record_start_time, self.record_end_time = self.record_end_time, self.record_start_time
self._show_record_time()
def _show_record_time(self):
if self.record_start_time is not None and self.record_end_time is not None:
self.statusbar.showMessage(
"Info: Starting time: ({}), and Ending time: ({}) (Duration: {}).".format(
self.record_start_time / 1000, self.record_end_time / 1000, self.video_duration / 1000))
def _check_duration(self):
if self.video_name == "":
self.statusbar.showMessage(
"Error: Please open a video first.")
elif self.record_start_time == self.record_end_time:
self.statusbar.showMessage(
"Error: Duration can NOT be 0.")
elif self.record_start_time > self.record_end_time:
self.statusbar.showMessage(
"Error: The start time should be earlier than the end time.")
else:
return True
return False
def record_subclip_video(self):
if self._check_duration():
self.statusbar.showMessage(
"Info: Please wait until the process ends.")
self.thread = Thread()
self.is_finished = False
self.thread.set_params(Thread.MSG_CUT_VIDEO, self.video_name,
self.record_start_time / 1000, self.record_end_time / 1000, self.combobox_degree.currentText())
self.thread.signal_return_value.connect(self.thread_done)
self.thread.start()
def record_subclip_audio(self):
if self._check_duration():
self.statusbar.showMessage(
"Info: Please wait until the process ends.")
self.thread = Thread()
self.is_finished = False
self.thread.set_params(Thread.MSG_EXTRACT_AUDIO, self.video_name,
self.record_start_time / 1000, self.record_end_time / 1000)
self.thread.signal_return_value.connect(self.thread_done)
self.thread.start()
def record_clear(self):
self.record_start_time = 0
self.record_end_time = 0
self.statusbar.showMessage(
"Info: Starting time: ({}), and Ending time: ({}).".format(self.record_start_time, self.record_end_time))
def thread_done(self, return_value, video_name):
if return_value:
self.is_finished = True
self.statusbar.showMessage(
"Info: The process has done and saved as {}.".format(video_name))
def closeEvent(self, event):
if not self.is_finished:
self.thread.stop()
event.accept()
if __name__ == '__main__':
"""
app = QApplication(sys.argv) will go wrong.
"""
app = QtCore.QCoreApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
player = VideoWindow()
player.show()
"""
sys.exit(app.exec_()) will go wrong.
"""
app.exec_()