You can build your own YouTube downloader under five lines of code. Python has huge free modules and libraries available. To build a YouTube downloader in python, we will use a module called pytube.
In this tutorial, I assume that you already have installed python version 3 and above on your machine and have some basic concepts of python programming. You can use any text editor such as VS Code, Pycharm, IDLE, etc…
Before we start to write a python script for the YouTube downloader, we will install the pytube module: downloading a video from YouTube with pytube is incredibly easy.
Step 1: Install the Pytube module
To install pytube, run the following command in your terminal/cmd:
pip install pytube
Step 2: Import the YouTube class from pytube
from pytube import YouTube
Step 3: Filter the type of video streams
YouTube facilities two types of streams ie DASH and Progressive streams. Dynamic Adaptive Streaming over HTTP (DASH) will just have video or just audio which we are not going to discuss here. We need to download both the audio and video tracks and then post-process them with software like FFmpeg to merge them. The legacy streams that contain the audio and video in a single file is called Progressive stream and it works for resolution 720px and below.
Therefore, we will write a script that can download progressive streams of 720px.
To download the resolution of 720px, we have to pass an argument 22 to the parameter called get_by_itag(). The Itag 22, will have a resolution of 720px.
Moreover, we will filter the file extension of mp4 only.
Step 4: Download a video
You can use the download method to save the file. By default, the file will be downloaded at the location of your script. If your script is on a desktop, the file will be downloaded on the desktop.
Complete YouTube downloader source code:
Combining all the steps mentioned above, you will get a script shared below which can be used in your machine if the python and pytube module is installed.
from pytube import YouTube URL = input("Enter video URL:") video = YouTube(URL) v_streams = video.streams.filter(progressive=True, file_extension="mp4").get_by_itag(22) print("Downloading.......") print("Filepath:", v_streams.download()) print("Download completed.")
Enter video URL:https://youtu.be/VhynagRiBv4 Downloading....... Filepath: C:\Users\DP\Desktop\Akash Vukoti Impresses With Spelling Bee Little Big Shots.mp4 Download completed.
It’s always better to automate things and use our own self-creation scripts. Using third-party websites to download videos may lead to phishing links which are quite dangerous. I hope this YouTube downloader script is helpful to you.