Erm... let's think for a second, is it possible to make a YT Downloader in Python? Well, actually, it is possible to do one. And yes, I made one.
Why you should make your own YT downloader?
The reason why I made a YT downloader is to download YT vids without going to other third-party websites like y2mate.com (which I commonly use) and savefrom.net. And my download was disturbed by some 18+ content (i am a 13 year old dev) that is really annoying and really disturbing to see. And I got inspired to make CLDownloader because of a TikTok video found on my For You page that uses Pafy. Without further ado, let's go!
Let's go!!!!
You need the following thing to get started.
Python 3.7
PyTube extension (achieved by
pip3 install pytube
)The CLDownloader gist (not needed if you want to explore around the code)
Okay, so you need that PyTube so we can download videos. You can do it by achieving this code.
pip3 install pytube
Otherwise, there will be an error running the script such as this.
Traceback (most recent call last):
File "downloader.py", line 1, in <module>
from pytube import YouTube
ModuleNotFoundError: No module named 'pytube'
After installing PyTube, we can check the presence by opening your Python shell and then type
from pytube import YouTube
If it outputs nothing, that means you succeeded!
Now we are creating a file called download.py
. In the contents of download.py
, import the PyTube we have did before. We will start with this loop.
while True:
Why I am putting a loop, you might be asking? This is because we need it if we want to download another video. In the loop, put a declaration statement as shown here.
link = input('Link: ')
yt = YouTube(link)
Now, we need to fetch the metadata of the video such as title, likes, etc. We will achieve it with this.
print('Title: ', yt.title)
print('Views: ', yt.views)
print('Length: ', yt.length)
print('Rating: ', yt.rating)
Output should look something like this.
Now, we will get the highest resolution and immediately download it.
ys = yt.streams.get_highest_resolution()
ys.download()
If you only set the download()
only method, it will save on anywhere it wants (idk where it is basically). But if you want to specify a custom directory, place the dir in the brackets of the download. I am using the OSes default profile dir, so we have to enter it as this.
ys.download(os.path.join(os.path.expandvars("%userprofile%"),"CLDownloader"))
On any download (whether on the dir or the default) it will always appear the same and this downloaded video on the script we made is actually playable and keeps the resolution like this.
Now, we need the user's confirmation to download again or not. So we will enter this code.
retry = input('Do you want to download more videos?')
if retry == 'y':
continue
else:
print('See you next time!!!!!!!')
time.sleep(2)
quit()
The continue
syntax here is used to loop again from the beginning of the while
command. Our full program should look like this.
from pytube import YouTube
import time
import os
while True:
link = input('Link: ')
yt = YouTube(link)
print('Title: ', yt.title)
print('Views: ', yt.views)
print('Length: ', yt.length)
print('Rating: ', yt.rating)
ys = yt.streams.get_highest_resolution()
ys.download(os.path.join(os.path.expandvars("%userprofile%"),"CLDownloader"))
retry = input('Do you want to download more videos?')
if retry == 'y':
continue
else:
print('See you next time!!!!!!!')
time.sleep(2)
quit()
And that is the end of the tutorial, if you want the full one, you may have the Gist. That is all for now, if you have any issues, don't be hesitated to ask me. That is all for now, and I will see you next time.
Top comments (0)