Because it’s cool that’s why!
Oh and Youtube doesn’t let people download their videos directly from their website.
And you will get hands on experience with Python. Good enough?
Alright, I will assume you are total beginner and know nothing about Python. This post will walk you through how to download YouTubes videos using Python by example.
Watch the Video Tutorial
First, install Python on your computer (if not already installed)
If you don’t already have the latest Python version 3 installed on your PC, go to the Python website and Download the latest release for your OS (click the download button).
Run the installer and follow the on-screen instructions. If you’re on a Windows 10 PC make sure to choose the option to add Python to your path (on the first screen of the installer). See here for Windows install instructions.
The installer should also by default install pip, the python package manager. You want this and we’ll use it later.
Then, import the Pytube Package
Now that you have pip installed, you will want to run the following command. Open up your Powershell or command prompt app and run it like this:
pip install pytube
Now run the pip list command to see that pip has installed the pytube module (pytube should appear in the returned list of Packages)
Next, Import the Certifi Package
For me I had to install the certifi package to get YouTube videos to download. Without that Python may complain about an SSL related problem.
Install the certifi package by running this command:
pip install certifi
If you’re on a Mac, you may need to run the following command to tell your Mac’s Python install to use the certifi module. Do that by typing the below command and hit enter. Note you may need to adjust 3.9 to whatever version you have:
/Applications/Python\ 3.9/Install\ Certificates.command
Finally, let’s do some coding!
Open up your favorite text editor. I like to use Visual Studio Code. You can use whatever you like to edit.
Let’s create a file p.py
Then add these five lines to your file and save:
from pytube import YouTube link = input("Enter video URL: ") video = YouTube(link) stream = video.streams.get_highest_resolution() stream.download()
Here’s what I’ve got in Visual Studio Code (below image).
Notice I have the Terminal (e.g. Powershell) open on the bottom. We’ll run our Python program using the Terminal in just a minute.
To open the Terminal, hit Ctrl+~ and then select from the dropdown and select powershell if not already selected. You can also just open up your Powershell from Windows search and cd to the directory where your Python program is located.
What the code is doing
Real quick, here is what’s going on
- The first line is importing the YouTube package into your Python program.
- Second line is asking for a YouTube URL and saving the entered input to a variable called “link”. It will ask from the command line since that is where we will run it.
- Third line is passing the link into a YouTube object and saving that object as the “video” variable.
- Fourth line is having the video variable (object) set itself up to download the highest resolution video available on YouTube for the video URL you specified on line 2.
- Fifth line is executing the download from YouTube to your computer.
Now run your code
Run your code by typing this command into the Terminal (in the directory where your Python code is) and hit enter:
Now your python program is asking for a YouTube video URL
I’ll use this one of Yoda vs the Emperor: https://www.youtube.com/watch?v=iFUs7E8liZU
And paste it into the Terminal then hit enter.
It may take a minute (the video is ~9 MB) but your Python program should then download the video from YouTube to your PC in the directory where you ran your Python program.
See below screenshot of the downloaded video on the right.
Awesome, now we can download YouTube videos to our computer using Python! 🙂
If you have problems running your code
I found after I wrote this blog post and as I was making a video on the subject, my application would produce errors and would not download youtube videos anymore. If you run into this too, you may need to update your pytube package like I did. Just do this:
pip install --upgrade pytube
Also, don’t forget to install the certifi package as described above.
Bonus: Download YouTube videos in just TWO lines of code!
Let’s now convert our five lines of code from above into just two lines of code so we can do everything we did in just fewer lines. Why? Because it’s cool that’s why 🙂
from pytube import YouTube YouTube(input("Enter video URL: ")).streams.get_highest_resolution().download()
That first line we still need to import the YouTube object and the second line combines everything we need to take user input (the youtube video URL), figure out which is the highest resolution video from that URL and then download it. Beautiful.
Download Videos using the Pytube CLI
Since you’ve installed the pytube package you can also run it from the Terminal like this:
In this case you get Luke fighting robots!
Mike is the creator of Go With Code and a coder at heart 🙂