Python library PyTube to download youtube videos

PythonServer Side ProgrammingProgramming

You know “youtube” right? Yes that most famous video sharing website especially in india . Most of the time, you like some videos and you try to download that video so as to check it later/offline. Then you come across “youtube-downloader” app to download youtube videos from the youtube website. But most of the apps comes with some restriction (if you are using it for free) or cost you money. But have you ever think of creating our own program to download youtube videos? If not you, then you should try as its very simply to do using the python library. Python provides “pytube” library to download videos from the youtube. This library allows us to download videos from the web.

Pytube is not a standard library, so we need to install it. With pip, its easy to install −

pip install pytube
Collecting pytube
Installing collected packages: pytube
Successfully installed pytube-9.3.5

Downloading a video

As we see down the article, downloading a youtube video using pytube is very easy.

So let’s start by importing the youtube class:

from pytube import YouTube

Now let’s try to get a link of the video. For example, lets select a video of your choice −

yt = YouTube('')

The pytube API makes all information intuitive to access. For example, this is how you would get the video’s title:

>>> yt.title
'Redmi Note 7 Fake 48MP Camera? Explained \U0001f525\U0001f525\U0001f525'

And to get the thumbnail url −

>>> yt.thumbnail_url

Now, we need to select the media format. Pytube module provides following media formats to download video −

>>> yt.streams.all()
[<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">, <Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">, <Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">, <Stream: itag="36" mime_type="video/3gpp" res="240p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">, <Stream: itag="17" mime_type="video/3gpp" res="144p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">, <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">, <Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9">, <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">, <Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9">, <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401f">, <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9">, <Stream: itag="397" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.05M.08">, <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">, <Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9">, <Stream: itag="396" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.05M.08">, <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d400d">, <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9">, <Stream: itag="395" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.05M.08">, <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">, <Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9">, <Stream: itag="394" mime_type="video/mp4" res="None" fps="30fps" vcodec="av01.0.05M.08">, <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">, <Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">, <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus">, <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus">, <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus">]

Let’s say we want to get the first stream:

>>> stream = yt.streams.first()
>>> stream
<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">

The video will be downloaded into your destination path −

'f:/Redmi Note 7 Fake 48MP Camera Explained \U0001f525\U0001f525\U0001f525.mp4'

Or else you can download the video into the current working directory −

'C:\Python\Python361\Redmi Note 7 Fake 48MP Camera Explained \U0001f525\U0001f525\U0001f525.mp4'

Now we see the video is downloaded in our destination path:

Updated on 30-Jul-2019 22:30:24