DEV Community

Cover image for Interesting Python Module PyTube
Atharva Shirdhankar
Atharva Shirdhankar

Posted on • Updated on

Interesting Python Module PyTube

Hello Everyone ,
In this Interesting Python Module series I will be discussing about interesting python module you must know.
And it will be actually a fun way to learn python by making some micro projects using some interesting python module. So lets started...

In this blog I will be discussing about PyTube. Its an awesome python module which helps us download youtube video!!!

I know ,I know ,you will be surprised by reading this, but yes this module help us download youtube video.

Problem Statement :

As we know youtube video can be downloaded and can be watched offline on mobile(iOS and Android) but on desktop its not possible . Using this python pytube module we can download youtube video on desktop.

So not just waste time and learn how we can write python code to download youtube video.

Before starting with the code part we must have python 2.7 or 3 version installed.

Note :
When you are installing python do check the box which says something like "pip" we will required it for installing pytube module . If we dont do that we will get error during installation of pytube module.

And after python installed properly one more thing need to be installed and that is Pytube Module.
So just open command prompt and type this pip command which will install our module.

pip install pytube
Enter fullscreen mode Exit fullscreen mode

We will get output for the pip command something like this

pip install output

And I have also created one folder which has initially one python file and in this folder I m going to download youtube video.
Image description

Now lets open ide and understand how code will work(I'm using the IDE which comes with python itself)

Code :

# importing the pytube module
from pytube import YouTube


# link of the video to be downloaded
link="https://youtu.be/z-vLtsKvp_4"

yt = YouTube(link)

# getting title of the video
head= yt.title
print(head)

#the streams is pytube class which help filter the content and download just audio or video or both

dl = yt.streams.get_by_itag(22)
#itag is the value given to different video quality stuff.
# For example here itag value 22 means video quality is 720p and we download both audio as well as video format
# Similarly itag value 18 means video quality is 360p and there are many itag value through which you can download differently like just audio or video or both with different video quality.

dl.download()
Enter fullscreen mode Exit fullscreen mode

Output:

On console/python shell we get title of the youtube video printed.

Output 1

Our video gets downloaded were our python file is present automatically
video

There is many methods and features to be explored of pytube ,you can read those on official pytube docs.

Pytube : Docs | Github

Idea for Mini Project using Pytube Module :

We can make one mini project using PyTube and Tkinter which will download youtube video of different video quality and create GUI which will ask input from user for "youtube link","video quality",etc.

I m a individual content writer and if you like my work ,do consider buying me a book your small contribution will help be to bring more awesome content.

Buy Me A Coffee

Top comments (0)