Download Youtube videos with Python
Tags: python, youtube, download
Published: 2021-01-11 18:53:48


<img src="https://dev-to-uploads.s3.amazonaws.com/i/vm79a37j6ql8gnpi556g.png" width="200" height="20" alt="Python Logo"/>              |  <img src="https://dev-to-uploads.s3.amazonaws.com/i/63h7fmm927m1nv6w6aa5.png" width="200" height="20" alt="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](https://www.python.org/downloads/))

Open the CMD and type,
```python
pip install pytube
``` 

After completing the installation, you can work with Pytube.

First open the Python shell with the command below. 

```python
python
```
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.

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

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

<hr>

Below script will show all the available streams of the video. The streams with <mark>progressive=True</mark> 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. 

```python
>>> from pytube import YouTube
>>> yt = YouTube('https://www.youtube.com/watch?v=kJQP7kiw5Fk')
>>> yt.streams
``` 
As a result you will get this.

```python
 [<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">]
``` 

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

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

```python
>>> yt.get_by_itag(22).download()
``` 
After completing the process you can see the downloaded video in your directory.

<hr>

###**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.

```python
>>> 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()
``` 

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.*

<hr>

Pytube is an open-source library and you can find it on [github](https://github.com/pytube/pytube).
{% github pytube/pytube no-readme %}

Enjoy with Pytube.

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

Thank you.