A Tale of Python: Easily Download Videos from YouTube

Videos on your computer, in a matter of seconds.

ยท

5 min read

Downloading videos from YouTube can be useful in a variety of scenarios. We may have a slow internet connection or we will be in a place without access to it for a few days. Perhaps we just want to have a copy of the video on our electronic devices in case it is deleted from the online platform, or it is easier for us to show it to a friend without having to watch the ads.

Among the most professional uses stand out having videos to extract information from them, such as audio or subtitles, and then processing that data and using it in some scientific study. Additionally, using Python, the process of downloading videos can be automated, which is very useful for projects that involve data analysis or machine learning.

Whatever your motivation, we will provide you with an easy alternative to download YouTube videos in seconds. You will never want to use external online platforms to do this work for you. We assure you!

However, it is important to note that downloading videos from YouTube is against their terms of service and should only be done with the explicit permission of the copyright holder of the video.

The pytube library

Our main tool for downloading YouTube videos using Python will be the pytube library. This library allows us to easily and efficiently download YouTube videos using a very simple syntax. With pytube, we can easily download videos, audio, and subtitles, and also filter the video based on resolution.

This library is very easy to use and hides the complexity of interacting with the YouTube API, making it a convenient option for developers looking to add video download functionality to their Python projects. Additionally, it supports block downloading of videos and allows downloading videos in different formats such as mp4, mp3, etc.

Building the solution

The first step to building our video downloader is to install the pytube library on our computer. To do this, we run the command pip install pytube in the console of our operating system.

Once the installation is complete, we can create the following Python program, which will download the YouTube video of our choice:

import pytube

video_url = "" # Add the URL of the video to download

yt = pytube.YouTube(video_url)

stream = yt.streams.first()

stream.download()

Let's see how this program works step by step:

  1. First, we import the pytube library into our program. This is done by the line import pytube that appears at the top of the program.

  2. Then, we create the variable video_url which will be where we store the URL of the video that we are going to download. In the above example, the variable has the value "". For the program to work correctly, this value must be replaced by a valid URL.

  3. Once we have the URL of the video stored, we create an instance of the YouTube class that pytube provides. This class takes as an argument the URL of the video that we are going to download.

  4. Then we make use of the streams property to get a list of the different formats and qualities that the video in question has. In this way, we can select the version that we need before downloading the video. In our case, we are choosing the first available option, without having anything else into consideration.

  5. Finally, we call the download method that the stream property gives us to start the download.

Once this program finishes running, the video will be downloaded in the same directory as the Python file. If we want to specify the location where to download the video, we can modify the call to the download function, passing as an argument a directory on our computer.

Let's see a version of the previous code where it will be more comfortable to specify the URL of the video to download and the directory where to store it, once downloaded:

import pytube

def download_video(video_url, save_location):
    yt = pytube.YouTube(video_url)
    stream = yt.streams.first()
    stream.download(save_location)
    print(f'{yt.title} was downloaded at {save_location}')

def main():
    video_url = "" # Add the URL of the video to download
    save_location = r"" # Add the download directory
    download_video(video_url, save_location)

if __name__ == "__main__":
    main()

Once again, to make this example work correctly, we must replace the values assigned to the variables video_url and save_location with the URL of the YouTube video and the directory where to download the video, respectively. This allows decoupling the download directory from the directory where our Python program is located.

Conclusions

In this article, we have seen how to download videos from YouTube using the programming language Python.

Specifically, we used the pytube library which allows creating instances of classes representing a YouTube video and accessing different versions of it, taking into account the resolution, among other factors. Additionally, we saw how to download the video in the version we want and how to specify the directory where to store it on our computer.

So next time you want to have a YouTube video in your possession, don't run to another online platform to do the job for you. Create your program in Python, and specify the video you want and where you want to store it. In a matter of seconds, you will have your copy of the video in the quality you want, without depending on third parties.

All this and more is possible in the world of Programming!

Note: This is the English translation of one of the articles published in another project that I have been collaborating on lately. It is an academy to teach programming topics using Python but taught in the Spanish language:

We felt like some of the articles published in Spanish should also be available for our English-speaking audience. So, stay tuned for more programming articles like this one. See you soon!


๐Ÿ‘‹ Hello, I'm Alberto, Software Developer at doWhile, Competitive Programmer, Teacher, and Fitness Enthusiast.

๐Ÿงก If you liked this article, consider sharing it.

๐Ÿ”— All links | Twitter | LinkedIn

Did you find this article valuable?

Support Alberto Gonzalez by becoming a sponsor. Any amount is appreciated!

ย