I know it is difficult for everyone to have an Internet Download manager or other third-party software to download a video from YouTube. But, if you are a programmer and know Python, then by running just a few lines of code, you can easily download a video from YouTube.
Python is one of the best and the most accessible programming languages for developing one's career. Generally, Python has hundreds of libraries to make working with it more comfortable, and pytube is a dependency-free Python library which is lightweight.
Pytube library in the Python programming language makes downloading the video from You Tube very easy, and it does not depend on any other Python library, so there is no need to install any additional libraries. Pytube is available in most of the Python versions (2.7 and higher).
To install the pytube library, type the following command in the terminal of your system.
pip install pytube
After installing the pytube library successfully, we need to import the YouTube module from the pytube library. The YouTube library is going to act as an Object in our program, and it provides many properties and methods like Title, rating, length, etc. We need to remember that in the 'YouTube' keyword, we must use 'Y' and 'T' as capital letters. If we don't use this case sensitive phenomena, we will end up with a bunch of errors in the program.
Use the following command to import the "YouTube" Module from the pytube library.
from pytube import YouTube
Here, I am using the VS Code to run the Python program, and the video will directly save in the path provided.
- import pytube
- from pytube import YouTube
- video_url = 'https://www.youtube.com/watch?v=5Z0rJfT0jdQ&t=5s'
- youtube = pytube.YouTube(video_url)
- video = youtube.streams.first()
- video.download('C:/Users/vkyku/Desktop/')
In this program, we are using YouTube keyword as the Object, and an object has properties and methods in any programming language.
The properties of the YouTube object are,
- Title
- Video Id
- Description
- Length
- Thumbnail_URL
- Views
- Rating
- Age Restricted
Title
YouTube returns the Title of the youtube video.
Video ID
The property video_id returns the ID of the youtube video.
Description
In every youtube video, we can see a description under the video about the video, company and a few hyperlinks about their blogs/websites, and third party websites. Using the description property, it returns whatever description is present below the video.
Length
The property length in the YouTube object will return the total length (Duration) of the youtube video in seconds.
Thumbnail URL
Thumbnail is an image which portrays the video while opening the youtube website. We can see N number of videos with their thumbnails in the youtube. Using thumbnail_url property in the YouTube object, it returns the URL of the thumbnail image.
Views
Every youtube video will have particular views, which means the number of times users watched the video then the count of views will automatically increase.
Rating
The rating property in the Object YouTube will return the average rating of the You Tube video.
Age Restricted
Generally, most YouTubers are uploading videos users who are 18 and above. To know whether the particular video is age-restricted or not, we can use the age_restricted property which returns true or false.
If the output is True, it means that the video is age-restricted and False means, not age-restricted.
- from pytube import YouTube
- video = YouTube('https://www.youtube.com/watch?v=5Z0rJfT0jdQ&t=5s')
- print(video.title)
- print(video.video_id)
- print(video.description)
- print(video.length)
- print(video.thumbnail_url)
- print(video.views)
- print(video.rating)
- print(video.age_restricted)
A YouTube video will consist of various sizes and formats. The YouTube object opens numerous streams from the YouTube video URL. Using the following program, we can get all the streams of the You Tube video.
- from pytube import YouTube
- video = YouTube('https://www.youtube.com/watch?v=5Z0rJfT0jdQ&t=5s')
- video.streams.all()
- stream = video.streams.all()
- for i in stream:
- print(i)