The Simplest Method To Download You Tube Videos Using Python - Pytube

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.
  1. import pytube  
  2. from pytube import YouTube  
  3. video_url = 'https://www.youtube.com/watch?v=5Z0rJfT0jdQ&t=5s'   
  4. youtube = pytube.YouTube(video_url)  
  5. video = youtube.streams.first()  
  6. video.download('C:/Users/vkyku/Desktop/')   
The Simplest Method To Download Youtube Videos Using Python || Pytube
 
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,
  1. Title
  2. Video Id
  3. Description
  4. Length
  5. Thumbnail_URL
  6. Views
  7. Rating
  8. 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.
  1. from pytube import YouTube  
  2. video = YouTube('https://www.youtube.com/watch?v=5Z0rJfT0jdQ&t=5s')  
  3. print(video.title)  
  4. print(video.video_id)  
  5. print(video.description)  
  6. print(video.length)  
  7. print(video.thumbnail_url)  
  8. print(video.views)  
  9. print(video.rating)  
  10. print(video.age_restricted)   
The Simplest Method To Download Youtube Videos Using Python || Pytube
 
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.
  1. from pytube import YouTube  
  2. video = YouTube('https://www.youtube.com/watch?v=5Z0rJfT0jdQ&t=5s')  
  3. video.streams.all()  
  4. stream = video.streams.all()  
  5. for i in stream:  
  6. print(i)   
The Simplest Method To Download Youtube Videos Using Python || Pytube

Next Recommended Reading Simple Stopwatch Using Python