How I made my very own YT downloader in Python
Tags: python, youtube, pytube
Published: 2021-12-02 12:18:52

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.
![shit image 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gapk6vpbt823epdvruww.png)

## 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](https://gist.github.com/zeankundev/12dc3d5395d07444840c50e6bf6ecfa3) (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

```py
from pytube import YouTube
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1aweizrrgjl1loqls12.png)

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.
```py
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.

```py
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.
```py
print('Title: ', yt.title)
print('Views: ', yt.views)
print('Length: ', yt.length)
print('Rating: ', yt.rating)
```
Output should look something like this.![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1h4kgppw40h5jpwt8la.png)

Now, we will get the highest resolution and immediately download it. 

```py
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.

```py
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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y1oxjkp89anv45aubjl.png)
 
Now, we need the user's confirmation to download again or not. So we will enter this code.

```py
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.

```py
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](https://gist.github.com/zeankundev/12dc3d5395d07444840c50e6bf6ecfa3). 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.