Introducing The Simple Way To Make You Creating a Youtube Downloader With Python
Introduction
Hi guys, here is another tutorial for Python beginners! I wanna show you again how learning python can be funny.
In this tutorial we are going to see the simplest way to create a basic YoutubeDownloader with these features:
- Download a single video from the given URL
- Allow you to choose the stream when downloading a single video.
- Download a full playlist by getting the higher resolution videos.
- Show a progress bar during the download
I hope I have intrigued you with this brief presentation, so let’s code together!
Step #1: Import all the libraries
Before starting to write the actual code we need to import the following libraries:
- pytube: it’s in charge of managing all our interactions with Youtube
- click: that’s my favorite library for handling command line arguments and options
We can install them with the following commands on the terminal:
pip install pytube
pip install click
Now we can finally import everything we need:
import click
import pytube
from pytube.cli import on_progress
from typing import List
I also introduced hint typing just to show you a nice feature introduced recently in python 3.
It consists in type declaration and can be useful to:
- Prevent errors
- Use autocompletion in your IDE in your functions
Step #2: Define the auxiliary functions
There is not so much to do in this step.
In the case of downloading a single video, we want to print all the streams (there are different versions we can download).
Having all the information about the existing streams we can choose the one we want to download by its tag.
So let’s see the code of the function which prints the necessary information about every stream in a given list.
def print_streams(streams: List[pytube.Stream]):
for stream in streams:
print(f"itag={stream.itag} mime_type={stream.mime_type} "
f"res={stream.resolution} fps={stream.fps} "
f"vcodec={stream.video_codec} "
f"acodec={stream.audio_codec} type={stream.type}>\n")
It simply loops through the list and prints all the information for each stream on the screen
So this step is complete, now let’s use click to define our CLI options!
Step #3: Handle CLI Options with click library
The library we are going to use makes our work extremely simple. We essentially need two parameters:
- The Youtube URL
- A flag that tells us if we are downloading a playlist
This is the code:
@click.command()
@click.option('-U','--url', help='The url of the YT video', required=True)
@click.option('-P','--playlist', is_flag = True, default=False, help='The url of the playlist')
The parameters define how to call the option on the command line, if it’s a flag and the response to the help option. Obviously the decorators have to be above the main function.
Step #3: The main function
We finally got to the logic of the program, we have the main function which has as parameters the ones defined as options with the click library.
The function is divided into two main blocks:
- Download video
- Download playlist
This is the first block: “Download a single video”
yt = pytube.YouTube(url, on_progress_callback=on_progress)
streams = yt.streams.filter(progressive=True)
print_streams(streams)
itag = input("Please choose the video you want to download: ")
yt.streams.get_by_itag(int(itag)).download()
But what is this code doing ?
- Creates a Youtube object, initialized with the given URL, and sets the default pytube progress bar as on_progress callback
- Filters all the progressive streams ( the ones with video and audio in a single file)
- Prints all the filtered streams
- Asks the user to choose the stream by its itag
- Downloads the selected stream
That’s all, so let’s see the block related to the playlist download:
pl = pytube.Playlist(url)
for video_url in pl.video_urls:
yt = pytube.YouTube(video_url,
on_progress_callback=on_progress)
streams = yt.streams.filter(progressive=True)
print(f"Downloading: {yt.title}")
streams.get_highest_resolution().download()
print("\n")
And this is the description step by step of this code.
- Creates a Playlist object with the URL from the command line
- Iterates all the videos inside the playlist
Then for every video it:
- Filters the progressive streams as in the previous block
- Prints the title
- Downloads the highest resolution video
Now, before concluding I guess you want to see the full code of our mini project: