-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp_func.py
379 lines (210 loc) · 10.4 KB
/
mp_func.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
#GPL-3.0-or-later
from ast import match_case
from genericpath import isdir, isfile
from pytube import YouTube as yt, Playlist as pl
import sounddevice as sd
import soundfile as sf
import pydub as pd
import requests
import shutil as sh
from send2trash import send2trash as s2t
import os
class started():
def __init__(self):
self.current = os.getcwd()
self.download_dir = self.check_download_dir()
self.ffmpeg_extensions = self.check_ffmpeg_extensions()
self.op_system = os.name
def check_download_dir(self):
download_dir = os.path.join(self.current, ".downloads")
if os.path.exists(download_dir) == False: os.mkdir(download_dir)
return download_dir
def check_ffmpeg_extensions(self):
ffmpeg_extensions = os.path.join(self.current, ".ffmpeg_extensions.txt")
if os.path.exists(ffmpeg_extensions) == False:
file = requests.get("https://raw.githubusercontent.com/pietrop/ffmpeg_formats_list/master/ffmpeg_extentions.js", allow_redirects=True)
with open(".ffmpeg_extensions.txt", "w") as extensions:
refined_text = str(file.content).replace("module.exports= ", "")
extensions.write(refined_text)
return ffmpeg_extensions
class files():
def __init__(self, download_dir, op_system):
self.download_dir = download_dir
self.op_system = op_system
def extract(self, file_name = str):
if os.path.isfile(file_name):
relatives = file_name.split("/")
parents = "/".join(relatives[:len(relatives) - 1])
file, suffix = relatives[len(relatives) - 1].split(".")
print(parents, file, suffix)
return parents, file, suffix
else: return file_name
def copy(self, abs_path, ffmpeg_extensions):
if os.path.isfile(abs_path):
parents, file, suffix = self.extract(abs_path)
if suffix in open(ffmpeg_extensions, "r").read():
new_location = os.path.join(self.download_dir, f"{file}.{suffix}")
sh.copy(abs_path, new_location)
self.convert(new_location)
self.remove(new_location)
else: raise Exception("An error occurred while converting, no audio file was found")
else:
dir_origin = os.listdir(abs_path)
for music_files in dir_origin:
old_location = os.path.join(abs_path, music_files)
new_location = os.path.join(self.download_dir, music_files)
sh.copy(old_location, new_location)
self.convert(new_location)
self.remove(new_location)
print("Transfer completed")
def convert(self, file_name):
if os.path.isabs(file_name):
parents, file, suffix= self.extract(file_name)
if os.path.isfile(file_name): song = pd.AudioSegment.from_file(file_name, suffix)
else: raise Exception("An error occurred while converting, no file was found")
else:
relatives = file_name.split(".")
file, suffix = "".join(relatives[:len(relatives)-1]), relatives[len(relatives)-1]
song = pd.AudioSegment.from_file(f"{self.download_dir}/{file_name}", suffix)
song.export(f"{self.download_dir}/{file}.wav", "wav")
def remove(self, file_name):
if self.download_dir in file_name: s2t(file_name)
else: s2t(os.path.join(self.download_dir, file_name))
class downloader():
def __init__(self, download_dir, op_system):
self.download_dir = download_dir
self.op_system = op_system
self.file_handler = files(self.download_dir, self.op_system)
def download_audio(self, url, name = str):
audio = yt(url)
stream = audio.streams.get_audio_only("mp4")
stream_filename = f"{audio.title}.{stream.subtype}" if name == "non" else f"{name}.{stream.subtype}"
stream.download(output_path = self.download_dir, filename = stream_filename)
self.file_handler.convert(stream_filename)
self.file_handler.remove(stream_filename)
print(f"{stream_filename} was downloaded")
def download_playlist(self, url):
playlist = pl(url)
for video in playlist.videos:
stream = video.streams.get_audio_only("mp4")
stream_filename = f"{video.title}.{stream.subtype}"
stream.download(output_path = self.download_dir, filename = stream_filename)
self.file_handler.convert(stream_filename)
self.file_handler.remove(stream_filename)
print(f"{playlist.title} was downloaded")
def print_playlist(self, url):
playlist = pl(url)
count = 0
for video in playlist.videos: count += 1, print(f"{count}. -Stream: {video.title}\n{len(str(count)) + 1} -URL: {playlist.video_urls[count-1]}")
def print_audio_data(self, url):
audio = yt(url)
stream = audio.streams.get_audio_only("mp4")
print(f"""Name: {audio.title}\n
Author: {audio.author}\n
Length: {audio.length}\n
Size: {stream.filesize}\n
Views and rating: {audio.views} | {audio.rating}\n
Date: {audio.publish_date}""")
class player():
def __init__(self, download_dir):
self.download_dir = download_dir
self.music = os.listdir(self.download_dir)
def songs_listed(self):
for songs in self.music: print(f"{self.music.index(songs)+1}. {songs}".replace("_", " "))
choose = input("Song number: ")
assert choose.isnumeric
return int(choose)-1
def play(self, song_number=int):
song = self.music[int(song_number)]
data, sr = sf.read(f"{self.download_dir}/{song}")
sd.play(data, sr)
sd.wait()
class app():
def __init__(self):
directories = started()
self.current = directories.current
self.download_dir = directories.download_dir
self.ffmpeg_extensions = directories.ffmpeg_extensions
self.op_system = directories.op_system
def exec(self):
option = int(input("1. Play music downloaded\n"\
"2. Download music from YouTube\n"\
"3. Add music on local files\n"\
"\nOption: "))
self.clear_screen()
match option:
case 1:
music = player(self.download_dir)
song = music.songs_listed()
music.play(song)
self.clear_screen()
self.exec()
case 2:
option = int(input("1. Download a video\n"\
"2. Download a playlist\n"\
"\nOption: "))
self.clear_screen()
download = downloader(self.download_dir, self.op_system)
match option:
case 1:
url = input("Video URL: ")
assert any([domains in url for domains in ["youtube", "youtu.be"]]) and "watch" in url
download.print_audio_data(url)
aprobe = input("Confirm the download? (y/n): ")
match aprobe:
case "y" | "yes":
change = input("Want to rename the file? (y/n): ")
match change:
case "y" | "yes": new_name = input("New name (without suffix [ex. '.mp4']): ")
case "n" | "no": new_name = "non"
download.download_audio(url, new_name)
self.clear_screen(True)
self.exec()
case "n" | "no":
print("Canceling download")
self.clear_screen(True)
self.exec()
case _:
print("Invalid input")
self.clear_screen(True)
self.exec()
case 2:
url = input("Playlist url")
assert any([domains in url for domains in ["youtube", "youtu.be"]]) and "playlist" in url
download.print_playlist(url)
aprobe = input("Confirm the download? (y/n): ")
match aprobe:
case "y" | "yes":
download.download_playlist(url)
self.clear_screen(True)
self.exec()
case "n" | "no":
print("Canceling download")
self.clear_screen(True)
self.exec()
case _:
print("Invalid input")
self.clear_screen(True)
self.exec()
case _:
print("Invalid input")
self.clear_screen(True)
self.exec()
case 3:
abs_path = input("Introduce the abs_path of the file: ")
assert os.path.isabs(abs_path) and os.path.exists(abs_path)
files(self.download_dir, self.op_system).copy(abs_path, self.ffmpeg_extensions)
self.clear_screen(True)
self.exec()
case _:
print("Invalid input")
self.clear_screen(True)
self.exec()
def clear_screen(self, wait = False):
match self.op_system:
case "nt":
if wait == True: os.system("pause")
os.system("cls")
case "posix":
if wait == True: os.system("read -n 1 -s -p 'Pulse any key to continue...'")
os.system("clear")