5 min read

You heard many times that python is capable of downloading videos from Youtube. Yes, it is true, Python can do this.

source: memes.com

Here, I will explaining you how to download videos using python. We will be using Pytube library for this purpose.


pytube is a very serious, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos. I liked this library because it is very simple to use and flexible, that makes my work easier.

If I have to explain in single line then it will be,โ€Behold, a perfect balance of simplicity versus flexibilityโ€œ.Finally pytube also includes a command-line utility, allowing you to quickly download videos right from terminal. This feature helps a lot for a user like me, who works linux operating system.


  • Support for Both Progressive & DASH Streams
  • Support for downloading complete playlist
  • Easily Register on_download_progress & on_download_complete callbacks
  • Command-line Interfaced Included
  • Caption Track Support
  • Outputs Caption Tracks to .srt format (SubRip Subtitle)
  • Ability to Capture Thumbnail URL.
  • Extensively Documented Source Code
  • No Third-Party Dependencies

Without wasting any time let get started,

Pytube Installation

Run the following command into the terminal, so that you python interpreter become able to recognize import python

pip install pytube

Finally environment has been setup ๐Ÿ™‚

source: memecrunch.com
>>> from pytube import YouTube
>>> YouTube('https://www.youtube.com/watch?v=ZiKMIuYidY0').streams.first().download()

This code will download the video with its highest video quality present on the Youtube.

Now the question arises,How I will get to know about how many types of streams available on the internet???????????????????????

It has simple answer, you donโ€™t have to check manually when python is their to help you. object_name.streams.all() will help you to do this work. Required code is below to give you an idea.

>>> freshlybuilt = YouTube('https://www.youtube.com/watch?v=ZiKMIuYidY0')
>>> freshlybuilt.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.4d401e">,
 <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9">,
 <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="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
 <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9">,
 <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="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">]

You may notice that some streams listed have both a video codec and audio codec, while others have just video or just audio, this is a result of YouTube supporting a streaming technique called Dynamic Adaptive Streaming over HTTP (DASH). You can research more about it, you will find it interesting.

You can download video by simple selecting the tag of its, there are other methods also but I will prefer you to use below given method.

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

Now question arises, how to save it to specified folder????????


You can use above code to save the file into specified folder.

Here is the final code.

from pytube import YouTube
freshlybuilt = YouTube('https://www.youtube.com/watch?v=ZiKMIuYidY0')

In the next article, we will read how to download Whole playlist from the given link in single run and only audio from the given video.


Vishal Sharma

Currently pursuing a Computer Science degree from G.B Pant Government Engineering college, New Delhi. I am from freshybuilt team.


Leave a Reply