YouTube is a free video-sharing website that can be accessed from PCs, tablets, mobile phones, and TVs. There are different products on YouTube like YouTube Go, YouTube Kids, YouTube Premium, and YouTube TV. It is the most popular video website with 100 hours of videos uploaded every minute. This means that you will always find something new to watch on YouTube.
Unfortunately, YouTube videos are accessed when a person is online. You will need to have an internet connection to watch these videos. But some people may not have an internet connection or would maybe want to share the video with a person who has no access to YouTube. This would mean that they have to download the video. YouTube offers an inbuilt download option for videos to watch later but cannot be shared as videos but as links.
The inbuilt Dowload feature is not so effective when it comes to sharing the videos. Something else is that If I save a video on Youtube with my account on the phone, I would not be abe to access it on my Laptop from the site on the browser. Several software programs have been developed to fill the gap and offer means to download and share videos without using the link only. An example is Pytube.
What is Pytube
Pytube is a lightweight, genuine, Python-based, dependency-free utility for downloading YouTube videos. Its advantages are that:
- It does not require Third-party applications to function.
- It is able to capture Thumbnail URL.
- It can also easily register Callbacks for complete and in-progress downloads.
- Has an extensively documented source code.
- This guide will show you how to install Pytube on Linux and Windows.
How to install Pytube on Linux
Let’s consider installation on the following Linux distributions.
Install Pytube on Debian/Ubuntu-based systems
Update your system packages using the following command.
sudo apt update
Install required packages for the installation.
sudo apt install python3-pip
Install Pytube using Pipy with the following command.
$ sudo pip install pytube
Collecting pytube
Downloading pytube-12.0.0-py3-none-any.whl (56 kB)
|████████████████████████████████| 56 kB 4.8 MB/s
Installing collected packages: pytube
Successfully installed pytube-12.0.0
Install Pytube on RHEL-based systems
Update your system packages
sudo dnf update -y
Install Python and Pip on your system
sudo dnf install python3-pip
Check for the version to confirm the installation
$ pip3 --version
pip 21.2.3 from /usr/lib/python3.9/site-packages/pip (python 3.9)
Install Pytube using pip with the following command.
$ sudo pip3 install pytube
Collecting pytube
Downloading pytube-12.1.0-py3-none-any.whl (56 kB)
|████████████████████████████████| 56 kB 3.3 MB/s
Installing collected packages: pytube
Successfully installed pytube-12.1.0
Install on Arch Linux
Install python and pip with Pacan using the following command
sudo pacman -S python-pip
Then install Pytube with the following command
pip install pytube
Install on SUSE-based systems
Download get-pip.py with the following command.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Then install pip.
python get-pip.py
Then install Pytube with pip using the following command.
pip install pytube
Installing Pytube on Windows
To install Pytube on Windows, you should have Python installed and Pip. This can easily be done from the Microsoft Store. Alternatively, You can follow the guide on How to Install Python 3.10 on Windows 10 / Windows 11.
If you installed Python from the Microsoft store, You will have to find the path where Python Scripts are located and copy it on the Command prompt to change to that directory.
cd C:\Users\Admin\AppData\Local\Programs\Python\Python310\Scripts
Then install Pytube using pip in the directory where the Python scripts are located with the following command
$ pip install pytube
Collecting pytube
Downloading pytube-12.0.0-py3-none-any.whl (56 kB)
---------------------------------------- 56.5/56.5 KB 228.0 kB/s eta 0:00:00
Installing collected packages: pytube
Successfully installed pytube-12.0.0
Download a Youtube Video using Pytube
Using the Command Line Interface (CLI)
Downloading videos from the CLI is straightforward. Use the pytube command together with the YouTube video link.
pytube 'https://www.youtube.com/watch?v=13FLawVtwSc'
You can download playlists also using the same syntax as a single Youtube video.
$ pytube https://www.youtube.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n
Using Python Scripts
Login to the Python shell using the following command in the respective operating systems.
### Linux ###
$ python3
### Windows ###
python3.10
To download a YouTube video, First, import the YouTube class using the following command.
>>> from pytube import YouTube
Then pass the argument of the Youtube video URL you want to download as a youtube object using the command below.
>>> YouTube("https://www.youtube.com/watch?v=13FLawVtwSc").streams.first().download()
Get Video Details
We can create a YouTube object from the youtube URL and give it a name. I will name it NewVideo.
>>> NewVideo = YouTube("https://www.youtube.com/watch?v=13FLawVtwSc")
The Youtube object has different properties that can be used to get more information about the video like title, length, and thumbnail URL.
a. Title
To get the title of the video, use the following command.
>>> print(NewVideo.title)
Bean ARRESTED | Bean Movie | Funny Clips | Mr Bean Official
This will give the name of the video.
b. Length
To get the length of the video, use the following command.
>>> print(NewVideo.length)
610
This gives the duration of the video in seconds.
c. Thumbnail URL
To get the thumbnail URL of the video, use the following command.
>>> print(NewVideo.thumbnail_url)
https://i.ytimg.com/vi/13FLawVtwSc/sddefault.jpg
Thumbnail is a short descriptive image of the video.
d. Description
To get the video description, use the following command.
>>> print(NewVideo.description)
Mr Bean finds himself in a rather sticky situation at the airport!
Welcome to the official Mr Bean Channel. Here you will find all of your favourite Mr Bean moments from the classic series with Rowan Atkinson and his new animated adventures. Make sure to subscribe and never miss a Full Episode of Mr Bean, or Mr Bean Compilations and clips as well as originals including Mr Bean Comics.
This is a short explanation of the content of the video found below the video.
e. Views
To get the number of views accumulated, use the following command.
>>> print(NewVideo.views)
26830772
These are the number of views the video has amassed.
f. Rating
To get the rating of the video, use the following command.
>>> print(NewVideo.rating)
None
This shows the rating of the video.
g. Video ID
To get the Video ID, use the following command.
>>> print(NewVideo.video_id)
13FLawVtwSc
This will show the video id at the end of the YouTube video URL.
If you want to download the video with the youtube object you would use the following code
NewVideo.streams.first().download()
Conclusion
From this guide, we have installed Pytube on Linux and Windows. We have also learned how to download youtube videos and playlists using Pytube on both operating systems. The advantage of Pytube is that it is easy to download single Youtube videos and also multiple videos from playlists.