Downloading YouTube videos to your local storage is often an uphill battle, especially when dedicated YouTube downloaders keep failing you. But you can make a reliable YouTube video downloader using Python.

No worries if you're not familiar with Python programming, we'll provide you with what you need to get started. It's easy, and once you have everything set up, you may not need to reinvent the wheel for subsequent downloads.

Let's get to it.

Set Up Python

To get started, you need to get Python up and running on your PC. Don't bother if you're using Mac, as it has Python pre-installed already.

But if you're on Windows, go to python.org to download and install the latest version of Python on your PC.

To test if Python is working on your PC after installation, open up your terminal and type:

        python --version
    

Then hit Enter. If your terminal displays the Python version you downloaded earlier, then you've successfully installed Python on your PC.

Next, create a folder for your project. Open up the command line to that directory and create a new Python file to the same location. Ensure that your Python file has the .py file extension.

Create a virtual environment and then open up any text editor you like to that location.

Related: Create a Virtual Environment in Python

Note: Only download videos when you have the proper authorization to do so. See Is It Legal to Download YouTube Videos? for more information.

Create Your YouTube Downloader With Python

To kick off this tutorial, you need to install a Python YouTube utility library called pytube using pip.

To do that, enter the following command in your terminal:

        pip install pytube
    

Once you install pytube, go back into your text editor, open your Python file and import pytube:

        from pytube import YouTube
    

Go to YouTube and copy the URL of the video you wish to download. Then create a YouTube instance on the next line of your Python file:

        URL = "Enter video URL"
video = YouTube(URL)

The pytube module works by giving you different stream options. A video, however, has different stream resolutions. So pytube lets you download your video based on those.

Once you instantiate a YouTube object with the URL of the video, you can print the streams available for it:

        video_streams = video.streams
print(video_streams)

You can run your Python code via the command line by calling your Python file like this:

        python file_name.py
    

Replace file_name with the name of your Python file.

The output looks like this:

Video streams output pytube

You can also specify streams by including the file extension type using the filter function:

        video_streams = video.streams.filter(file_extension='mp4')
print(video_streams)

And that looks like this:

video steams output

The module, however, returns different stream resolutions, starting with 360p to 720p and 1080p (and maybe more). But when you look closely, each resolution has an itag value.

For instance, res="720" has itag="22", while the itag at 360p resolution is 18.

You can call a stream using this itag value by including the get_by_itag() function:

        video_streams = video.streams.filter(file_extension='mp4').get_by_itag(22)
print(video_streams)

<strong>Output:</strong> <Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">

The resolution of the above stream is 720p (res="720p"). You can try the itag value for 360p to get a lower resolution. You can also increase the resolution to 1080p or any other available one if you want. All you need is the itag value for your preferred resolution, which is always available when your print the streams for any video.

To check the title of a video:

        video = YouTube(URL)
video_streams = video.streams.filter(file_extension='mp4').get_by_itag(22)
print(video_streams.title)

<strong>Output:</strong> Achilles Vs. Hector - TROY (2004)

Now here's how to download a video at 720p resolution:

        video = YouTube(URL)
video_streams = video.streams.filter(file_extension ='mp4').get_by_itag(22)
video_streams.download()

The video, however, downloads to your current working directory in this case. It also inherits the default title from YouTube.

But you can specify a download directory for your video and change the file name:

        video = YouTube(URL)
video_streams = video.streams.filter(file_extension = 'mp4').get_by_itag(22)
video_streams.download(filename = "my first YouTube download2",
output_path = "video_path")

Remember to replace video_path with your preferred download directory.

Now let's put the entire code together in one place. But this time, changing the resolution to 360p:

        from pytube import YouTube
URL = "Enter video URL"
video = YouTube(URL)
video_streams = video.streams.filter(file_extension='mp4').get_by_itag(18)

video_streams.download(filename = "my first YouTube download2",
output_path = "video_path")

That's it! You just made a DIY YouTube video downloader with Python.

You can confirm your video resolution by right-clicking the video and then going to Properties > Details. Under Video, check the value of the Frame height, this indicates the video resolution.

Keep Automating Tasks With Python

Python is versatile, and using it to automate simple tasks on your PC improves your productivity. If you know a little bit of it, the ability to self-code your own YouTube video downloader is one of the dividends you receive.

That said, you can also automate excel calculations, make a calculator, customize your bash, and do more with Python programming.