Atulya Khatri — Published On February 1, 2022 and Last Modified On July 22nd, 2022
Beginner Python Videos

This article was published as a part of the Data Science Blogathon.


  • Introduction
  • What is a Youtube video downloader using python?
  • What other things can we do with it?
  • How to make it?


Have you ever felt if all the videos you watch on youtube be without ads? Well, there is a way, if you know python you can fulfill this dream with a few lines of code. Using the Youtube video downloader program, you can download a whole playlist or a specific video or all videos from a youtube channel, and then you can watch it without any ads or interruption. So let’s get started.

What is a Youtube video downloader using python?

Youtube video downloader is simply a tool for downloading youtube videos. Here we will use python and its libraries to make it. See there is a difference between downloaded videos on youtube with downloaded videos using our program. The main differences are, videos downloaded on youtube mobile will not be available or will be automatically deleted after about 90 days of no internet connection on the application and you can only play videos on the youtube application and it will not play on any other video players. But opposite to this, will be our program which will download videos on your hard drive or your storage and you can also play it on any other video players.

What other things can we do with it?

We will create a program that will have many functionalities like a whole playlist downloader, only audio file downloader, or only video without audio file downloader or whole channel downloader and such type of things.

Youtube Video Downloader using Python

Youtube logo

How to make it?

To make a program like this we will use “pytube” a python module to work with youtube.

Generally, there are many ways of downloading youtube videos, we have to code manually like using Selenium, bs4, or requests module. But the simplest of this is using pytube. It’s a very lightweight and simple to use python library with easy syntax. Now we will just create one.

First of all, install the pytube module using the command prompt.

So its syntax is this.

pip install pytube

Now we will start our code.

Here we have imported Youtube from pytube in order to work with youtube videos. In the next line, we have imported Playlist to work with Playlists on youtube and next, we have imported Channel from pytube to work with channels on youtube. At last, we have imported os to work with files on the hard drive.

def playlist(url):
    playlist = Playlist(url)
    print('Number of videos in playlist: %s' % len(playlist.video_urls))
    for video in playlist.videos:

Above we have created a function named playlist(). Clearly, it will deal with playlists. So inside the function, the first line initializes the Playlist which we have imported back into the variable playlist. Next, we have printed for mainly debugging purposes and it can also be called a feature of our program which prints the number of videos in the playlist and then counts video_urls from the imported playlist using the in-built len() function of python, we can show the total number of videos of a playlist to the user. Then using for loop, we will iterate through every video of the playlist and will download the videos with maximum resolution. So using “video” as every video iteration, we will use pytube’s stream attribute and then filter it with arguments as progressive=True which simply is how a video will be downloaded or more specifically if it will download audio and video files differently or both in the same and then we will make its file extension as mp4, as you know, it is used for video formats. Then using order_by(‘resolution’) which will apply for sort order. Filters out-stream that do not have the attribute. Then .desc() function which just sort streams in descending order. Then .first() function that gets the first stream in the results. And at last, it is download() function. At last for debugging and for adding an extra feature, we will just print Done!!

def video(url):
    video_caller = YouTube(url)

Now we have created a function named video which will simply download a video at the highest resolution. So we have created a video_caller variable that will initialize the imported YouTube which will take the input URL that the user enters. Again for debugging and adding a feature, we will print the title of the video using the title attribute of the video_caller which we have just created. Here we don’t have to work with many videos like we have to do for the playlist, so we can directly download the video. So using all the same properties as playlist’s videos properties we can just do the same. At last, just for debugging purposes, print Done!!

def channel(url):
    channel_videos = Channel(url)
    print(f'Downloading videos by: {channel_videos.channel_name}')
    for video in channel_videos.videos:

Again to download all the videos of a channel we have created a function named “channel” which will take the URL as a parameter. Same as all the above, we will do all the same things as using variable channel_videos which will initialize the imported Channel and will take URL as a parameter. For adding features as well as for debugging purposes, we will print the channel name, and same as the playlist function’s approach, we will download all videos with the highest resolution. Then will print done!!

def video_voice_only(url):
    video_caller = YouTube(url)
    audio = video_caller.streams.filter(only_audio=True).first()
    out_path =
    new_name = os.path.splitext(out_path)
    os.rename(out_path,new_name[0] + ".mp3")

Here in this function, the purpose is to save storage as audio format is generally of less size as compared to videos. So the function video_voice_only() takes URL as a parameter and all things will be the same as the video downloader function except here the attributes of filter will be only_audio and the first() function in the audio variable. Next, we will create the out_path variable which will download the file with output_path = video_caller.title and it will save the file as the title of the video. Here it is downloaded in video format, we will convert it to audio format or mp3 file. So using variable new_name which will use os library’s path attribute’s splitext() function which will take the out_path variable as an argument to find the file. Then using the os.rename() function we will convert it to mp3 format.

def picture_only(url):
    video_caller = YouTube(url)
    video = video_caller.streams.filter(only_video=True).first()
    out_path =
    new_name = os.path.splitext(out_path)
    os.rename(out_path,new_name[0] + ".mp4")

Here we have created this function to get the video without audio in it. So all things will be the same as the video_voice_only() function except here will be only_video instead of only_audio.


if __name__ == "__main__":
    required = input("Enter playlist to download playlist; video to download a videonchannel to download all videos from a channelnvoice for only voice filenpicture for picture onlyn")
    if required=="playlist":
        url = input("Enter the url for whole playlistn")
    elif required=="video":
        url = input("Enter the url of the videon")
    elif required=="channel":
        url = input("Enter the url of the channeln")
    elif required == "voice":
        url = input("Enter the url of the videon")
    elif required == "picture":
        url = input("Enter the url of the videon")

After using the __main__ function, we have created the required variable that will take input and guide the user. The thing that the user enters will be processed using if-else statements or if the user input something else, it will be taken as invalid.


Voila! You have now created your own youtube video/playlist/channel downloader with just a few lines of code. Now you can chill while watching youtube videos without any ad.

BONUS: If you want to download youtube videos with the highest resolution you can directly use the following syntax in command prompt or PowerShell or else.

Its syntax is here.

pytube <your_youtube_video_url>
Youtube Video Downloader using Python

Here is the snapshot of our created program.


I am Atulya Khatri, a python geek. I love to learn different programming languages try different libraries and create different programming kinds of stuff.

My other articles are as follows

Gui Calculator using python

Beginners guide to password generator using python

Building an infinite timer using python

Images on this page.

1st: Photo by Alexander Shatov on Unsplash

2nd: Photo by author

Do share this with all your friends who you think need this.

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.

About the Author

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

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