Skip to content

Commit f32c422

Browse files
committed
Merge branch 'development'
2 parents 8694ff3 + 505c368 commit f32c422

File tree

2 files changed

+105
-22
lines changed

2 files changed

+105
-22
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ requests==2.18.4
44
mutagen==1.41.0
55
notify2==0.3.1
66
pygame==1.9.4
7-
git+git://github.com/nficano/pytube.git
7+
youtube_dl==2019.5.11
8+
pytube==9.4.0
89
pyperclip==1.6.4
910
lxml==4.2.3
1011
moviepy==0.2.3.5

youtube.py

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/python3
2+
from __future__ import unicode_literals
3+
import youtube_dl
4+
25
from pytube import YouTube
36
from bs4 import BeautifulSoup
47
import requests
@@ -9,9 +12,21 @@
912
#FIX STARTUP PYGAME HELLO MESSAGE
1013
#THANKS @Mad Physicist FROM STACK OVERFLOW
1114
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)
1530

1631
class Youtube(object):
1732

@@ -42,6 +57,8 @@ def removeInvallidLinks(self):
4257

4358
def get(self, text, dur):
4459

60+
text = str(text).replace('&','')
61+
4562
data1 = self.getVideoFromYoutube(text)
4663
data2 = self.getVideoFromYoutube(text + ' Audio')
4764

@@ -57,6 +74,8 @@ def getVideoFromYoutube(self,text):
5774
:return: list of results
5875
'''
5976

77+
logging.info(f"Finding")
78+
6079
request = self.__url + str(text).replace(' ','+')
6180
response = requests.get(request, headers=self.headers)
6281
soup = BeautifulSoup(response.text,'lxml')
@@ -77,42 +96,80 @@ def download(self, url, path='', filename='video'):
7796
:param filename: name of file
7897
:return: str, filename
7998
'''
99+
#logging
100+
logging.info(f"Start downloading")
80101
try:
81102

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")
83113

84-
#downloading
85-
yt = yt.streams.filter(
86-
progressive=True,
87-
file_extension='mp4'
88-
).order_by('resolution').desc().first()
89114

90115
fullpath = os.getcwd() + '/cache'
91116

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])
94138

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")
96145

97146
return filename
98147
except: return None
99148

100149

101150
def convertVideoToMusic(self, uri):
151+
#logging
152+
logging.info(f"Start converting")
102153

103154
try:
104155
fullpath = os.getcwd() + f'/cache/{uri}/'
105156
if not os.path.exists(fullpath):
106157
os.makedirs(fullpath)
107158
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")
109166

110167
try:
111168

112-
clip = mp.VideoFileClip(f'cache/{uri}/{uri}.mp4').subclip()
113-
clip.audio.write_audiofile(f'cache/{uri}/{uri}.mp3', bitrate='3000k')
169+
pass
114170

115171
except Exception as e:
172+
logging.error(f"Youtube.convertVideoToMusic")
116173
return -1
117174

118175
finally:
@@ -140,19 +197,33 @@ def classify(self, data1, data2, duration=229486):
140197
link = None
141198

142199
for item in research:
200+
201+
143202
try:
144203

145-
y = YouTube(item)
204+
try:item = str(item).replace('com//watch','com/watch')
205+
except:pass
146206

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
148216
diff = duration - item_duration
149217
diff = diff * -1 if diff < 0 else diff
150218

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:
152222
result, link = diff, item
153223

154224
except:
155-
pass
225+
#logging
226+
logging.error(f"Some problems on classify loop")
156227

157228
if link:
158229
_result = [link] + data1 + data2
@@ -186,8 +257,19 @@ def getNameFromYoutube(self, url):
186257

187258
return name
188259

260+
261+
189262
if __name__ == "__main__":
190263

191264
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

Comments
 (0)