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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
#!/usr/bin/python
''' Hypervideo GUI '''
import sys
from PyQt5.QtCore import (
QFile,
QPoint,
QRect,
QSize,
QStandardPaths,
Qt,
QProcess,
QSettings
)
from PyQt5.QtGui import QIcon, QFont, QClipboard
from PyQt5.QtWidgets import (
QAction,
QApplication,
QComboBox,
QFileDialog,
QHBoxLayout,
QLineEdit,
QLabel,
QMainWindow,
QMessageBox,
QProgressBar,
QPushButton,
QToolButton,
QVBoxLayout,
QWidget,
)
# Debuging
if len(sys.argv) == 2:
if sys.argv[1] == '-v':
DEBUG = 0
else:
DEBUG = None
else:
DEBUG = None
__version__ = '0.1'
__license__ = 'GPL-3'
__title__ = 'Simple Hypervideo Download GUI'
class MainWindow(QMainWindow):
def __init__(self):
''' Initial '''
super(MainWindow, self).__init__()
self.hypervideo_bin = None
self.url_catch = None
self.out_folder_path = '/tmp/'
self.settings = QSettings('YouTubeDL', 'YTDL')
self.setAttribute(Qt.WA_DeleteOnClose)
self.create_status_bar()
pyfile = QStandardPaths.findExecutable("hypervideo")
if not pyfile == "":
debugging('Found executable: %s' % pyfile)
self.hypervideo_bin = pyfile
else:
self.msgbox("hypervideo not found\nPlease install hypervideo")
self.default_formats_menu_items = ['Video/Audio - Best Quality',
'Audio Only - Best Quality']
self.list = []
self.init_ui()
def init_ui(self):
''' Initial UI '''
self.setWindowTitle(__title__)
btnwidth = 155
self.cmd = None
self.process = QProcess(self)
self.process.started.connect(lambda: self.show_message("Creating List"))
self.process.started.connect(lambda: self.btn_get_formats.setEnabled(False))
self.process.finished.connect(lambda: self.show_message("Finished creating List"))
self.process.finished.connect(self.process_finished)
self.process.finished.connect(lambda: self.btn_get_formats.setEnabled(True))
self.process.readyRead.connect(self.process_output)
self.download_process = QProcess(self)
self.download_process.setProcessChannelMode(QProcess.MergedChannels)
self.download_process.started.connect(lambda: self.show_message("Download started"))
self.download_process.started.connect(lambda: self.download_button.setEnabled(False))
self.download_process.started.connect(lambda: self.cancel_button.setEnabled(True))
self.download_process.finished.connect(lambda: self.show_message("Download finished"))
self.download_process.finished.connect(lambda: self.download_button.setEnabled(True))
self.download_process.finished.connect(lambda: self.cancel_button.setEnabled(False))
self.download_process.finished.connect(lambda: self.setWindowTitle(__title__))
self.download_process.readyRead.connect(self.dl_process_out)
self.setGeometry(0, 0, 600, 250)
self.setFixedSize(600, 250)
self.setStyleSheet(ui_style_sheet(self))
self.setWindowIcon(QIcon.fromTheme("video-playlist"))
# Menu
main_menu = self.menuBar()
file_menu = main_menu.addMenu('File')
help_menu = main_menu.addMenu('Help')
# Exit button
exit_button = QAction('Exit', self)
exit_button.setShortcut('Ctrl+Q')
exit_button.setStatusTip('Exit application')
exit_button.triggered.connect(self.close)
# About button
about_button = QAction('About', self)
about_button.triggered.connect(self.on_button_clicked)
# Adding buttons to Menu
help_menu.addAction(about_button)
file_menu.addAction(exit_button)
# Path
lbl_url = QLabel()
lbl_url.setText("Insert URL/ID:")
lbl_url.setAlignment(Qt.AlignRight)
lbl_url.setFixedWidth(btnwidth)
lbl_url.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
self.lbl_url_path = QLineEdit()
self.lbl_url_path.setPlaceholderText('https://invidio.us/watch?v=8SdPLG-_wtA')
# Set up callback to update video formats when URL is changed
self.lbl_url_path.textChanged.connect(self.reset_video_formats)
hlayout = QHBoxLayout()
hlayout.addWidget(lbl_url)
hlayout.addWidget(self.lbl_url_path)
# Output path
btn_out_path = QToolButton()
btn_out_path.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
btn_out_path.setText("Select Output Folder")
btn_out_path.setFixedWidth(btnwidth)
btn_out_path.clicked.connect(self.open_output_folder)
self.lbl_out_path = QLineEdit()
self.lbl_out_path.setPlaceholderText("Insert Output Folder Path")
self.lbl_out_path.textChanged.connect(self.update_output_path)
hlayout2 = QHBoxLayout()
hlayout2.addWidget(btn_out_path)
hlayout2.addWidget(self.lbl_out_path)
# Hypervideo path
btn_hypervideo_path = QToolButton()
btn_hypervideo_path.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
btn_hypervideo_path.setText("Select hypervideo")
btn_hypervideo_path.setFixedWidth(btnwidth)
btn_hypervideo_path.clicked.connect(self.select_hyper_dl)
self.lbl_hypervideo_path = QLineEdit(str(self.hypervideo_bin))
self.lbl_hypervideo_path.textChanged.connect(self.update_hypervideo_path)
self.lbl_hypervideo_path.setPlaceholderText("Insert Path to Hypervideo")
hlayout3 = QHBoxLayout()
hlayout3.addWidget(btn_hypervideo_path)
hlayout3.addWidget(self.lbl_hypervideo_path)
self.download_button = QToolButton()
self.download_button.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.download_button.setText("Download")
self.download_button.clicked.connect(self.download_selected)
self.download_button.setFixedWidth(btnwidth)
self.download_button.setFixedHeight(32)
self.btn_get_formats = QToolButton()
self.btn_get_formats.setText('Get Formats')
self.btn_get_formats.setFixedWidth(btnwidth)
self.btn_get_formats.setFixedHeight(32)
self.btn_get_formats.clicked.connect(self.fill_combo_formats)
self.cancel_button = QToolButton()
self.cancel_button.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.cancel_button.setText("Cancel")
self.cancel_button.clicked.connect(self.cancel_download)
self.cancel_button.setEnabled(False)
self.cancel_button.setFixedWidth(btnwidth)
self.cancel_button.setFixedHeight(32)
self.video_format_combobox = QComboBox()
self.populate_video_format_combobox(self.default_formats_menu_items)
self.video_format_combobox.setFixedHeight(26)
self.pbar = QProgressBar()
self.pbar.setFixedHeight(16)
self.pbar.setMaximum(100)
self.pbar.setMinimum(0)
self.pbar.setValue(0)
btn_layout = QHBoxLayout()
btn_layout.addWidget(self.download_button)
btn_layout.addWidget(self.btn_get_formats)
btn_layout.addWidget(self.cancel_button)
vlayout = QVBoxLayout()
vlayout.addLayout(hlayout)
vlayout.addLayout(hlayout2)
vlayout.addLayout(hlayout3)
vlayout.addWidget(self.video_format_combobox)
vlayout.addWidget(self.pbar)
vlayout.addLayout(btn_layout)
mywidget = QWidget()
mywidget.setLayout(vlayout)
self.setCentralWidget(mywidget)
# Copy background ID or URL of clipboard
self.clip = QApplication.clipboard()
if self.clip.text().startswith("http"):
self.lbl_url_path.setText(self.clip.text())
self.fill_combo_formats()
self.read_settings()
def on_button_clicked(self):
""" Button about """
msg = QMessageBox()
msg.setWindowTitle('About us')
msg.setText("<p align='center'>Written with Python3 and PyQt5<br>"
"Version: %s <br> License: %s </p>" % (__version__, __license__))
msg.setIcon(QMessageBox.Information)
self.show()
msg.exec_()
def closeEvent(self, event):
'''Protected Function for PyQt5
gets called when the user closes the GUI.
'''
self.write_settings()
close = QMessageBox()
close.setIcon(QMessageBox.Question)
close.setWindowTitle('Exit')
close.setText('You sure?')
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def read_settings(self):
''' Read config '''
debugging('Reading settings')
if self.settings.contains('geometry'):
self.setGeometry(self.settings.value('geometry'))
if self.settings.contains('outFolder'):
self.lbl_out_path.setText(self.settings.value('outFolder'))
def write_settings(self):
''' Save Settings '''
debugging('Writing settings')
self.settings.setValue('outFolder', self.out_folder_path)
self.settings.setValue('geometry', self.geometry())
def update_output_path(self):
''' Update Path Output '''
self.out_folder_path = self.lbl_out_path.text()
self.show_message("Output path changed to: %s" % self.lbl_out_path.text())
def update_hypervideo_path(self):
''' Update Hypervideo Path Output '''
self.hypervideo_bin = self.lbl_hypervideo_path.text()
self.show_message("hypervideo path changed to: %s" % self.lbl_hypervideo_path.text())
def show_message(self, message):
''' Show Message in StatuBar '''
self.statusBar().showMessage(message, 0)
def select_hyper_dl(self):
''' Select hypervideo executable '''
file_name, _ = QFileDialog.getOpenFileName(self, "locate hypervideo",
"/usr/bin/hypervideo", "exec Files (*)")
if file_name:
self.lbl_hypervideo_path.setText(file_name)
self.hypervideo_bin = file_name
def open_output_folder(self):
''' Open out folder path '''
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.Directory)
d_path = dlg.getExistingDirectory()
if d_path:
self.lbl_out_path.setText(d_path)
def populate_video_format_combobox(self, labels):
'''Populate the video format combobox with video formats. Clear the previous labels.
labels {list} -- list of strings representing the video format combobox options
'''
self.video_format_combobox.clear()
for label in labels:
self.video_format_combobox.addItem(label)
def reset_video_formats(self):
''' Clean video formast '''
idx = self.video_format_combobox.currentIndex()
self.populate_video_format_combobox(self.default_formats_menu_items)
# preserve combobox index if possible
if idx > 1:
self.video_format_combobox.setCurrentIndex(0)
else:
self.video_format_combobox.setCurrentIndex(idx)
def fill_combo_formats(self):
''' Scan formats and Add item to combobox '''
self.video_format_combobox.clear()
if QFile.exists(self.hypervideo_bin):
# Default options
self.video_format_combobox.addItems(self.default_formats_menu_items[0:2])
self.list = []
self.url_catch = self.lbl_url_path.text()
if not self.lbl_url_path.text() == "":
debugging('Scan Formats')
self.process.start(self.hypervideo_bin, ['-F', self.url_catch])
else:
self.show_message("URL empty")
else:
self.show_message("hypervideo missing")
def process_output(self):
''' Process out '''
try:
output = str(self.process.readAll(), encoding='utf8').rstrip()
except TypeError:
output = str(self.process.readAll()).rstrip()
self.list.append(output)
def process_finished(self):
''' Process Finished '''
out = ','.join(self.list)
out = out.partition("resolution note")[2]
out = out.partition('\n')[2]
mylist = out.rsplit('\n')
self.video_format_combobox.addItems(mylist)
count = self.video_format_combobox.count()
self.video_format_combobox.setCurrentIndex(count-1)
def download_selected(self):
''' Download selected video format '''
if QFile.exists(self.hypervideo_bin):
self.pbar.setValue(0)
self.url_catch = self.lbl_url_path.text()
quality = None
if self.video_format_combobox.currentText() == self.default_formats_menu_items[0]:
quality = 'bestvideo+bestaudio/best'
options = []
options.append('-f')
options.append(quality)
elif self.video_format_combobox.currentText() == self.default_formats_menu_items[1]:
quality = '--audio-quality'
options = []
options.append('-x')
options.append('--audio-format')
options.append('mp3')
options.append(quality)
options.append('192')
else:
quality = self.video_format_combobox.currentText().partition(" ")[0]
options = []
options.append('-f')
options.append(quality)
if self.url_catch != '':
if quality is not None:
options.append("-o")
options.append("%(title)s.%(ext)s")
options.append(self.url_catch)
self.show_message("Download started")
debugging('Download Selected Quality: %s' % quality)
debugging('Download URL: %s' % self.url_catch)
self.download_process.setWorkingDirectory(self.out_folder_path)
self.download_process.start(self.hypervideo_bin, options)
else:
self.show_message("List of available files is empty")
else:
self.show_message("URL empty")
else:
self.show_message("hypervideo missing")
def dl_process_out(self):
''' Download process out '''
try:
out = str(self.download_process.readAll(), encoding='utf8').rstrip()
except TypeError:
out = str(self.download_process.readAll()).rstrip()
out = out.rpartition("[download] ")[2]
self.show_message("Progress: %s" % out)
self.setWindowTitle(out)
out = out.rpartition("%")[0].rpartition(".")[0]
if not out == "":
try:
pout = int(out)
self.pbar.setValue(pout)
except ValueError:
pass
def cancel_download(self):
''' Cancel download'''
if self.download_process.state() == QProcess.Running:
debugging('Process is running, will be cancelled')
self.download_process.close()
self.show_message("Download cancelled")
self.pbar.setValue(0)
self.cancel_button.setEnabled(False)
else:
self.show_message("Process is not running")
def create_status_bar(self):
''' Create StatusBar'''
self.statusBar().showMessage("Ready")
def msgbox(self, message):
''' MessageBox'''
QMessageBox.warning(self, "Message", message)
def debugging(var):
''' Debugging '''
if DEBUG == 0:
message_debug = print('[debug] %s' % var)
else:
message_debug = None
return message_debug
def ui_style_sheet(self):
return """
QStatusBar
{
font-family: Helvetica;
font-size: 8pt;
color: #666666;
}
QProgressBar:horizontal {
border: 1px solid gray;
text-align: top;
padding: 1px;
border-radius: 3px;
background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,
stop: 0 #fff,
stop: 0.4999 #eee,
stop: 0.5 #ddd,
stop: 1 #eee );
width: 15px;
}
QProgressBar::chunk:horizontal
{
background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,
stop: 0 #5baaf5,
stop: 0.4999 #4ba6f5,
stop: 0.5 #3ba6f5,
stop: 1 #00aaff );
border-radius: 3px;
border: 1px solid black;
}
"""
|