-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
229 lines (185 loc) · 8.55 KB
/
app.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
import time
import webbrowser
import threading
import os
import sys
import yt_dlp
from urllib.parse import urlparse
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import (
QApplication, QWidget, QLabel, QLineEdit, QCheckBox, QPushButton,
QVBoxLayout, QMessageBox, QProgressBar, QComboBox, QFileDialog, QHBoxLayout
)
# GFW Proxy import (assumed it's correct)
from gfw.pyprox_HTTPS_current import start as gfw_proxy_start
# Set the ffmpeg path based on OS
def get_ffmpeg_path():
if sys.platform == "win32":
return os.path.join(os.path.dirname(__file__), 'ffmpeg', 'ffmpeg.exe')
elif sys.platform == "darwin": # macOS
return os.path.join(os.path.dirname(__file__), 'ffmpeg', 'ffmpeg')
else: # Linux or Unix
return os.path.join(os.path.dirname(__file__), 'ffmpeg', 'ffmpeg')
class DownloadThread(QThread):
progress_signal = pyqtSignal(int)
finished_signal = pyqtSignal(str)
def __init__(self, video_url, proxy, is_playlist, quality, audio_only, download_path):
super().__init__()
self.video_url = video_url
self.proxy = proxy
self.is_playlist = is_playlist
self.quality = quality
self.audio_only = audio_only
self.download_path = download_path
def run(self):
# Get the FFmpeg location
ffmpeg_path = get_ffmpeg_path()
# Basic YDL options
ydl_opts = {
'progress_hooks': [self.progress_hook],
'outtmpl': os.path.join(self.download_path, '%(title)s.%(ext)s'),
'ffmpeg_location': ffmpeg_path, # Specify FFmpeg binary
'retries': 5,
'noprogress': True,
'ignoreerrors': True,
'continuedl': False,
}
if self.proxy:
ydl_opts['proxy'] = self.proxy
if self.audio_only:
ydl_opts['format'] = 'bestaudio/best'
ydl_opts['postprocessors'] = [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}]
else:
# Set video quality based on user selection
if self.quality == '720p':
ydl_opts['format'] = 'bestvideo[height<=720]+bestaudio/best'
elif self.quality == '480p':
ydl_opts['format'] = 'bestvideo[height<=480]+bestaudio/best'
elif self.quality == '1080p':
ydl_opts['format'] = 'bestvideo[height<=1080]+bestaudio/best'
else:
ydl_opts['format'] = 'best'
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([self.video_url])
self.finished_signal.emit("دانلود و ترکیب فایلها با موفقیت به پایان رسید!")
except Exception as e:
self.finished_signal.emit(f"دانلود شکست خورد: {str(e)}")
def progress_hook(self, d):
if d['status'] == 'downloading':
downloaded_bytes = d.get('downloaded_bytes', 0)
total_bytes = d.get('total_bytes', d.get('total_bytes_estimate', 0))
if total_bytes > 0:
progress = int(downloaded_bytes / total_bytes * 100)
self.progress_signal.emit(progress)
elif d['status'] == 'error':
self.finished_signal.emit("دانلود شکست خورد. لطفا دوباره تلاش کنید.")
class YouTubeApp(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def open_link(self, url):
webbrowser.open(url)
def init_ui(self):
# Create the widgets
self.note_label = QLabel("با باز بودن برنامه میتونید روی پورت ۴۵۰۰ یوتوب روی بدون فیلترشکن نگاه کنید")
self.tut_label = QLabel('<a href="https://mohammadforoutan.github.io/gfw-py/">برای آموزش کلید کنید</a>')
self.tut_label.linkActivated.connect(self.open_link)
self.url_label = QLabel("YouTube Video URL:")
self.url_input = QLineEdit(self)
self.proxy_label = QLabel("Proxy (optional):")
self.proxy_input = QLineEdit(self)
self.proxy_input.setText("http://127.0.0.1:4500")
self.playlist_checkbox = QCheckBox("Is this a playlist?", self)
self.quality_label = QLabel("Select Quality:")
self.quality_combo = QComboBox(self)
self.quality_combo.addItems(['720p', '480p', '1080p'])
self.quality_combo.setCurrentText('720p')
self.audio_only_checkbox = QCheckBox("Download Audio Only (MP3)", self)
self.path_label = QLabel("Download Path:")
self.path_input = QLineEdit(self)
self.path_input.setReadOnly(True)
self.browse_button = QPushButton("Browse", self)
self.browse_button.clicked.connect(self.browse_folder)
self.set_default_download_path()
self.progress_bar = QProgressBar(self)
self.progress_bar.setValue(0)
self.progress_bar.setMaximum(100)
self.progress_bar_note = QLabel("تکمیل شدن دانلود چند مرحله داره.\n"
"۱. دانلود ویدیو\n"
"۲. دانلود صدا\n"
"۳. ترکیب کردن تصویر و صدا\n"
"به همین دلیل ممکنه چند بار به صد درصد برسه.")
self.submit_button = QPushButton("Submit", self)
self.submit_button.clicked.connect(self.start_download)
# Layout configuration
layout = QVBoxLayout()
layout.addWidget(self.note_label)
layout.addWidget(self.tut_label)
layout.addWidget(self.url_label)
layout.addWidget(self.url_input)
layout.addWidget(self.proxy_label)
layout.addWidget(self.proxy_input)
layout.addWidget(self.playlist_checkbox)
layout.addWidget(self.quality_label)
layout.addWidget(self.quality_combo)
layout.addWidget(self.audio_only_checkbox)
layout.addWidget(self.path_label)
layout.addWidget(self.path_input)
layout.addWidget(self.browse_button)
layout.addWidget(self.progress_bar)
layout.addWidget(self.progress_bar_note)
layout.addWidget(self.submit_button)
self.setLayout(layout)
self.setWindowTitle('YouTube Video Downloader')
self.setGeometry(100, 100, 400, 350)
def set_default_download_path(self):
if sys.platform == 'win32':
default_path = os.path.join(os.getenv('USERPROFILE'), 'Downloads')
elif sys.platform == 'darwin':
default_path = os.path.join(os.path.expanduser('~'), 'Downloads')
else:
default_path = os.path.join(os.path.expanduser('~'), 'Downloads')
self.path_input.setText(default_path)
def browse_folder(self):
folder = QFileDialog.getExistingDirectory(self, "Select Download Folder")
if folder:
self.path_input.setText(folder)
def start_download(self):
video_url = self.url_input.text()
proxy = self.proxy_input.text()
is_playlist = self.playlist_checkbox.isChecked()
quality = self.quality_combo.currentText()
audio_only = self.audio_only_checkbox.isChecked()
download_path = self.path_input.text()
if not video_url:
QMessageBox.warning(self, "Input Error", "لطفا لینک ویدیو یوتیوب را وارد کنید")
return
if not download_path:
QMessageBox.warning(self, "Input Error", "لطفا مسیر ذخیرهسازی را انتخاب کنید")
return
self.submit_button.setEnabled(False)
self.download_thread = DownloadThread(video_url, proxy, is_playlist, quality, audio_only, download_path)
self.download_thread.progress_signal.connect(self.update_progress_bar)
self.download_thread.finished_signal.connect(self.download_finished)
self.download_thread.start()
def update_progress_bar(self, value):
self.progress_bar.setValue(value)
def download_finished(self, message):
QMessageBox.information(self, "Download Status", message)
self.submit_button.setEnabled(True)
self.progress_bar.setValue(0)
if __name__ == '__main__':
# Start the GFW proxy in a separate thread
gfw_thread = threading.Thread(target=gfw_proxy_start)
gfw_thread.daemon = True
gfw_thread.start()
time.sleep(1)
app = QApplication(sys.argv)
window = YouTubeApp()
window.show()
sys.exit(app.exec_())