-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (128 loc) · 5.56 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
from pytube import YouTube
from PySide6.QtWidgets import QApplication, QFileDialog, QListWidgetItem, QMessageBox, QWidget
from PySide6 import QtCore
from PySide6 import QtGui
import os, sys, time
from src.YTDownloaderUI import Ui_Form
from src.styles import STYLES
basedir = os.path.dirname(__file__)
outdir = 'out'
output_dir = os.path.join(os.path.dirname(sys.executable), outdir)
prefix = "https://www.youtube.com/watch?v="
appconfi = {
"author": "S3R43o3",
"version": "1.4.0"
}
class YTDownloader(QWidget, Ui_Form):
def __init__(self):
super(YTDownloader, self).__init__()
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setWindowTitle("Youtube Downloader")
self.versionLabel.setText(f"Version: {appconfi['version']} © 2023 {appconfi['author']}")
self.setupButtons()
self.changeSavepath()
def setupButtons(self):
self.addUrlBtn.clicked.connect(self.addDownloadURL)
self.removeUrlBtn.clicked.connect(self.removeDownloadUrl)
self.quitBtn.clicked.connect(self.close)
self.minimizeBtn.clicked.connect(self.showMinimized)
self.downloadBtn.clicked.connect(self.downloadAudio)
self.downloadProgress.setValue(0)
self.downloadProgress.setVisible(False)
self.changePathBtn.clicked.connect(self.chooseFolderClicked)
self.audioformatBox.addItems(['.mp3', '.mp4'])
self.audioformatBox.setCurrentIndex(0)
self.openSaveDirBtn.clicked.connect(self.openSavePath)
def openSavePath(self):
path = self.savePathLabel.text()
os.system(f'explorer.exe "{path}"')
def changeSavepath(self):
self.savePathLabel.setText(output_dir)
def generateSaveDir(self):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
self.showNotification("Initialization", f"Initialization successfull!\nSavedirectory created at path:\n'{output_dir}'")
def addDownloadURL(self):
url = self.urlEdit.text()
if url != "":
if not url.startswith(prefix):
self.showNotification("Error!", f"This URL\n'{url}'\nis not a valid Youtube Video URL!")
return
item = QListWidgetItem(url)
self.urlEdit.setText("")
self.listWidget.addItem(item)
def showNotification(self, title, text):
msgBox = QMessageBox(self)
msgBox.setWindowTitle(title)
msgBox.setText(text)
msgBox.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(sys.executable),'logo-transparent.ico')))
msgBox.setIcon(QMessageBox.Information)
msgBox.addButton("OK", QMessageBox.AcceptRole)
msgBox.setStyleSheet(STYLES['buttons'])
msgBox.exec()
def removeDownloadUrl(self):
item = self.listWidget.selectedItems()
if len(item) > 0:
row = self.listWidget.currentRow()
self.listWidget.takeItem(row)
def chooseFolderClicked(self):
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
folder_path = QFileDialog.getExistingDirectory(self, 'Select Folder', '', options=options)
if folder_path:
self.savePathLabel.setText(folder_path)
def downloadFinish(self, ytObj, path):
print("Youtube video is downloaded. Path: ", path, " Type: ", ytObj.title)
def downloadAudio(self):
self.downloadBtn.setDisabled(True)
self.downloadProgress.setVisible(True)
self.downloadProgress.setValue(1)
urls = self.listWidget.findItems("", QtCore.Qt.MatchFlag.MatchContains)
if len(urls) > 0:
self.downloadProgress.setMaximum(len(urls))
count = 0
self.downloadProgress.setValue(count)
for u in urls:
count += 1
yt = YouTube(url=u.text())
stream = yt.streams.get_audio_only()
if stream:
stream.download(self.savePathLabel.text(), f"{stream.default_filename[:-4]}{self.audioformatBox.currentText()}")
row = self.listWidget.row(u)
self.listWidget.takeItem(row)
self.downloadProgress.setValue(count)
self.downloadProgress.setValue(self.downloadProgress.maximum())
time.sleep(1)
self.downloadProgress.setVisible(False)
self.downloadBtn.setDisabled(False)
self.showNotification("Download Finish", f"All your '{count}' youtube videos are successfully downloaded!")
else:
self.downloadBtn.setDisabled(False)
self.downloadProgress.setVisible(False)
return
def mousePressEvent(self, event):
if event.buttons() == QtCore.Qt.MouseButton.LeftButton:
self.dragPos = event.globalPos()
event.accept()
def mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.MouseButton.LeftButton:
self.move(self.pos() + event.globalPos() - self.dragPos)
self.dragPos = event.globalPos()
event.accept()
def main():
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(sys.executable),'logo-transparent.ico')))
app.setStyle("Fusion")
window = YTDownloader()
window.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(sys.executable),'logo-transparent.ico')))
window.show()
window.generateSaveDir()
sys.exit(app.exec())
if __name__ == '__main__':
try:
main()
except Exception as e:
print("Error: ", e)
finally:
sys.exit()