How to make use of the Python pytube tool to download videos from YouTube

Introduction to the pytube tool

pytube is a python package written for the main purpose of downloading videos from the famous video website Youtube. According to the official documentation there are no third party dependencies to this tool, in other words it is a standalone package making use of the standard python libraries.

How to install pytube on your machine

The installation of any python package is made easy with the help of the python package manager pip. Shown in the following command:

pip install pytube

This command is going to collect the package, download it and install it for you on your machine. A successful operation should print the below output on your linux console.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Collecting pytube
Downloading pytube-6.2.2-py2-none-any.whl
Installing collected packages: pytube
Successfully installed pytube-6.2.2

Make use of pytube

Once the installation is finished open a python console and import the package pytube using the import statement, as shown in the following block of code:

import pytube

The python class YouTube represents a single instance of a Youtube session so you’ll need to initiate it using the following code:

yt = pytube.YouTube('https://www.youtube.com/watch?v=xtZOnyYu16U')

With the generation of the instance which is being assigned to the variable yt one can easily get the name of the video by using the filename attribute.

fn = yt.filename

As you may know to get the value of an object in python you just type the variable and hit the return button on your keyboard.

fn

The following will then be printed on the Python interactive console:

u'Stoto - Late Night (Original Mix)'

It is self explanatory. The result on the filename depends on the video link passed as input in the YouTube.__init__ method.

There is also a video_id attribute which parses the id from the video url being passed in the __init__ method.

video_id = yt.video_id

The id of the video can be printed using a print statement, like the one shown here:

print(video_id)

The following will then be displayed on the console:

'xtZOnyYu16U'

To get a list of the available videos for download the get_videos method can be run and assigned to a variable; as in this code example:

videos = yt.get_videos()

Then print each video using a for loop.

for v in videos:
    print(v)

The output which is going to be displayed on the console in my case is the following: *Depending on the video url you passed in the instance this output may be different*

<Video>
<Video>
<Video>
<Video>
<Video>

So far a list of videos is made available to us. Finding out the type of each object in this list is as easy as running the following code:

for v in videos:
    print(type(v))

The following will be printed on the console informing us about the type of object each one of the videos comes from.

The class pytube.models.Video can be used to create single instances of Youtube videos for the purpose of downloading them via the download method.

The first video instance can be pulled out of the list using the indexing features of the python list builtin object.

first_video = videos[0]

Printing the first video is as easy as utilizing this code:

print(first_video)

A Video instance has many attributes such as the filename, url, extension, video_codec, audio_codec, audio_bitrate, resolution, etc.

To access each attribute, the syntax shown below can be followed:

object.attribute

For example, to get the extension of the first video one can run the following python code.

first_video.extension

Running the above command is going to produce the following output in my case.

'3gp'

The quality of the video is being stored as a string in the resolution attribute of the Video instance.

quality = first_video.resolution
print(quality)

The quality of the video is going to be printed.

'144p'

Other attributes such as the bitrate of the audio or video can be accessed with the following code.

audio_bitrate = first_video.audio_bitrate
video_bitrate = first_video.video_bitrate
print(audio_bitrate)
print(video_bitrate)

Each Video instance has a download method which makes it real easy to download any video from one of the most used sites when it comes to video content.

One can easy download a video using the following code:

first_video.download('/Users/oltjano/Desktop')

The above code is going to call the download method in the Video instance and download the video to the specified path. The path is a required parameter to the download method of the Video instance so the program knows where to save the file.

The destination output directory in the above case is the following:

/Users/oltjano/Desktop

Feel free to specify any path you want. One can also overwrite a video file by setting the force_overwrite parameter to True, as shown here:

first_video.download(path='/Users/oltjano/Desktop', force_overwrite=True)

Download the desired video from Youtube

One may want to download a video from Youtube with a specific file format and quality. The pytube tool offers a method to the users to accomplish this task.

Note: It is recommended that you restart the python console so we can have a clean environment.

Import the pytube package with the following command:

import pytube

Assign the link string of the Youtube video you want to download to a variable so when needed to make multiple uses it is easier to accomplish the task.

video_link = 'https://www.youtube.com/watch?v=8ZpVwAeLzm4'

Then start a Youtube session by using the following command:

yt = pytube.YouTube(video_link)

Get the list of the available videos for download using the following command:

videos = yt.videos

The above list can give you information on the Video instances one can use to download the video via the download method.

Use the get method present in the pytube.YouTube instance to filter a Video instance based on the specified file format and video quality.

video = yt.get('mp4', '720p')

If available, the above code is going to return the specific Video instance. If not, then an error is going to be raised.

If no error is being raised the video can be downloaded by using the code shown below.

Before downloading the video, a path to the location where you want it to be saved will be required. Set the path variable as shown below:

path = '/Users/Oltjano/Desktop'

Then download the video using the following command:

video.download(path)

Conclusion

There are many tools out there that can help a linux user to download Youtube videos via the command line. Some are open source, some are not. With intro level coding skills one can write scripts to interact with pytube for specific purposes such as downloading multiple videos at once, or downloading an entire playlist.

But even if you don’t posses such skills, downloading a video with pytube is a matter of simply typing a single command on your console!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *