-
Notifications
You must be signed in to change notification settings - Fork 0
/
YTR.py
253 lines (197 loc) · 9.93 KB
/
YTR.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
import sys
import os
import subprocess
import logging
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget, QLabel, QLineEdit, QTextEdit
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from PyQt5.QtGui import QIcon
import pyperclip
logging.basicConfig(level=logging.INFO)
DOWNLOAD_FOLDER = os.path.expanduser("./Downloads")
def create_readme():
"""Creates a readme.txt file with the specified text"""
readme_path = os.path.join(DOWNLOAD_FOLDER, 'readme.txt')
try:
with open(readme_path, 'w') as readme_file:
readme_file.write("Android copy the .mp3 to the ringtones folder and reboot device. iPhone drag and drop the .m4r file in iTunes and sync device.")
print(f"readme.txt created at: {readme_path}")
except Exception as e:
print(f"Failed to create readme.txt: {str(e)}")
class DownloadThread(QThread):
update_signal = pyqtSignal(str)
download_complete_signal = pyqtSignal()
def __init__(self, url, file_type):
super().__init__()
self.url = url
self.file_type = file_type
def run(self):
try:
if self.file_type == 'mp4':
output_path = os.path.join(DOWNLOAD_FOLDER, '%(title)s.mp4')
command = ['yt-dlp', '-f', 'mp4', '-o', output_path, self.url]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode == 0:
video_filename = self.get_video_filename(output_path)
if video_filename:
self.update_signal.emit(f"Video downloaded: {video_filename}")
create_readme()
else:
self.update_signal.emit("Error: Video file not found.")
else:
self.update_signal.emit(f"Error downloading video: {stderr.decode()}")
return
elif self.file_type == 'android':
video_filename = self.get_video_filename(f'{DOWNLOAD_FOLDER}/%(title)s.mp4')
if video_filename:
command = ['ffmpeg', '-i', video_filename, '-t', '20', '-q:a', '0', '-map', 'a', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3']
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode == 0:
self.update_signal.emit("Android ringtone created successfully")
else:
self.update_signal.emit(f"Error creating Android ringtone: {stderr.decode()}")
return
else:
self.update_signal.emit("Error: Video file not found for Android ringtone.")
elif self.file_type == 'iphone':
command = ['ffmpeg', '-i', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3', '-t', '20', '-acodec', 'aac', '-b:a', '128k', '-f', 'mp4', f'{DOWNLOAD_FOLDER}/iPhoneRingtone.m4r']
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode == 0:
self.update_signal.emit("iPhone ringtone created successfully")
else:
self.update_signal.emit(f"Error creating iPhone ringtone: {stderr.decode()}")
return
except Exception as e:
self.update_signal.emit(f"Failed to process: {str(e)}")
finally:
self.download_complete_signal.emit()
def get_video_filename(self, output_path):
"""Finds and returns the actual downloaded filename"""
title = os.path.basename(output_path).replace('%(title)s', '').strip()
video_filename = os.path.join(DOWNLOAD_FOLDER, f"{title}.mp4")
if os.path.exists(video_filename):
return video_filename
else:
for file in os.listdir(DOWNLOAD_FOLDER):
if file.endswith(".mp4"):
return os.path.join(DOWNLOAD_FOLDER, file)
return None
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('YT Ringtones')
self.setWindowIcon(QIcon('ytd.icns'))
self.setFixedSize(380, 500)
self.url_input = QLineEdit(self)
self.url_input.setPlaceholderText('Enter A YouTube URL')
self.download_button = QPushButton('Download Video', self)
self.download_button.setStyleSheet("color: yellow;")
self.download_button.clicked.connect(self.download_video)
self.android_button = QPushButton('Android Ringtone', self)
self.android_button.clicked.connect(self.android_download)
self.android_button.setEnabled(False)
self.iphone_button = QPushButton('iPhone Ringtone', self)
self.iphone_button.clicked.connect(self.iphone_download)
self.iphone_button.setEnabled(False)
self.open_folder_button = QPushButton('Open Downloads Folder', self)
self.open_folder_button.clicked.connect(self.open_folder)
self.push_to_device_button = QPushButton('Push to Device Android', self)
self.push_to_device_button.clicked.connect(self.push_to_device)
self.paste_button = QPushButton('Paste URL from Clipboard', self)
self.paste_button.clicked.connect(self.paste_from_clipboard)
self.clear_button = QPushButton('Reset', self)
self.clear_button.clicked.connect(self.clear_input)
self.status_text = QTextEdit(self)
self.status_text.setReadOnly(True)
self.footer_label = QLabel('<a href="https://cash.app/$iLostmyipod" style="color:white">it works!</a>', self)
self.footer_label.setOpenExternalLinks(True)
self.footer_label.setAlignment(Qt.AlignCenter)
self.btc_label = QLabel('<span style="color:yellow; font-size:10px;">BTC: 32WfTVZTzoJXSTdgfLer1Ad3SMVvjokkbX</span>', self)
self.btc_label.setAlignment(Qt.AlignCenter)
self.eth_label = QLabel('<span style="color:lightgreen; font-size:10px;">ETH: 0x47aCF0718770f3E1a57e4cCEA7609aEd95E220b5</span>', self)
self.eth_label.setAlignment(Qt.AlignCenter)
main_layout = QVBoxLayout()
main_layout.addWidget(self.url_input)
main_layout.addWidget(self.download_button)
main_layout.addWidget(self.android_button)
main_layout.addWidget(self.iphone_button)
main_layout.addWidget(self.open_folder_button)
main_layout.addWidget(self.push_to_device_button)
main_layout.addWidget(self.paste_button)
main_layout.addWidget(self.clear_button)
main_layout.addWidget(self.status_text)
footer_layout = QVBoxLayout()
footer_layout.addWidget(self.footer_label)
footer_layout.addWidget(self.btc_label)
footer_layout.addWidget(self.eth_label)
footer_widget = QWidget()
footer_widget.setLayout(footer_layout)
container = QWidget()
container.setLayout(main_layout)
main_layout.addStretch(1)
main_layout.addWidget(footer_widget)
self.setCentralWidget(container)
def push_to_device(self):
"""Push the Android ringtone .mp3 to the device using adb"""
try:
adb_command = ['adb', 'push', os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3'), '/sdcard/']
process = subprocess.Popen(adb_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode == 0:
self.status_text.append("Successfully pushed AndroidRingtone.mp3 to the device.")
else:
self.status_text.append(f"Error pushing to device: {stderr.decode()}")
except Exception as e:
self.status_text.append(f"Failed to push file to device: {str(e)}")
def download_video(self):
url = self.url_input.text()
if url:
self.status_text.append(f"Started downloading video for: {url}")
self.thread = DownloadThread(url, 'mp4')
self.thread.update_signal.connect(self.update_status)
self.thread.download_complete_signal.connect(self.enable_buttons)
self.thread.start()
else:
self.status_text.append('No video URL input')
def android_download(self):
self.start_conversion('android', "Creating Android Ringtone")
def iphone_download(self):
self.start_conversion('iphone', "Creating iPhone ringtone")
def start_conversion(self, file_type, message):
url = self.url_input.text()
if url:
self.status_text.append(message)
self.thread = DownloadThread(url, file_type)
self.thread.update_signal.connect(self.update_status)
self.thread.download_complete_signal.connect(self.enable_buttons)
self.thread.start()
else:
self.status_text.append('No video URL input')
def update_status(self, message):
self.status_text.append(message)
logging.info(message)
def enable_buttons(self):
self.android_button.setEnabled(True)
self.iphone_button.setEnabled(True)
def open_folder(self):
try:
if sys.platform == 'win32':
os.startfile(DOWNLOAD_FOLDER)
elif sys.platform == 'darwin':
subprocess.call(['open', DOWNLOAD_FOLDER])
else:
subprocess.call(['xdg-open', DOWNLOAD_FOLDER])
except Exception as e:
self.status_text.append(f"Failed to open downloads folder: {str(e)}")
def paste_from_clipboard(self):
url = pyperclip.paste()
self.url_input.setText(url)
def clear_input(self):
self.url_input.clear()
self.status_text.clear()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())