-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3_dl.py
319 lines (292 loc) · 9.93 KB
/
mp3_dl.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
import youtube_dl, spotipy, os, requests, threading, queue
from spotipy.oauth2 import SpotifyOAuth
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, TIT2, TPE1, TALB, TRCK
from mutagen.id3 import ID3NoHeaderError
from youtube_dl.utils import DownloadError
from requests.exceptions import HTTPError
number_of_threads = 50
root = os.getcwd()
q = queue.Queue()
sp = None
count = 0
total = 0
ytdl_options = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'extractaudio': True,
'audioformat': 'mp3',
#'ffmpeg-location': os.getcwd() + '/ffmpeg/ffmpeg.exe',
'hls-prefer-ffmpeg': True,
'outtmpl': '%(id)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0'
}
ffmpeg_options = {
'options': '-vn',
}
ytdl = youtube_dl.YoutubeDL(ytdl_options)
def dl_yt_playlist(link, silent=False):
print('Gathering Youtube playlist data...')
try:
result = ytdl.extract_info(link, download=False)
except DownloadError as e:
if 'This video is DRM protected' in str(e):
print('ERROR: Invalid Youtube playlist URL')
return
playlist_name = result['title']
print('Downloading Youtube playlist: \"{}\"'.format(playlist_name))
playlist_name = legalize_chars(playlist_name)
if not os.path.exists(playlist_name):
os.mkdir(playlist_name)
os.chdir(playlist_name)
global count, total
total = len(result['entries'])
count = 0
for i in range(number_of_threads):
t = threading.Thread(target=yt_playlist_worker)
t.daemon = True
t.start()
for video in result['entries']:
q.put(video)
q.join()
os.chdir(root + '/out')
if not silent:
print('Playlist download complete!')
count = 0
total = 0
return True
def dl_yt_video(link, silent=True, recurse=0):
try:
if not silent:
print('Downloading Youtube video...')
result = ytdl.extract_info(link)
filename = '{}.mp3'.format(result['id'])
new_name = result['title']
new_name = '{}.mp3'.format(legalize_chars(new_name))
if not silent:
print('Audio from: \"{}\" Download complete!'.format(result['title']))
if os.path.exists(new_name):
os.remove(new_name)
os.rename(filename, new_name)
except (DownloadError, HTTPError) as e:
if 'ffprobe/avprobe and ffmpeg/avconv not found' in str(e):
return
if 'Incomplete YouTube ID' in str(e):
return
elif 'Video unavailable' in str(e):
return
if recurse >= 4:
print('Retry unsucessful!')
return
else:
print('ERROR: Download of: \"{}\" failed. Retrying...'.format(link))
dl_yt_video(link, silent=True, recurse=recurse+1)
if recurse:
print('Retry sucessful!')
try:
return filename
except UnboundLocalError as e:
if str(e) == 'local variable \'filename\' referenced before assignment':
return
def dl_query(query, silent=True, rename=False, recurse=0):
try:
query = query.replace('-', ' ')
if not silent:
print('Querying Youtube search for \"{}\"...'.format(query))
if rename:
result = ytdl.extract_info('ytsearch:{} lyrics'.format(query))
else:
result = ytdl.extract_info('ytsearch:{}'.format(query))
try:
filename = '{}.mp3'.format(result['entries'][0]['id'])
except Exception as e:
print(result)
raise e
new_name = result['entries'][0]['title']
if not silent:
print('Audio from: \"{}\" Download complete!'.format(new_name))
if rename:
new_name = '{}.mp3'.format(legalize_chars(new_name))
if os.path.exists(new_name):
os.remove(new_name)
os.rename(filename, new_name)
except (DownloadError, HTTPError) as e:
if 'ffprobe/avprobe and ffmpeg/avconv not found' in str(e):
return
if recurse >= 4:
print('Retry unsucessful! Please try again later.')
return None
else:
print('ERROR: Download for query: \"', query, '\" failed. Retrying...')
filename = dl_query(query, silent=True, rename=rename, recurse=recurse+1)
if recurse:
print('Retry sucessful, Download complete!')
return filename
def dl_spotify(input_link, silent=False):
if not sp:
spotipy_initialize()
playlist_name = input_link['name']
if len(input_link['tracks']['items'][0]) == 6:
playlist_type = 'playlist'
else:
playlist_type = 'album'
album = []
album.append(input_link['images'][0]['url'])
album.append(playlist_name)
print('Downloading Spotify {}: \"{}\"...'.format(playlist_type, playlist_name))
playlist_name = legalize_chars(playlist_name)
if not os.path.exists(playlist_name):
os.mkdir(playlist_name)
os.chdir(playlist_name)
playlist = input_link['tracks']
global total, count
while playlist['next']:
total = total + 100
playlist = sp.next(playlist)
tracks = playlist['items']
total = total + len(tracks)
for i in range(number_of_threads):
t = threading.Thread(target=sp_playlist_worker)
t.daemon = True
t.start()
playlist = input_link['tracks']
while playlist['next']:
tracks = playlist['items']
for track in tracks:
if playlist_type == 'playlist':
track = track['track']
if playlist_type == 'playlist':
args = [track, True, None]
else:
args = [track, True, album]
q.put(args)
playlist = sp.next(playlist)
tracks = playlist['items']
for track in tracks:
if playlist_type == 'playlist':
track = track['track']
if playlist_type == 'playlist':
args = [track, True, None]
else:
args = [track, True, album]
q.put(args)
q.join()
if not silent:
print('Playlist download complete!')
os.chdir(root + '/out')
count = 0
total = 0
return True
def dl_sp_track(track, silent=True, album=None):
if not sp:
spotipy_initialize
title = track['name']
artist = track['artists'][0]['name']
if not silent:
print('Downloading Spotify track: \"{} - {}\".'.format(title, artist))
query = '{} {}'.format(title, artist)
new_name = '{} - {}.mp3'.format(title, artist)
new_name = legalize_chars(new_name)
if os.path.isfile(new_name):
return
filename = dl_query(query, silent=True, rename=False)
if not filename:
return track
if not os.path.isfile(new_name):
os.rename(filename, new_name)
album_name = album[1] if album else track['album']['name']
track_number = str(track['track_number'])
thumbnail_name = '{}-{}.jpg'.format(legalize_chars(album_name), track_number)
with open(thumbnail_name, 'wb') as handle:
try:
if album:
thumbnail = requests.get(album[0]).content
else:
thumbnail = requests.get(track['album']['images'][0]['url']).content
handle.write(thumbnail)
except Exception as e:
if isinstance(e, KeyboardInterrupt):
raise e
print('ERROR: Processing of thumbnail for \"{} - {}\" failed.'.format(title, artist))
try:
audio = MP3(new_name, ID3=ID3)
audio.tags.add(
APIC(
encoding=3,
mime='image/jpg',
type=3,
desc=u'Cover',
data=open(thumbnail_name, 'rb').read()
)
)
audio.save(v2_version=3)
try:
tags = ID3(new_name)
except ID3NoHeaderError:
print("Adding ID3 header")
tags = ID3()
tags["TIT2"] = TIT2(encoding=3, text=title)
tags["TALB"] = TALB(encoding=3, text=album_name)
tags["TPE1"] = TPE1(encoding=3, text=artist)
tags["TRCK"] = TRCK(encoding=3, text=track_number)
tags.save(new_name)
except Exception as e:
if isinstance(e, KeyboardInterrupt):
raise e
if not ('Errno 13' in str(e)): print('ERROR: ID3 tags unable to be written.')
os.remove(thumbnail_name)
if not silent:
print('Download complete!')
def progress(count=0, total=1, song=''):
percentage = int(count * 1000 / total)
print(
'{}/{} '.format(count, total),
percentage / 10.0, '% ',
'\"{}\" Download Complete!'.format(song),
)
def legalize_chars(filename):
illegal_chars = ['<', '>', ':', '\"', '/', '\\', '|', '?', '*']
for char in illegal_chars:
if char in filename:
filename = filename.replace(char, '')
return filename
def spotipy_initialize(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET):
global sp
sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET,
redirect_uri='http://localhost:8000',
scope='user-library-read',
cache_path='{}/OAuthCache.txt'.format(root)
)
)
def sp_playlist_worker():
while True:
global count, total
args = q.get()
dl_sp_track(args[0], args[1], args[2])
count = count + 1
progress(count, total, '{} - {}'.format(
args[0]['name'],
args[0]['artists'][0]['name']
))
q.task_done()
def yt_playlist_worker():
while True:
global count, total
video = q.get()
dl_yt_video(video['webpage_url'])
count = count + 1
progress(count, total, video['title'])
q.task_done()