1
1
#!/usr/bin/python3
2
+ from __future__ import unicode_literals
3
+ import youtube_dl
4
+
2
5
from pytube import YouTube
3
6
from bs4 import BeautifulSoup
4
7
import requests
9
12
#FIX STARTUP PYGAME HELLO MESSAGE
10
13
#THANKS @Mad Physicist FROM STACK OVERFLOW
11
14
import contextlib
12
- with contextlib .redirect_stdout (None ):
13
- from moviepy .editor import *
14
- import moviepy .editor as mp
15
+ # with contextlib.redirect_stdout(None):
16
+ # from moviepy.editor import *
17
+ # import moviepy.editor as mp
18
+
19
+ import imageio
20
+ imageio .plugins .ffmpeg .download ()
21
+ from moviepy .editor import *
22
+ import moviepy .editor as mp
23
+
24
+ import logging
25
+
26
+ logging .basicConfig (level = logging .INFO ,
27
+ format = '%(asctime)s - %(levelname)-2s - %(message)s' )
28
+ console = logging .StreamHandler ()
29
+ console .setLevel (logging .INFO )
15
30
16
31
class Youtube (object ):
17
32
@@ -42,6 +57,8 @@ def removeInvallidLinks(self):
42
57
43
58
def get (self , text , dur ):
44
59
60
+ text = str (text ).replace ('&' ,'' )
61
+
45
62
data1 = self .getVideoFromYoutube (text )
46
63
data2 = self .getVideoFromYoutube (text + ' Audio' )
47
64
@@ -57,6 +74,8 @@ def getVideoFromYoutube(self,text):
57
74
:return: list of results
58
75
'''
59
76
77
+ logging .info (f"Finding" )
78
+
60
79
request = self .__url + str (text ).replace (' ' ,'+' )
61
80
response = requests .get (request , headers = self .headers )
62
81
soup = BeautifulSoup (response .text ,'lxml' )
@@ -77,42 +96,80 @@ def download(self, url, path='', filename='video'):
77
96
:param filename: name of file
78
97
:return: str, filename
79
98
'''
99
+ #logging
100
+ logging .info (f"Start downloading" )
80
101
try :
81
102
82
- yt = YouTube (url )
103
+ try :url = str (url ).replace ('com//watch' ,'com/watch' )
104
+ except :pass
105
+
106
+ #logging
107
+ logging .info (f"Init YouTube" )
108
+ logging .warning (f"URL { url } " )
109
+
110
+
111
+ #logging
112
+ logging .info (f"Create Directory" )
83
113
84
- #downloading
85
- yt = yt .streams .filter (
86
- progressive = True ,
87
- file_extension = 'mp4'
88
- ).order_by ('resolution' ).desc ().first ()
89
114
90
115
fullpath = os .getcwd () + '/cache'
91
116
92
- try :os .makedirs ('cache/' + path )
93
- except :pass
117
+ try :
118
+ # if not os.path.exists(fullpath):
119
+ # os.makedirs(fullpath)
120
+ os .makedirs ('cache/' + path )
121
+ #logging
122
+ logging .info (f"Created" )
123
+ except :
124
+ #logging
125
+ logging .error (f"Youtube:os.makedirs('cache/'+path)" )
126
+
127
+ #logging
128
+ logging .info (f"Start downloading" )
129
+
130
+
131
+ print (filename )
132
+ ydl_opts = {
133
+ 'outtmpl' : f'{ fullpath } /{ filename } /{ filename } ' ,
134
+ 'format' :'best'
135
+ }
136
+ with youtube_dl .YoutubeDL (ydl_opts ) as ydl :
137
+ ydl .download ([url ])
94
138
95
- yt .download ('cache/' + path , filename = path )
139
+ os .system (f'cp { fullpath } /{ filename } /{ filename } { fullpath } /{ filename } /{ filename } .mp4' )
140
+
141
+ #yt.download('cache/'+ path, filename=path)
142
+
143
+ #logging
144
+ logging .info (f"Downloading successful" )
96
145
97
146
return filename
98
147
except : return None
99
148
100
149
101
150
def convertVideoToMusic (self , uri ):
151
+ #logging
152
+ logging .info (f"Start converting" )
102
153
103
154
try :
104
155
fullpath = os .getcwd () + f'/cache/{ uri } /'
105
156
if not os .path .exists (fullpath ):
106
157
os .makedirs (fullpath )
107
158
except :
108
- pass
159
+ #logging
160
+ logging .error (f"Youtube:os.makedirs(fullpath)" )
161
+
162
+ clip = mp .VideoFileClip (f'cache/{ uri } /{ uri } .mp4' ).subclip ()
163
+ clip .audio .write_audiofile (f'cache/{ uri } /{ uri } .mp3' , bitrate = '3000k' , progress_bar = False )
164
+
165
+ logging .info (f"Converting successful" )
109
166
110
167
try :
111
168
112
- clip = mp .VideoFileClip (f'cache/{ uri } /{ uri } .mp4' ).subclip ()
113
- clip .audio .write_audiofile (f'cache/{ uri } /{ uri } .mp3' , bitrate = '3000k' )
169
+ pass
114
170
115
171
except Exception as e :
172
+ logging .error (f"Youtube.convertVideoToMusic" )
116
173
return - 1
117
174
118
175
finally :
@@ -140,19 +197,33 @@ def classify(self, data1, data2, duration=229486):
140
197
link = None
141
198
142
199
for item in research :
200
+
201
+
143
202
try :
144
203
145
- y = YouTube (item )
204
+ try :item = str (item ).replace ('com//watch' ,'com/watch' )
205
+ except :pass
146
206
147
- item_duration = int (y .length )* 1000
207
+ ydl_opts = {
208
+ 'outtmpl' : f'1' ,
209
+ 'format' :'best'
210
+ }
211
+
212
+ with youtube_dl .YoutubeDL (ydl_opts ) as ydl :
213
+ dictMeta = ydl .extract_info (item , download = False )
214
+
215
+ item_duration = int (dictMeta ['duration' ])* 1000
148
216
diff = duration - item_duration
149
217
diff = diff * - 1 if diff < 0 else diff
150
218
151
- if (result == - 1 or diff < result ) and not str (y .title ).find ('8D' ) > - 1 :
219
+ logging .warning (f'{ item } { item_duration } ' )
220
+
221
+ if (result == - 1 or diff < result ) and not str (dictMeta ['title' ]).find ('8D' ) > - 1 :
152
222
result , link = diff , item
153
223
154
224
except :
155
- pass
225
+ #logging
226
+ logging .error (f"Some problems on classify loop" )
156
227
157
228
if link :
158
229
_result = [link ] + data1 + data2
@@ -186,8 +257,19 @@ def getNameFromYoutube(self, url):
186
257
187
258
return name
188
259
260
+
261
+
189
262
if __name__ == "__main__" :
190
263
191
264
y = Youtube ()
192
- name = y .getNameFromYoutube ('https://www.youtube.com/watch?v=YAqm_vUeUik' )
193
- print (name )
265
+ #name = y.get(text="Sean Paul & J Balvin - Contra La Pared", dur=256271)
266
+ y .download (url = 'https://www.youtube.com//watch?v=l91u752OCPo' , path = 'boom' ,filename = 'file' )
267
+
268
+
269
+
270
+ # ydl_opts = {
271
+ # 'outtmpl': 'videoo.%(ext)s',
272
+ # 'format':'137'
273
+ # }
274
+ # with youtube_dl.YoutubeDL(ydl_opts) as ydl:
275
+ # ydl.download(['https://www.youtube.com/watch?v=dP15zlyra3c'])
0 commit comments