How to Make a Simple YouTube Downloader in Python

Have you ever wondered if you could download YouTube videos without having to visit random websites in their maximum quality possible? Well, today in this tutorial we are going to do exactly that. We will be writing a very simple YouTube downloader script that allows you to not only download any video but also lets you download an entire playlist. But what if you don’t want to download an entire playlist but only some selected videos in that playlist? Well, you can do that too. And the best part is you can do all that under 50 lines of code. So, let’s get started.

This tutorial is based on a library called PyTube and we will be using that library to communicate with YouTube and extract information about the video or playlist and capture the stream to download the video).

 So, first thing first, you need to install the PyTube library in your PC and to do so simply open the command prompt and type:

pip install pytube

We are also using a library called progress bar to show the progress while downloading the playlist, so we need to install that as well. To do so type:

pip install progress

Once the installation of both these libraries is done, we are good to go.  We will start by importing the libraries into our python script. As shown in the image below.

Once that is done, we will create an object that will download the entire playlist or a single video based on the user input as shown in line 18 above.

If the user has selected 1 then the script will execute the statement and will be presented with two options again. The first option will be to download the entire playlist and the second option is to download selected videos within that playlist. If the user selects option 1 the program will download the entire playlist as shown in the image below.

Similarly, if the user selected option 2 then the program will ask for the video index number (Number starts from 0) to download that specific video from the playlist instead of downloading all the videos.

Now if the User selected 2 in the beginning, they will be asked to enter the URL link of the video that they wish to download, and the program will download that video as shown in the image.

So, this was a very simple but very effective YouTube downloader app that you can use to download any video you want in just a few clicks without having to visit random shady website or use any third-party application that asks monthly subscription or is filled with thousands of ads. The complete code is given below. Feel free to tweak the script and see what else the library has to offer.

# A simple youtube downloader 
'''This is a Simple Youtube Downloader based of Pytube module that can download
    1. Single Video File
    2. Entire Video playlist
    3. Select Videos from a playlist
    4. Single Audio File
    5. Entire Audio Playlist
    6. Select Audios from a playlist

Disclaimer : Some Files may fail to download due to privacy settings or unavailability of the video
             in a given region. This downloader uses progressing stream which limits the maximum
             video quality to 720P. (Modify to DASH if you wish to download videos up to 4K resolution)
'''
from progress.bar import ChargingBar
from pytube import YouTube,Playlist

print('-'*70)
print(f'Download:\n1. Playlist\n2. Video')
print('-'*70)
inp = int(input('Choice:'))

if inp == 1:
    plink = input('playlist link:')
    p = Playlist(plink)
    vids = len([url for url in p.video_urls])
    # Details about playlist
    print('Playlist Title:',p.title)
    print('No of Videos  :',vids)
    print('\nEntire Playlist or Select Videos?:')
    print(f'1. Playlist\n2. Select Videos')
    
    pinput = int(input('Choice:'))

# Downloading Entire Playlist    
    if pinput == 1:
        bar = ChargingBar(max = vids)
        for video in p.videos:
            print(f' Downloading: {video.title}')
            video.streams.get_highest_resolution().download()
            bar.next()
        bar.finish()
    
    elif pinput == 2:
        print('Enter Video indexes[Index starting from 0]:')
        try:
            v_list = []
            while True:
                v_list.append(int(input()))
        except:
            print(v_list)
        
# Downloading Select Videos from Playlist based on video index
        bar = ChargingBar()
        for x, video in enumerate(p.videos):
            if x in v_list:
                video.streams.get_highest_resolution().download()
                bar.next()    
            else:
                print(f'skipped {video.title}')
        bar.finish()

# Downloading Video 
elif inp == 2:
    try:
        vlink = input('Video Link:')
        v = YouTube(vlink)
        print(f'downloading {v.title}')
        v.streams.get_highest_resolution().download() 
    except:
        print('Sorry! This Video is Unavailable')

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO