Last active
June 28, 2021 05:47
-
-
Save utgupta27/c6c430b897c121f45c5b45c2e43599b0 to your computer and use it in GitHub Desktop.
Implementation of PyTube package to download Youtube Videos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing pytube module for downloading and selecting the streams for the video to be downloaded | |
from pytube import YouTube | |
# Importing the OS module for accessing system functionality | |
# 'getlogin' gives the username of the user who is logged in currently | |
# 'remove' to delete the junk files from the system to free up space | |
from os import remove,getlogin | |
# Importing the platform module to identify the type of system on which the program is being used. | |
from platform import system | |
def get_video_info(url): | |
details=[] | |
update() | |
yt=YouTube(url) | |
main.update() | |
details.append(yt.title) | |
details.append(yt.rating) | |
details.append(yt.length) | |
details.append(yt.views) | |
main.update() | |
check=True | |
return details | |
def initialisePath(): | |
global path | |
if system() == 'Linux': | |
path = "/home/"+ getlogin()+"/Downloads/" | |
elif system() == 'Windows': | |
path = "C:/Users/"+ getlogin() + "/Downloads/" | |
pathDisplay.config( | |
text= "Location: "+path, | |
font= ("calibri",12,"bold"), | |
bg='light blue' | |
) | |
def getPath(): | |
global path | |
path = filedialog.askdirectory() | |
pathDisplay.config( | |
text= "Location: "+path, | |
font= ("calibri",12,"bold"), | |
bg='light blue' | |
) | |
status() | |
def status(): | |
videoIndex = index.get() | |
prefferedStream = videoStream[videoIndex] | |
fileSize = prefferedStream.filesize | |
if fileSize>1024 *1024 *1024: | |
size = fileSize/(1024*1024) | |
unit = " GB" | |
elif fileSize > 1024*1024: | |
size = fileSize//(1024) | |
unit = " MB" | |
else: | |
size = fileSize | |
unit = " KB" | |
sizeDisplay.config( | |
text="File Size: "+ str(size/1024)[:4]+ unit, | |
font= ("calibri",12,"bold"), | |
bg='light blue' | |
) | |
def get_streams(url): | |
global yt | |
streams = [] | |
main.update() | |
yt= YouTube(url,on_progress_callback=progress_check) | |
main.update() | |
streams.append(yt.streams.get_by_itag(22)) | |
main.update() | |
streams.append(yt.streams.get_by_itag(18)) | |
main.update() | |
streams.append(yt.streams.get_by_itag(133)) | |
main.update() | |
streams.append(yt.streams.get_by_itag(160)) | |
main.update() | |
streams.append(yt.streams.get_by_itag(140)) | |
main.update() | |
streams.append(yt.streams.get_by_itag(18)) | |
main.update() | |
return streams | |
def resolveLink(link): | |
remove="https://www.youtube.com/watch?v=" | |
reslove ="https://youtu.be/" | |
if link[:len(remove)] == remove : | |
newLink = reslove+link[len(remove):] | |
return newLink | |
else: | |
return link | |
def getVideoDetails(): | |
global check | |
check =False | |
global data | |
global videoStream | |
videoUrl = resolveLink(inputlink.get()) | |
try: | |
data= get_video_info(videoUrl) | |
main.update() | |
videoStream = get_streams(videoUrl) | |
main.update() | |
except: | |
print("Error Occured: Enter a Valid Url...") | |
else: | |
print("Video Found: Available for download ") | |
print(data) | |
initialisePath() | |
status() | |
def download(): | |
global fileSize | |
main.update() | |
videoIndex = index.get() | |
try: | |
if check == False : | |
pass | |
except: | |
print("Error Downlaod") | |
prefferedStream = videoStream[videoIndex] | |
fileSize = prefferedStream.filesize | |
try: | |
main.update() | |
prefferedStream.download(path) | |
main.update() | |
except: | |
print("Download Failed") | |
else: | |
print("Downlaod Success") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment