DEV Community

zeankun.dev
zeankun.dev

Posted on

How I made my very own YT downloader in Python

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

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

After installing PyTube, we can check the presence by opening your Python shell and then type

from pytube import YouTube
Enter fullscreen mode Exit fullscreen mode

Image description

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:
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output should look something like this.Image description

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

ys = yt.streams.get_highest_resolution()
ys.download()
Enter fullscreen mode Exit fullscreen mode

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"))
Enter fullscreen mode Exit fullscreen mode

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

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()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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)