A simple Python Script to download YouTube Videos using pytube

Shubham Phadte
Sage Systems
Published in
3 min readSep 27, 2020

If you are just starting out learning python and looking for simple python projects or applications or if you are looking to download YouTube videos the “programmer way”, then this article is for you.

This was one of my first python scripts a few years ago and I thought of putting it out here along with the github repository url so that others can access it. I know there are many websites where you can simply enter the url of the YouTube video or YouTube playlist but there is a different vibe about running a command line script to download YouTube videos.

pytube is a very serious, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos. It can be used to download individual videos as well as a playlist of YouTube videos. So let’s get started.

  1. In an empty directory create a blank file with a .py extension (the python script)

2. Install the pytube library using the command line(below command is for python 3)

3. Import the Youtube module in our script

4. I’ll explain the following code line by line

Run the script the command line using python <scriptname>.py

The first 3 lines is about taking the input from the command line. Once the script is run you can enter the url of the desired video in the command line and press enter.
It gives you the details of the YouTube video. yt=YouTube(x) where x is the url of the video.

As can be seen yt.title will give you the title of the video.

yt.streams will give you all the streams available of the video. We are accessing the first stream(highest quality) using yt.streams.first().

Finally we download the video using download() method. yt.streams.first().download() will start downloading the video and “Download Successful!!” will be printed once video download is completed.

5. To access the available streams of the video, you can use yt.streams.all()

6. To download an entire playlist

Github repo: https://github.com/shubham-rp/python-youtube-downloader

To know more about pytube: https://pypi.org/project/pytube/

--

--