This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
forked from artyshko/smd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.py
executable file
·299 lines (216 loc) · 7.69 KB
/
youtube.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
#!/usr/bin/python3
from __future__ import unicode_literals
import youtube_dl
from pytube import YouTube
from bs4 import BeautifulSoup
import requests
import lxml
import os
#IMPORT WITH STDOUT REDIRECTION
#FIX STARTUP PYGAME HELLO MESSAGE
#THANKS @Mad Physicist FROM STACK OVERFLOW
import contextlib
with contextlib.redirect_stdout(None):
from moviepy.editor import *
import moviepy.editor as mp
import imageio
imageio.plugins.ffmpeg.download()
from moviepy.editor import *
import moviepy.editor as mp
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)-2s - %(message)s')
# console = logging.StreamHandler()
# console.setLevel(logging.INFO)
from contextlib import contextmanager
import sys, os
@contextmanager
def suppress_stdout1():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
@contextmanager
def suppress_stdout():
new_target = open(os.devnull, "w")
old_target = sys.stdout
sys.stdout = new_target
try:
yield new_target
finally:
sys.stdout = old_target
class Youtube(object):
def __init__(self):
self.__query = ''
self.__host = 'https://www.youtube.com/'
self.__url = self.__host + 'results?search_query='
self.headers = {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}
self.__result = []
def getResult(self,i=0):
return self.__result[i]
def getFullResult(self):
return self.__result
def removeInvallidLinks(self):
temp = []
for item in self.getFullResult():
if 40 < len(item) < 50:
temp.append(item)
self.__result = temp
def get(self, text, dur):
text = str(text).replace('&','')
data1 = self.getVideoFromYoutube(text)
data2 = self.getVideoFromYoutube(text + ' Audio')
self.__result = self.classify(data1, data2, dur)
return self.__result
def getVideoFromYoutube(self,text):
'''
Getting song url from YouTube
:param text: name of song
:return: list of results
'''
#logging.info(f"Finding")
request = self.__url + str(text).replace(' ','+')
response = requests.get(request, headers=self.headers)
soup = BeautifulSoup(response.text,'lxml')
self.__result = []
for link in soup.findAll(attrs={'class': 'yt-uix-tile-link'}):
self.__result.append(self.__host + link['href'])
self.removeInvallidLinks()
return self.__result
def download(self, url, path='', filename='video'):
'''
Downloading song from YouTube
:param url: video url on YouTube
:param path: local directory
:param filename: name of file
:return: str, filename
'''
#logging
#logging.info(f"Start downloading")
try:
try:url = str(url).replace('com//watch','com/watch')
except:pass
#logging
#logging.info(f"Init YouTube")
#logging.warning(f"URL {url}")
#logging
#logging.info(f"Create Directory")
fullpath = os.getcwd() + '/cache'
try:
# if not os.path.exists(fullpath):
# os.makedirs(fullpath)
os.makedirs('cache/'+path)
#logging
#logging.info(f"Created")
except:
#logging
logging.error(f"Youtube:os.makedirs('cache/'+path)")
#logging
#logging.info(f"Start downloading")
ydl_opts = {
'outtmpl': f'{fullpath}/{filename}/{filename}.mp4',
'format':'best'
}
with suppress_stdout():
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
with suppress_stdout():
ydl.download([url])
#yt.download('cache/'+ path, filename=path)
#logging
#logging.info(f"Downloading successful")
return filename
except: return None
def convertVideoToMusic(self, uri):
#logging
#logging.info(f"Start converting")
try:
fullpath = os.getcwd() + f'/cache/{uri}/'
if not os.path.exists(fullpath):
os.makedirs(fullpath)
except:
#logging
logging.error(f"Youtube:os.makedirs(fullpath)")
print(uri)
clip = mp.VideoFileClip(f'cache/{uri}/{uri}.mp4').subclip()
clip.audio.write_audiofile(f'cache/{uri}/{uri}.mp3', bitrate='3000k', progress_bar=True)
#logging.info(f"Converting successful")
try:
pass
except Exception as e:
logging.error(f"Youtube.convertVideoToMusic")
return -1
finally:
return 0
def getTrack(self,name):
'''
quick download and convert to mp3
'''
#self.convertVideoToMusic(self.download(self.get(name)[0],filename=name))
return None
def classify(self, data1, data2, duration=229486):
data1 = data1[:2] if len(data1) >= 2 else data1
data2 = data2[:2] if len(data2) >= 2 else data2
research = data2 + data1
if duration == 0:
return research
result = -1
link = None
for item in research:
try:
try:item = str(item).replace('com//watch','com/watch')
except:pass
ydl_opts = {
'outtmpl': f'1',
'format':'best'
}
with suppress_stdout():
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
with suppress_stdout():
dictMeta = ydl.extract_info(item, download=False)
item_duration = int(dictMeta['duration'])*1000
diff = duration - item_duration
diff = diff * -1 if diff < 0 else diff
#logging.warning(f'{item} {item_duration}')
if (result == -1 or diff < result) and not str(dictMeta['title']).find('8D') > -1:
result, link = diff, item
except:
#logging
logging.error(f"Some problems on classify loop")
if link:
_result = [link] + data1 + data2
else:
_result = data1 + data2
return _result
def getNameFromYoutube(self, url):
response = requests.get(url, headers=self.headers)
soup = BeautifulSoup(response.text,'lxml')
_title = soup.find('title').text
_title = str(_title).replace(' - YouTube', '')
_result = []
__name = None
if not str(_title).find('-') > -1:
for link in soup.findAll('meta', attrs={'property': 'og:video:tag'}):
_result.append(link.get('content'))
if len(_result) > 1:
name = f"{_result[0]} - {_title}"
else:
name = _title
else:
name = _title
return name
if __name__ == "__main__":
y = Youtube()
#name = y.get(text="Sean Paul & J Balvin - Contra La Pared", dur=256271)
y.download(url='https://www.youtube.com//watch?v=l91u752OCPo', path='boom',filename='file')
# ydl_opts = {
# 'outtmpl': 'videoo.%(ext)s',
# 'format':'137'
# }
# with youtube_dl.YoutubeDL(ydl_opts) as ydl:
# ydl.download(['https://www.youtube.com/watch?v=dP15zlyra3c'])