-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutube_music_downloader.py
More file actions
118 lines (94 loc) · 3.66 KB
/
Youtube_music_downloader.py
File metadata and controls
118 lines (94 loc) · 3.66 KB
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
import yt_dlp as yt
from PySide6.QtCore import Signal, QObject
class DownloadThread(QObject):
sig = Signal(list)
finished = Signal()
def __init__(self, url, path, id):
super().__init__(parent=None)
self.id = id
self.url = url
self.path = path
def run(self):
download(self.url, self.path, self.sig, self.id)
self.finished.emit()
class Logger:
def __init__(self, signal: Signal, id):
self.signal = signal
self.id = id
def debug(self, msg):
if msg.startswith('[debug] '):
pass
else:
self.info(msg)
def info(self, msg: str):
# [youtube] handling
if msg.startswith('[youtube] Extracting URL'):
self.signal.emit([self.id, 'Extracting:' + msg.split(':', 1)[1]])
elif msg.startswith('[youtube]'):
pass
# [info] handling
elif msg.startswith('[info]'):
pass
# [download] handling
elif msg.startswith('[download]'):
msg = msg.lstrip('[download] ')
if msg.startswith('Destination'):
msg = msg.rsplit('.', 1)[0].rsplit('\\', 1)[1]
self.signal.emit([self.id, 'Name: ' + msg])
elif not (msg.startswith('Resuming') or msg.endswith('downloaded')):
_of = msg.split('of ')
if 'ETA' in msg:
_at = _of[1].split('at ')
_in = _at[1].split('ETA ')
# format: percent, speed, time, size
self.signal.emit([self.id,
[_of[0].replace(' ', ''),
_in[0].replace(' ', ''),
_in[1].replace(' ', ''),
_at[0].replace(' ', '')
]
])
elif 'in' in msg:
_in = _of[1].split('in ')
_at = _in[1].split('at ')
self.signal.emit([self.id,
[_of[0].replace(' ', ''),
_at[1].replace(' ', ''),
_at[0].replace(' ', ''),
_in[0].replace(' ', '')
]
])
else:
self.signal.emit([self.id, 'Already downloaded: ' + _of[1].replace(' ', '')])
# [ExtractAudio] handling
elif msg.startswith('[ExtractAudio]'):
msg = msg.rsplit('\\', 1)[1]
self.signal.emit([self.id, 'Destination: ' + msg])
# Deleting handling
elif msg.startswith('Deleting'):
self.signal.emit([self.id, 'Delete'])
# other case
else:
self.signal.emit([self.id, msg, 'else'])
def warning(self, msg):
pass
def error(self, msg):
print(msg)
def download(video_url, folder, signal=None, id=None, format='mp3'):
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': format,
'preferredquality': '192',
}],
'restrictfilenames': True,
'outtmpl': folder + '/%(title)s.%(ext)s',
}
if signal is not None and id is not None:
ydl_opts['logger'] = Logger(signal, id)
with yt.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
if __name__ == "__main__":
DownloadThread('https://www.youtube.com/watch?v=Jv2uxzhPFl4', './', 0, None).run()
# lignes de YoutubeDL.py: 375