-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUI.py
300 lines (241 loc) · 10.8 KB
/
GUI.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
# -*- coding: utf-8 -*-
"""
This is a sample GUI for LVDOWin written by Aaron Gokaslan.
The GUI just feeds a very simplistic set of parameters to and from video.
"""
from PySide import QtGui
from PySide.QtCore import Qt, QRegExp
from subprocess import Popen, PIPE, STDOUT
import os
########################################################################
class OsirisGUI(QtGui.QWidget):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
# super(DialogDemo, self).__init__()
QtGui.QWidget.__init__(self)
self.label = QtGui.QLabel("Welcome to LVDOWin!")
self.label.setAlignment(Qt.AlignCenter| Qt.AlignCenter)
self.te = QtGui.QTextEdit()
self.te.setEnabled(False)
# create the buttons
encodeFileBtn = QtGui.QPushButton("Convert file to video")
decodeFileBtn = QtGui.QPushButton("Convert a video to a file")
# connect the buttons to the functions (signals to slots)
encodeFileBtn.clicked.connect(self.encodeFile)
decodeFileBtn.clicked.connect(self.decodeFile)
encodeFileBtn.setToolTip("Saves the file as output.mkv")
decodeFileBtn.setToolTip("Decodes the file at the specified")
buttonLayout = QtGui.QHBoxLayout()
buttonLayout.addWidget(encodeFileBtn)
buttonLayout.addWidget(decodeFileBtn)
io_box = self.createIOBox()
settings_box = self.createSettings()
layout = QtGui.QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(io_box)
#layout.addWidget(settings_box)
layout.addWidget(self.te)
layout.addLayout(buttonLayout)
self.setLayout(layout)
self.adjustSize()
self.move(QtGui.QDesktopWidget().availableGeometry().center()
- self.frameGeometry().center())
self.setWindowTitle("LVDOWin - GUI")
# ...
def createIOBox(self):
source_component = QtGui.QVBoxLayout()
sourceLabel = QtGui.QLabel("Input File:")
sourceLabel.setToolTip("Specifies what file you want to convert")
sourceLabel.setAlignment(Qt.AlignCenter| Qt.AlignCenter)
sourceLayout = QtGui.QHBoxLayout()
openBtn = QtGui.QPushButton("...")
width = openBtn.fontMetrics().boundingRect(openBtn.text()).width() + 14
openBtn.setMaximumWidth(width)
self.sourcePath = QtGui.QLineEdit(".." + os.path.sep + "message.txt")
self.sourcePath.setEnabled(False)
openBtn.clicked.connect(self.setSourcePath)
sourceLayout.addWidget(sourceLabel)
sourceLayout.addWidget(self.sourcePath)
sourceLayout.addWidget(openBtn)
#source_component.addWidget(sourceLabel)
source_component.addLayout(sourceLayout)
#source_box = self.BorderBox(source_component)
destination_component = QtGui.QVBoxLayout()
destinationLabel = QtGui.QLabel("Output File:")
destinationLabel.setToolTip("Specifies where you want to save the file")
destinationLabel.setAlignment(Qt.AlignCenter| Qt.AlignCenter)
destination_layout = QtGui.QHBoxLayout()
#TODO Add a wrapper the preserves the original filename
saveBtn = QtGui.QPushButton('...')
width = saveBtn.fontMetrics().boundingRect(saveBtn.text()).width() + 14
saveBtn.setMaximumWidth(width)
self.destinationPath = QtGui.QLineEdit(".." + os.path.sep + "output.mkv")
self.destinationPath.setEnabled(False)
saveBtn.clicked.connect(self.setDestinationPath)
destination_layout.addWidget(destinationLabel)
destination_layout.addWidget(self.destinationPath)
destination_layout.addWidget(saveBtn)
#destination_component.addWidget(destinationLabel)
destination_component.addLayout(destination_layout)
#destination_box = self.BorderBox(destination_component)
io_component = QtGui.QVBoxLayout()
io_component.addLayout(source_component)
io_component.addLayout(destination_component)
io_box = self.BorderBox(io_component)
return io_box
def createSettings(self):
screen_res_box = QtGui.QHBoxLayout()
screen_res_box.addWidget(QtGui.QLabel('Video resolution'))
self.screenResolution = QtGui.QLineEdit('640x480')
self.screenResolution.setValidator(
QtGui.QRegExpValidator(QRegExp('[0-9]{3,}x[0-9]{3,}'),
self.screenResolution))
self.screenResolution.textChanged.connect(self.check_state)
self.screenResolution.textChanged.emit(self.screenResolution.text())
self.screenResolution.setEnabled(False)
screen_res_box.addWidget(self.screenResolution)
return self.BorderBox(screen_res_box)
def setSourcePath(self):
import re
path = self.exec_file_open_dialog()
if path is None:
return
path = re.sub('(.)', r'\1', path)
self.sourcePath.setText(str(path))
def setDestinationPath(self):
import re
path = self.exec_file_save_dialog()
if path is None:
return
path = re.sub('(.)', r'\1', path)
self.destinationPath.setText(str(path))
def BorderBox(self, layout):
from PySide.QtGui import QFrame
toto = QFrame()
toto.setLayout(layout)
toto.setFrameShape(QFrame.Box)
toto.setFrameShadow(QFrame.Sunken)
return toto
def popupMessage(self, message):
from PySide.QtGui import QMessageBox
msgBox = QMessageBox()
msgBox.setWindowTitle('Warning!!!')
msgBox.setText(message)
msgBox.exec_()
def HLine(self):
from PySide.QtGui import QFrame
toto = QFrame()
toto.setFrameShape(QFrame.HLine)
toto.setFrameShadow(QFrame.Sunken)
return toto
def normalOutputWritten(self, text):
"""Append text to the QTextEdit."""
# Maybe QTextEdit.append() works as well, but this is how I do it:
cursor = self.te.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.te.setTextCursor(cursor)
self.te.ensureCursorVisible()
#------O----------------------------------------------------------------
def encodeFile(self):
"""
Opens a file dialog and encodes the file as output.mkv
"""
path = self.sourcePath.text()
#This is necessary since we are using the shell.
if path is None:
return
destination_text = self.destinationPath.text()
input_res = '640x480'
#TODO Reimpliment with shell off when you find an alternative for type
cmd = 'type "%s"' % path
cmd += ' | lvdoenc -s ' + input_res + ' -q 6 --qmin 1 --qmax 4 | '
cmd += 'x264 --input-res ' + input_res + ' --fps 1 --profile high --level 5.1 --tune stillimage '
cmd += '--crf 22 --colormatrix bt709 --me dia '
cmd += '--merange 0 -o "%s" -' % destination_text
bin_directory = os.getcwd() + os.path.sep +'bin'
p = Popen(cmd, stdout = PIPE,
stderr = STDOUT, shell = True, cwd=bin_directory,
bufsize = 0, universal_newlines=True)
while True:
line = p.stdout.readline()
print(line.rstrip())
self.normalOutputWritten(str(line))
self.te.repaint()
if not line: break
def decodeFile(self):
"""
Opens a file dialog and decodes the file as message.txt
"""
path = self.sourcePath.text()
#This is necessary since we are using the shell.
if path is None:
return
if not isVideo(path):
self.popupMessage('File: The input file does not seem to be a video file')
return
#TODO Reimpliment with shell off when you find an alternative for type
input_res = '640x480'
cmd = 'ffmpeg -i "%s"' % path
cmd += ' -r 1 -f rawvideo - | lvdodec -s ' + input_res + ' -q 6 --qmin 1 --qmax 4 > '
#cmd = cmd.split()
cmd = cmd + '"' + self.destinationPath.text() + '"'
bin_directory = os.getcwd() + os.path.sep +'bin'
p = Popen(cmd, stdout = PIPE,
stderr = STDOUT, shell = True, cwd=bin_directory,
bufsize = 0, universal_newlines=True)
while True:
line = p.stdout.readline()
# print(line.rstrip())
self.normalOutputWritten(str(line))
self.te.repaint()
if not line: break
def exec_file_open_dialog(self):
path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open File", os.getcwd())
if path:
path = path = path.replace("/", os.path.sep)
return path
else:
return None
def exec_file_save_dialog(self):
path, _ = QtGui.QFileDialog.getSaveFileName(self, "Save file", "")
print(path)
if path:
path = path = path.replace("/", os.path.sep)
return path
else:
return None
#----------------------------------------------------------------------
def openDirectoryDialog(self):
"""
Opens a dialog to allow user to choose a directory
"""
flags = QtGui.QFileDialog.DontResolveSymlinks | QtGui.QFileDialog.ShowDirsOnly
d = directory = QtGui.QFileDialog.getExistingDirectory(self,
"Open Directory",
os.getcwd(),
flags)
self.label.setText(d)
def check_state(self, *args, **kwargs):
sender = self.sender()
validator = sender.validator()
state = validator.validate(sender.text(), 0)[0]
if state == QtGui.QValidator.Acceptable:
color = '#c4df9b' # green
elif state == QtGui.QValidator.Intermediate:
color = '#fff79a' # yellow
else:
color = '#f6989d' # red
sender.setStyleSheet('QLineEdit { background-color: %s }' % color)
def isVideo(filename):
filename, file_extension = os.path.splitext(filename)
valid_filetypes = ['.mp4', '.flv', '.avi', '.wmv', '.mkv']
return file_extension in valid_filetypes
#----------------------------------------------------------------------
if __name__ == "__main__":
app = QtGui.QApplication([])
form = OsirisGUI()
form.show()
app.exec_()