-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
395 lines (319 loc) · 12.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
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
import math
import os
import subprocess
import sys
import urllib.request
import pytube
from pytube import YouTube
import pytube.exceptions
from PIL import Image
from PySide2 import QtCore, QtGui
from PySide2.QtGui import (QColor)
from PySide2.QtWidgets import *
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC, error
from mutagen.mp3 import MP3
# loading screen ui
from converter_outside import Ui_LoadingWindow
# main app ui
from converter_ui import Ui_Jazzie
# import pytube
# from pytube import YouTube
# stores a list of words that need to be taken out of artist name so user does not have to change it
blacklistAuthor = ["Topic", "topic"]
blacklistTitle = ["(Official Audio)",
"(OFFICIAL AUDIO)"
"(Official Music Video)",
"[Official Audio]",
"[Official Music Video]",
"[Audio Only]",
"[Audio]"]
blacklistChar = ["#", "%", "&", "{", "}", "\'", "\\", "<", ">", "*", "?", "/", "$", "!", "'", "\"", ":", "@", "+", "`",
"|", "=", "~", "^", ";", "[", "]", "(", ")", ",", "."]
# globals
cnt = 0
savePath = os.path.expanduser('~/Downloads/')
authorName = ""
titleName = ""
fullName = ""
thumbnailPath = ""
newFile = ""
oldFile = ""
id3 = None
length = ''
tempNewFile = ''
# loading screen
class LoadingScreen(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main = MainWindow()
self.ui = Ui_LoadingWindow()
self.ui.setupUi(self)
# remove title bar for clean ui
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
# Drop shadow effect
self.shadow = QGraphicsDropShadowEffect(self)
self.shadow.setBlurRadius(20)
self.shadow.setXOffset(0)
self.shadow.setYOffset(0)
self.shadow.setColor(QColor(0, 0, 0, 60))
self.ui.dropShadowFrame.setGraphicsEffect(self.shadow)
# start qtimer and connect event to ui progress bar
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.progress)
# timer in ms
self.timer.start(30)
# initial subtitle
self.ui.subtitleLabel.setText("Converting made <strong>easy</strong>")
# varying subtitle change 1500 to a different time period
QtCore.QTimer.singleShot(1500, lambda: self.ui.subtitleLabel.setText("Loading backend"))
QtCore.QTimer.singleShot(2000, lambda: self.ui.subtitleLabel.setText("Loading backend."))
QtCore.QTimer.singleShot(2500, lambda: self.ui.subtitleLabel.setText("Loading backend.."))
QtCore.QTimer.singleShot(3000, lambda: self.ui.subtitleLabel.setText("Loading backend..."))
self.show()
# END
def progress(self):
global cnt
# Set value to progress bar
self.ui.progressBar.setValue(cnt)
# close load screen and open actual app
if cnt > 100:
# stop cnt
self.timer.stop()
# show main application
self.main.show()
self.close()
cnt += 1
# main app
def cropTN(imgPath):
img = Image.open(imgPath)
# 720x720 square for the thumbnail
left = 280
top = 0
right = 1000
bottom = 720
cropped = img.crop((left, top, right, bottom))
# cropped.show()
# try to remove prior to saving
try:
os.remove(imgPath)
except FileNotFoundError:
pass
cropped.save(imgPath)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.msg = QMessageBox() # error message box
self.complete = QMessageBox() # on complete conversion message box
self.msg.setWindowIcon(QtGui.QIcon('j.png'))
self.complete.setWindowIcon(QtGui.QIcon('j.png'))
self.ui = Ui_Jazzie()
self.ui.setupUi(self)
self.setWindowTitle('Jazzie YT to MP3 Converter')
self.setWindowIcon(QtGui.QIcon('j.png'))
# gui events
self.ui.urlEntry.textChanged.connect(self.urlChanged)
self.ui.convertButton.clicked.connect(self.buttonClicked)
def urlChanged(self):
url = self.ui.urlEntry.text()
split = url.split("=")
id = split[1]
try:
global titleName, authorName, fullName, thumbnailPath, tempNewFile, oldFile, newFile, length
yt = YouTube(url)
length = yt.length
maxMinutes = math.floor(length / 60)
maxSeconds = (length / 60 - math.floor(length / 60)) * 60
titleName = yt.title
authorName = yt.author
authorName = cleanup_author(authorName)
titleName = cleanup_title(titleName, authorName)
thumbnailURL = yt.thumbnail_url
fullName = authorName + '-' + titleName # name of file before prefix
thumbnailPath = savePath + fullName + '.jpeg'
urllib.request.urlretrieve("https://img.youtube.com/vi/" + id + "/maxresdefault.jpg", thumbnailPath) # saves thumbnail
cropTN(thumbnailPath)
audio = yt.streams.get_audio_only()
audio.download(savePath, fullName + "1.mp3") # saves mp4 file
# new save paths
oldFile = savePath + fullName + '1.mp3'
newFile = savePath + fullName + '.mp3'
# autofill text fields
self.ui.titleEntry.setText(cleanup_title_og(titleName))
self.ui.authorEntry.setText(cleanup_author_og(authorName))
self.ui.albumEntry.setText('Provided by Jazzie MP3')
# calculate formatting of string for input to ffmpeg
mins = stringMin(maxMinutes)
secs = stringSec(maxSeconds)
# autofill start and stop entries
self.ui.startEntry.setText('00:00:00.00')
self.ui.stopEntry.setText(mins + secs)
# allow user to change id3 parameters
self.ui.authorEntry.setEnabled(True)
self.ui.titleEntry.setEnabled(True)
self.ui.albumEntry.setEnabled(True)
self.ui.comboBox.setEnabled(True)
self.ui.stopEntry.setEnabled(True)
self.ui.startEntry.setEnabled(True)
# set focus to start
self.ui.startEntry.setFocus()
except (KeyError, pytube.exceptions.RegexMatchError):
pass
def buttonClicked(self):
if self.ui.comboBox.currentText() == "--Select--":
self.msg.setIcon(QMessageBox.Critical)
self.msg.setText('Please select a genre')
self.msg.setWindowTitle('Error Occurred')
self.msg.exec_()
return
# variables for math arithmetic to compute difference in seconds from start to stop
try:
startMin = int(self.ui.startEntry.text()[3:5])
startSec = int(self.ui.startEntry.text()[6:8])
stopMin = int(self.ui.stopEntry.text()[3:5])
stopSec = int(self.ui.stopEntry.text()[6:8])
except ValueError:
self.msg.setIcon(QMessageBox.Critical)
self.msg.setText('Please input correctly formatted start and end times')
self.msg.setWindowTitle('Error Occurred')
self.msg.exec_()
return
#print("before convert")
# convert
subprocess.run('ffmpeg -hide_banner -loglevel error -y -i ' + oldFile + ' ' + newFile, shell=True)
#print("in the middle of conversion")
# trim mp3 file
# subprocess.run('ffmpeg -hide_banner -loglevel error -y -i ' + oldFile +
# ' -ss ' + self.ui.startEntry.text() +
# ' -t ' + str((((stopMin - startMin) * 60) + stopSec - startSec)) +
# ' -acodec copy ' + newFile)
#print("after convert")
# remove old file and thumbnail after creation
try:
os.remove(oldFile)
except FileNotFoundError:
pass
# grab mp3 file
mp3 = MP3(newFile, ID3=ID3)
try:
mp3.add_tags()
except error:
print("tags error")
pass
# embed thumbnail into mp3 file
try:
mp3.tags.add(APIC(mime='image/jpeg', type=3, desc=u'Cover (front)', data=open(thumbnailPath, 'rb').read()))
mp3.save()
except error:
print("embed thumbnail failed")
pass
# change id3 tags
global id3
id3 = EasyID3(newFile)
id3['genre'] = str(self.ui.comboBox.currentText())
id3['album'] = str(self.ui.albumEntry.text())
id3['artist'] = str(self.ui.authorEntry.text())
id3['title'] = str(self.ui.titleEntry.text())
id3.save()
# remove thumbnail and mp4 file from downloads
try:
os.remove(thumbnailPath)
os.remove(oldFile)
except FileNotFoundError:
pass
# clear selections and allow for new entry
self.ui.comboBox.setCurrentIndex(0)
self.ui.authorEntry.clear()
self.ui.titleEntry.clear()
self.ui.albumEntry.clear()
self.ui.urlEntry.clear()
self.ui.startEntry.clear()
self.ui.stopEntry.clear()
self.ui.urlEntry.setFocus()
# display message box
self.complete.setIcon(QMessageBox.Information)
self.complete.setText('Enter in a new song!')
self.complete.setWindowTitle("Conversion Completed!")
self.complete.exec_()
def cleanup_title_og(title_name):
name = title_name
# replace underscores for spaces
for char in name:
if char == '_':
name = name.replace(char, ' ')
return name
def cleanup_author_og(author_name):
name = author_name
# replace underscores for spaces
for char in name:
if char == '_':
name = name.replace(char, ' ')
return name
def cleanup_title(title_name, author_name):
temp = author_name
for char in temp:
if char == '_':
temp = temp.replace(char, ' ')
name = title_name
# remove author name if it is in title
if temp in name:
name = name.replace(temp, '')
# remove unwanted words if they are in title
for word in blacklistTitle:
if word in name:
name = name.replace(word, '')
# remove unwanted hyphens
for char in name:
if char == '-':
name = name.replace(char, '')
name = name.lstrip(' ')
name = name.rstrip(' ')
# remove illegal directory characters
for char in name:
if char in blacklistChar:
name = name.replace(char, '')
# replace spaces in title with underscores
for char in name:
if char == ' ':
name = name.replace(char, '_')
return name
def cleanup_author(author_name):
name = author_name
# remove words like 'topic' etc from author
for word in blacklistAuthor:
if word in name:
name = name.replace(word, '')
# get rid of spaces after name
for char in name[::-1]:
if char == " ":
name = name.replace(char, '')
else:
break
# put an underscore for each space in the name
for char in name:
if char == " ":
name = name.replace(char, '_')
# remove illegal characters
for char in name:
if char in blacklistChar:
name = name.replace(char, '')
return name
def stringMin(maxMinutes):
stmin = ''
if maxMinutes < 10:
stmin = '00:0' + str(maxMinutes) + ':'
else:
stmin = '00:' + str(maxMinutes) + ':'
return stmin
def stringSec(maxSeconds):
stsec = ''
if maxSeconds < 10:
stsec = '0' + str(int(maxSeconds))
else:
stsec = str('%.2f' % maxSeconds)
return stsec
if __name__ == "__main__":
app = QApplication()
window = LoadingScreen()
sys.exit(app.exec_())