Photo by Alexander Shatov on Unsplash

Create your own YouTube downloader in 2 mins

theoneamin
2 min readJun 4, 2021

--

The code for this article can be found here: https://github.com/mamin11/youtube_downloader

In this article, we will be looking at how to download video or audio files from YouTube using less than 15 lines of Python code. We will be using pytube which is a free Python library. To be able to successfully complete this tutorial, you will need to have Python installed on your machine. You can read more about it here: https://pytube.io/en/latest/

“pytube is a very serious, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.”

To be able to use the library, we first need to install it.

pip3 install pytube3

Once installed, you can then create a file, give it any name you desire but make sure that it has the .py extension. This tells that the file contains Python code. I have called mine driver.py.

In the file, we are going to create a simple function that we can pass a URL to and it downloads the video for us whenever we call it.

I have called my function downloadLink and it takes a URL as an argument.

def downloadLink(url): 
try:
from pytube import YouTube
except Exception as e:
print("some modules are missing {}".format(e))
ytd = YouTube(url)
print(ytd.streams.filter(only_audio=True, file_extension='mp4').first().download(output_path='C:/Downloads'))

downloadLink("https://www.youtube.com/watch?v=KGD2N5hJ2e0")

To run the script, open a terminal and navigate to the location of your file. Then simply run python driver.py or pyton3 driver.py depending on the Python version you have installed on your machine.

And that is it, you have yourself your own YouTube downloader.

Thanks for reading.

--

--