-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube_dl_GUI.py
274 lines (218 loc) · 11.2 KB
/
youtube_dl_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
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import threading
import json
import yt_dlp as youtube_dl
GUI_STATE_JSON_FILE = './youtube_dl_GUI_state.json'
class App(QMainWindow):
def __init__(self):
super().__init__()
gui_state = self.loadGUIState() #load GUI state from file, load default state if save file not found
self.download_folder_list = gui_state['DownloadFolderList']
self.default_video_formats_menu_items=['Video - Best Quality','Audio Only - Best Quality','Detect All Available Formats']
self.initUI()
self.videoFormatCombobox.setCurrentIndex(gui_state['DownloadFormatComboboxIndex'])
def initUI(self):
self.setWindowTitle('YouTube DL GUI')
self.statusBar().showMessage('Welcome to YouTube Downloader GUI!') #initialize status bar
#initialize window dimensions
left, top, width, height = 100, 100, 640, 100
self.setGeometry(left, top, width, height)
#create URL entry buttons and entry textbox
urlEntryLabel = QLabel('Enter URL:')
self.urlEntryText = QLineEdit()
self.urlEntryText.setPlaceholderText('Enter YouTube URL')
self.urlEntryText.setText('https://www.youtube.com/watch?v=C0DPdy98e4c') #set default video to download
self.urlEntryText.textChanged.connect(self.resetVideoFormats) #set up callback to update video formats when URL is changed
#create output folder button and entry textbox
outputFolderButton = QPushButton('Select Output Folder')
outputFolderButton.setToolTip('Select output folder')
outputFolderButton.clicked.connect(self.updateOutputFolder)
self.outputEntryCombobox = QComboBox()
self.outputEntryCombobox.setEditable(True)
for item in self.download_folder_list:
self.outputEntryCombobox.addItem(item) #set default output folder to be downloads folder
#add combobox for video download format and detect formats button
detectFormatsLabel = QLabel('Download Format:')
self.videoFormatCombobox = QComboBox()
self.populateVideoFormatCombobox(self.default_video_formats_menu_items) #set default values for format select combobox
self.videoFormatCombobox.activated[str].connect(self.videoFormatChange)
#add download button
downloadButton = QPushButton('Download')
downloadButton.clicked.connect(self.downloadVideo_callback)
#create grid layout
layout = QGridLayout()
#add widgets to the layout
layout.addWidget(urlEntryLabel,1,0)
layout.addWidget(self.urlEntryText,1,1)
layout.addWidget(outputFolderButton,2,0)
layout.addWidget(self.outputEntryCombobox,2,1)
layout.addWidget(detectFormatsLabel,3,0)
layout.addWidget(self.videoFormatCombobox,3,1)
layout.addWidget(downloadButton,5,0)
#add grid layout as central widget for main window
main_widget=QWidget()
main_widget.setLayout(layout)
self.setCentralWidget(main_widget)
self.show()
def downloadVideo_callback(self):
''' Callback for the "Download Video" button'''
def downloadVideo_thread_helper(self,ydl_opts):
'''Download the video. Meant to be called in a background daemon thread'''
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([self.urlEntryText.text()])
self.statusBar().showMessage('Downloading Video... Done.')
#make sure a valid output directory was entered
if not os.path.isdir(self.outputEntryCombobox.currentText()):
self.statusBar().showMessage('Invalid download directory!')
return
#make sure the Download Folder List combobox is populated with the latest entry
#this covers the case where the user uses the edittext portion of the combobox
self.addItemToDownloadsCombobox(self.outputEntryCombobox.currentText())
#set output path/format
outtmpl = os.path.join(self.outputEntryCombobox.currentText(),r'%(title)s.%(ext)s')
#create the youtube downloader options based on video format combobox selection
if self.videoFormatCombobox.currentText() == self.default_video_formats_menu_items[0]:
#download best video quality
ydl_opts = {
'format': 'best',
'outtmpl': outtmpl,
}
elif self.videoFormatCombobox.currentText() == self.default_video_formats_menu_items[1]:
#for downloading best audio and converting to mp3
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': outtmpl,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192', #not actually best quality...
}],
}
else:
#grab video format from the dropdown string: ie. "135 - some video metadata here" -> "135"
video_format = self.videoFormatCombobox.currentText()[0:self.videoFormatCombobox.currentText().find('-')-1]
ydl_opts = {
'format': video_format,
'outtmpl': outtmpl,
}
#download the video in daemon thread
self.statusBar().showMessage('Downloading Video...') #update status bar
thread = threading.Thread(target = downloadVideo_thread_helper, args = (self,ydl_opts,) )
thread.daemon = True
thread.start()
def updateVideoFormats(self):
''' Grabs the list of available video formats in background thread and populates
video format combobox with results when complete.'''
def getVideoFormats_thread_helper(self,url):
''' Grabs the available video formats. Intended to be run as background thread.
'''
options = {'noplaylist': True, # only download single song, not playlist
}
with youtube_dl.YoutubeDL(options) as ydl:
meta = ydl.extract_info(url, download=False)
formats = meta.get('formats', [meta])
#add formats to combobox
item_list=self.default_video_formats_menu_items[0:2]
item_list.extend([ f['format'] +' ' +f['ext'] for f in formats])
self.populateVideoFormatCombobox(item_list)
self.statusBar().showMessage('Finished Downloading Video Formats')
self.videoFormatCombobox.setCurrentIndex(0)
url = self.urlEntryText.text()
#check if is valid url
if not url.startswith('https://www.youtube.com/watch?'):
#invalid url
self.populateVideoFormatCombobox(self.default_video_formats_menu_items)
return
else:
#valid url - fetch the video formats in background daemon thread
self.statusBar().showMessage('Downloading Video Formats')
thread = threading.Thread(target = getVideoFormats_thread_helper, args = (self,url,) )
thread.daemon = True
thread.start()
def videoFormatChange(self,text):
if text == self.default_video_formats_menu_items[2]:
#detect video formats was selected
self.statusBar().showMessage('Loading available formats...') #update statusbar to let user know something is happening
self.updateVideoFormats() #update video formats
def populateVideoFormatCombobox(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.videoFormatCombobox.clear()
for label in labels:
self.videoFormatCombobox.addItem(label)
def resetVideoFormats(self):
idx = self.videoFormatCombobox.currentIndex()
self.populateVideoFormatCombobox(self.default_video_formats_menu_items)
#preserve combobox index if possible
if idx>1:
self.videoFormatCombobox.setCurrentIndex(0)
else:
self.videoFormatCombobox.setCurrentIndex(idx)
@pyqtSlot()
def updateOutputFolder(self):
''' Callback for "Update Output Folder" button. Allows user to select
output directory via standard UI.
'''
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
if len(file)>0:
self.addItemToDownloadsCombobox(file)
else:
self.statusBar().showMessage('Select a folder!')
def downloadTextChanged(self,text):
#function not used right now
if text not in self.download_folder_list and os.path.isdir(text):
self.addItemToDownloadsCombobox(text)
def addItemToDownloadsCombobox(self,text):
if text not in self.download_folder_list:
#if it's not in the list, add it to the list
self.download_folder_list = [text]+self.download_folder_list
else:
#if it's in the list already, move it to the top of the list
self.download_folder_list = [text]+[folder for folder in self.download_folder_list if not folder == text]
#maximum download folder history of 5
if len(self.download_folder_list)>5:
self.download_folder_list = self.download_folder_list[0:6]
#update the combobox
self.outputEntryCombobox.clear()
for item in self.download_folder_list:
self.outputEntryCombobox.addItem(item)
self.outputEntryCombobox.setCurrentIndex(0) #reset index - just in case
def saveGUIState(self):
DownloadFormatComboboxIndex = self.videoFormatCombobox.currentIndex()
save_dict= {'DownloadFolderList': self.download_folder_list,
'DownloadFormatComboboxIndex': DownloadFormatComboboxIndex if DownloadFormatComboboxIndex in [0,1] else 0,
}
with open(GUI_STATE_JSON_FILE,'w') as file:
json.dump(save_dict,file)
def loadGUIState(self):
if os.path.isfile(GUI_STATE_JSON_FILE):
with open(GUI_STATE_JSON_FILE, 'r') as file:
save_data = json.load(file)
if isinstance(save_data['DownloadFolderList'],str):
save_data['DownloadFolderList'] = [save_data['DownloadFolderList']] #convert to list
if 'DownloadFormatComboboxIndex' not in save_data:
save_data['DownloadFormatComboboxIndex'] = 0
else:
save_data = {'DownloadFolderList': [get_default_download_path()],
'DownloadFormatComboboxIndex': 0,
}
return save_data
def closeEvent(self, event):
'''This function gets called when the user closes the GUI. It saves the GUI state to the json file
GUI_STATE_JSON_FILE. '''
self.saveGUIState()
event.accept() # let the window close
def get_default_download_path():
""" Returns the path for the "Downloads" folder on Linux or Windows.
Used as default directory for videos to be saved to. """
return os.path.join(os.path.expanduser('~'), 'downloads')
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle('Fusion')
ex = App()
sys.exit(app.exec_())