Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
artyshko committed May 17, 2019
2 parents 8694ff3 + 505c368 commit f32c422
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 22 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ requests==2.18.4
mutagen==1.41.0
notify2==0.3.1
pygame==1.9.4
git+git://github.com/nficano/pytube.git
youtube_dl==2019.5.11
pytube==9.4.0
pyperclip==1.6.4
lxml==4.2.3
moviepy==0.2.3.5
Expand Down
124 changes: 103 additions & 21 deletions youtube.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/python3
from __future__ import unicode_literals
import youtube_dl

from pytube import YouTube
from bs4 import BeautifulSoup
import requests
Expand All @@ -9,9 +12,21 @@
#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
# 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)

class Youtube(object):

Expand Down Expand Up @@ -42,6 +57,8 @@ def removeInvallidLinks(self):

def get(self, text, dur):

text = str(text).replace('&','')

data1 = self.getVideoFromYoutube(text)
data2 = self.getVideoFromYoutube(text + ' Audio')

Expand All @@ -57,6 +74,8 @@ def getVideoFromYoutube(self,text):
: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')
Expand All @@ -77,42 +96,80 @@ def download(self, url, path='', filename='video'):
:param filename: name of file
:return: str, filename
'''
#logging
logging.info(f"Start downloading")
try:

yt = YouTube(url)
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")

#downloading
yt = yt.streams.filter(
progressive=True,
file_extension='mp4'
).order_by('resolution').desc().first()

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

try:os.makedirs('cache/'+path)
except:pass
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")


print(filename)
ydl_opts = {
'outtmpl': f'{fullpath}/{filename}/{filename}',
'format':'best'
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])

yt.download('cache/'+ path, filename=path)
os.system(f'cp {fullpath}/{filename}/{filename} {fullpath}/{filename}/{filename}.mp4')

#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:
pass
#logging
logging.error(f"Youtube:os.makedirs(fullpath)")

clip = mp.VideoFileClip(f'cache/{uri}/{uri}.mp4').subclip()
clip.audio.write_audiofile(f'cache/{uri}/{uri}.mp3', bitrate='3000k', progress_bar=False)

logging.info(f"Converting successful")

try:

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

except Exception as e:
logging.error(f"Youtube.convertVideoToMusic")
return -1

finally:
Expand Down Expand Up @@ -140,19 +197,33 @@ def classify(self, data1, data2, duration=229486):
link = None

for item in research:


try:

y = YouTube(item)
try:item = str(item).replace('com//watch','com/watch')
except:pass

item_duration = int(y.length)*1000
ydl_opts = {
'outtmpl': f'1',
'format':'best'
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
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

if (result == -1 or diff < result) and not str(y.title).find('8D') > -1:
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:
pass
#logging
logging.error(f"Some problems on classify loop")

if link:
_result = [link] + data1 + data2
Expand Down Expand Up @@ -186,8 +257,19 @@ def getNameFromYoutube(self, url):

return name



if __name__ == "__main__":

y = Youtube()
name = y.getNameFromYoutube('https://www.youtube.com/watch?v=YAqm_vUeUik')
print(name)
#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'])

0 comments on commit f32c422

Please sign in to comment.