download-youtube-videos-using-python-pytube

Introduction

Youtube is the online video-sharing platform owned by Google. According to stats, Youtube videos were watched one billion hours time each day. There is no further explanation of Youtube needed, as we all are familiar with it.

Come to the craziness of people towards Youtube which is only available when there is an internet connection(except for Youtube offline downloads), we can use the simple python script to download the Youtube videos.

Pytube is one of the great open-source projects in python which can be used to download Youtube videos directly with a few lines of code that provides robust features.

Pytube is a lightweight library that is written completely in Python.

Some of the features of the Pytube package:

  • Pytube has no third-party dependencies and it is highly reliable
  • Videos can be downloaded using Command Line Interface(CLI) too
  • Pytube supports downloading complete playlists
  • Also support features like capturing thumbnail URLs, generating captions regarding language specified, and many more.

 

Installation of Pytube

To install the pytube package, you have to run the following command in your terminal:

$ pip install pytube

Note: pytube requires an installation of python version >= 3.6 on your pc.

First import the YouTube class from pytube

>>> from pytube import YouTube

Take the youtube video URL as input

>>> video_url = input()
https://www.youtube.com/watch?v=nYh-n7EOtMA

Create a YouTube class object called ‘yt’

yt = YouTube(video_url)

Great now we have passed the video URL into the YouTube class and created object yt. Now we can access the information about the video such as title, ratings, views, description, and other useful data.

 

Obtain title

>>> yt.title 
'Sia - Cheap Thrills (Lyric Video) ft. Sean Paul'

 

Obtain total views of the video

>>> yt.views
1626589807

 

Obtain the length of the video

>>> yt.length
262

262 seconds of video which means about 4 minutes and 22 seconds

 

Obtain author

>>> yt.author
'SiaVEVO'

 

Obtain description

>>> yt.description
'"Cheap Thrills" by Sia feat. Sean Paul\nListen to Sia:
.......
.......

 

Obtain ratings

>>> yt.rating
4.8518591

 

Obtain thumbnail URL

>>> yt.thumbnail_url
'https://i.ytimg.com/vi/nYh-n7EOtMA/maxresdefault.jpg'

 

Obtain publish date of the video

>>> yt.publish_date   
datetime.datetime(2016, 2, 10, 0, 0)

So far we have obtained the information about the video and now in the next step, we are heading towards downloading the video.

 

First, we have to know the concept of streams. As you notice while watching youtube videos there you can select the resolutions of videos. The streams can be divided into two parts:

  1. DASH streaming
  2. Progressive streaming

 

1. DASH streaming

Dynamic Adaptive Streaming over HTTP(DASH) is the streaming technique that Youtube uses for high-quality video rendering above resolutions 720p. In DASH, we can find that some of the streams come with both video codec and audio codec whereas other streams come with video and audio codec separately. In this case, we have to download the stream separately and merged them using any other software.

 

2. Progressive streaming

Those types of streaming can contain both audio and video codecs but only supported up to resolutions 720p.

For the understanding purpose, we only deal with progressive streams in this tutorial.

Check out different streams

>>> yt.streams
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="24fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">
<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="24fps" vcodec="avc1.640028" progressive="False" type="video">
<Stream: itag="248" mime_type="video/webm" res="1080p" fps="24fps" vcodec="vp9" progressive="False" type="video">

 

For choosing the Progressive stream, you have to pass progressive=True as an argument inside the filter function as:

>>> yt.streams.filter(progressive=True).get_highest_resolution()
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="24fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">

Above you can notice that in progressive stream highest resolutions of 360p as it supports up to 720p. It is because this video does not have resolutions of 720p in progressive streams but it supports those resolutions in DASH streaming. You can check out in above outputs in your terminal.

 

Now, we can download the video as:

>>> ys.download()
'C:\\Users\\SHIV\\Sia - Cheap Thrills (Lyric Video) ft Sean Paul.mp4'

We did it, Hurray!! Check out your working directory and play the video.

 

If you want to provide a custom path and filename of the video, you can do as following:

>>> ys.download(output_path='D:\\', filename="Sia Song") 
'D:\\Sia Song.mp4'

 

Conclusion

Hence, we successfully downloaded youtube videos using the pytube python library. We explored the different streaming methods and extracted information such as the title, length, views, ratings, etc. of a particular video. If you come to have any problems then feel free to comment your queries below.

Happy coding:-)

Reference

Official Pytube Document

Leave a Reply

Your email address will not be published.