Create Your Own YouTube Video Downloader

Project Description

I was watching a certain course on YouTube and wanted to download the entire playlist so that I could watch it while I would travel via flight. However, downloading a YouTube video would mean I would have to use some third-party application to download the videos one by one. That is when I thought of creating a script that would not only download a certain video, but would also be capable of downloading an entire playlist.

Therefore, in this project, we will be learning how to download a YouTube video using Python. We will also learn how to download an entire playlist using the same Python script. Once you have prepared the script, all you have to do is copy the link of the video or the playlist that you want to download and feed it as input to begin the download. So, without further delay, let’s dive into the steps to complete our project.

Step 1: Install and Import pytube

pytube is a lightweight Python library specifically created to download YouTube videos. Since it is not a built-in Python library, therefore you need to install it using PIP. Open your terminal and type the following command –

pip install pytube

Once you have installed the pytube library, go ahead and import the YouTube module and the Playlist module from it. The YouTube module will allow you to download a single video. In comparison, the Playlist module will allow you to download all the videos in a playlist. You will also need the help of the regex library while downloading an entire playlist. So, make sure that you also import regex.

import re
from pytube import Playlist
from pytube import YouTube

Step 2: Create the Video Downloader Function

def video_downloader():
    url = YouTube(link)
    video = url.streams.get_highest_resolution()
    video.download('./Downloads')

Explanation: The YouTube object allows us to fetch the link from YouTube. To ensure that video is downloaded in the highest possible resolution available use url.streams.get_highest_resolution(). Next use the download() function to download the video file in a specific folder.

Step 3: Create the Playlist Downloader Function

def playlist_downloader(plylst):
    playlist = Playlist(plylst)
    playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
    print(len(playlist.video_urls))
    for url in playlist.video_urls:
        print(url)
        video_downloader(url)
    print("Download Complete!")

Explanation: In this case, you have to use the Playlist Module and create its instance and then use a regular expression as re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") to be able to fetch all the video links present within the playlist. Once you have all the video links, you can use a for loop and send them one by one to the video_downloader function. Thus, each video will get downloaded one by one in the specified Downloads folder. Note that this may take some time depending on the number of videos present within the playlist.

Step 4: Fetch Video/Playlist Link

The user needs to enter the link of the playlist or the video he/she wants to download. Therefore, you need to store this link as a string within a variable. Simply put, you have to accept the URL to be downloaded as a user input.

Depending on whether you want to download a single video or an entire playlist you can create an if-else block that will give the user the option to either download an entire playlist or a single video. The link can be accepted accordingly within the script.

Code:

choice = input("Press 1 if you want to download a video.\nPress 2 if you want to download a playlist.")
if choice == '1':
    link = input("Enter the link of the video or playlist you want to download:")
    video_downloader(link)
elif choice == '2':
    link = input("Enter the link of the video or playlist you want to download:")
    playlist_downloader(link)
else:
    print("Wrong Option Selected!")

Putting it All Together

Now this was quite a bit. So let’s put it all together to visualise how the entire script works –

import re
from pytube import Playlist
from pytube import YouTube

def video_downloader(dwnld):
    url = YouTube(dwnld)
    video = url.streams.get_highest_resolution()
    video.download('./Downloads')


def playlist_downloader(plylst):
    playlist = Playlist(plylst)
    playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
    print(len(playlist.video_urls))
    for url in playlist.video_urls:
        print(url)
        video_downloader(url)
    print("Download Complete!")

choice = input("Press 1 if you want to download a video.\nPress 2 if you want to download a playlist.\nYour Choice: ")
if choice == '1':
    link = input("Enter the link of the video or playlist you want to download:")
    video_downloader(link)
elif choice == '2':
    link = input("Enter the link of the video or playlist you want to download:")
    playlist_downloader(link)
else:
    print("Wrong Option Selected!")

Conclusion

Hurrah! We have successfully created our own YouTube video and playlist downloader and we no longer need the help of any other third-party application or website to download the videos. Isn’t that amazing? With that we come to the end of this project.  I hope this project added some value and helped you in your coding quest. Stay tuned and subscribe for more interesting projects and tutorials.


Google, Facebook, and Amazon engineers are regular expression masters. If you want to become one as well, check out our new book: The Smartest Way to Learn Python Regex (Amazon Kindle/Print, opens in new tab).

Python Regex Course

Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.  

Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages

Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.  Regular expressions ​rule the game ​when text processing ​meets computer science. 

If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet: