Download Youtube Videos Using Python Pytube

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 any further explaination of Youtube needed, as we all are familiar with it.

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

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

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

Some of the features of Pytube package:

  • Pytube has no any 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 url, generating captions regarding language specified, and many more.

Installation of Pytube

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

$ pip install pytube

Note: pytube requires an installation of python version >= 3.6 in 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 YouTube class object called ‘yt’

yt = YouTube(video_url)

Great now we have pass the video url into 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 video

>>> yt.views
1626589807

Obtain length of video

>>> yt.length
262

262 seconds of video that 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 video

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

So far we have obtain 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 about 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 found 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 download the stream separately and merged them using any other software.

2. Progressive streaming

Those type of streaming that can contains both audio and video codec but only supported upto resolutions 720p.

For the understanding purpose, we only deal with progressive stream 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 Progressive stream, you have to pass progressive=True as 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 upto 720p. It is because this video does not have resolutions of 720p in progressive streams but it supports those resoultions 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 custom path and filename of video, you can do as following:

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

Conclusion

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

Happy coding:-)

Reference

Official Pytube Document

Leave a Reply