DEV Community

Cover image for Download Youtube videos with Python
Dumidu Kasun Bandara Rajakaruna
Dumidu Kasun Bandara Rajakaruna

Posted on

Download Youtube videos with Python

Python Logo Python Logo

What is pytube?

Pytube is a lightweight, dependency-free Python library which is used for downloading videos from the web.
Pytube is not the native library. You need to install it before using it.

Note: Before installing Pytube You need to have Python Installed on your PC (Guide to Install Python)

Open the CMD and type,

pip install pytube
Enter fullscreen mode Exit fullscreen mode

After completing the installation, you can work with Pytube.

First open the Python shell with the command below.

python
Enter fullscreen mode Exit fullscreen mode

Then using the Python shell you can use Pytube to download youtube videos.

The Following is a simple Python script to download a youtube video.

>>> from pytube import YouTube
>>> yt = YouTube('https://www.youtube.com/watch?v=kJQP7kiw5Fk')
>>> yt.streams.get_highest_resolution().download()
Enter fullscreen mode Exit fullscreen mode

The above commands will simply download the video that belongs to the URL "https://www.youtube.com/watch?v=kJQP7kiw5Fk" to your current directory.


Below script will show all the available streams of the video. The streams with progressive=True are the videos that include both audio and video in the same stream. Others don't have both audio and video in the same stream.

>>> from pytube import YouTube
>>> yt = YouTube('https://www.youtube.com/watch?v=kJQP7kiw5Fk')
>>> yt.streams
Enter fullscreen mode Exit fullscreen mode

As a result you will get this.

 [<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">,
 <Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">,
 <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028" progressive="False" type="video">,
 <Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="399" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.08M.08" progressive="False" type="video">,
 <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f" progressive="False" type="video">,
 <Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="398" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.05M.08" progressive="False" type="video">,
 <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">,
 <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="397" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.04M.08" progressive="False" type="video">,
 <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">,
 <Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="396" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.01M.08" progressive="False" type="video">,
 <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015" progressive="False" type="video">,
 <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="395" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.00M.08" progressive="False" type="video">,
 <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c" progressive="False" type="video">,
 <Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="394" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.00M.08" progressive="False" type="video">,
 <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2" progressive="False" type="audio">,
 <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus" progressive="False" type="audio">,
 <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus" progressive="False" type="audio">,
 <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus" progressive="False" type="audio">]
Enter fullscreen mode Exit fullscreen mode

Then you can select a stream you want to download and get the itag of it. Note that You should select one with progressive="True" with a desired resolution.

Next by using the command below you can download the corresponding stream belongs to the itag that you have selected.

>>> yt.get_by_itag(22).download()
Enter fullscreen mode Exit fullscreen mode

After completing the process you can see the downloaded video in your directory.


Downloading a Playlist from Youtube

Let's think that we want to download an entire playlist in the youtube. It is soo simple with Pytube.

First you need to get the URL of the playlist.
Then use the below commands to download the entire playlist.

>>> from pytube import Playlist
>>> pl = Playlist("https://www.youtube.com/watch?v=Edpy1szoG80&list=PL153hDY-y1E00uQtCVCVC8xJ25TYX8yPU")
>>> for video in pl.videos:
>>>     video.streams.get_highest_resolution().download()
Enter fullscreen mode Exit fullscreen mode

The above command will download all the videos in the playlist with the best resolution available.
Note that the indentations are crucial when using Python.


Pytube is an open-source library and you can find it on github.

GitHub logo pytube / pytube

A lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

Enjoy with Pytube.

Hope to bring a new post which shows how to use Pytube from your mobile phone.

Thank you.

Top comments (1)

Collapse
 
sachitha2 profile image
Sachitha Hirushan

Very Interesting <3