Chapter 6: Building a Simple Desktop Youtube Downloader
Estimated time to read: 3 minutes
In this chapter, we will be building from the user interface we've shown on chapter 5. We will be building a simple youtube video downloader using a third-party library. In this chapter, we will exercise more on the concept of classes and objects and usage of a third-party library.
The Interface
We will be using the user interface from the previous chapter:
And so we will also be using the code to build it:
In the listing above, on line 17, we added the padding for horizontal with padx
and the fill='x'
to
make the entry expand horizontally when the window is resized.
Line 3-5 is a function that we will be dealing with later. For now, it just gets the content of the txt_url
entry and
prints it to the console, but later, we will using this to add the logic for the download of the best quality video
available for the youtube video given.
The library
In this project, we will be using the yt-dlp
library. As the other libraries, pytube
and youtube-dl
is giving
some issues, we will be using this one for now.
To install the library:
Text Only | |
---|---|
This will install the yt-dlp
alongside with other pre-requisite libraries:
Brotli
certifi
mutagen
pycryptodomex
websockets
Now let's create a new class in a new file. For this let's call it downloader.py
or name it anything you want.
downloader.py | |
---|---|
This class has a constructor that takes url as a required argument so we must pass it in the object initialization from our main code like this.
Text Only | |
---|---|
So now, our download function would be like below:
Text Only | |
---|---|
Our main.py
now will be
Now you can run the program and paste a youtube url in the entry. Then click on the Download button. This will download the video with the best format in your current directory.
In summary, we have tackled the following:
- Created a new class
Downloader
- Using the third party package
yt-dlp
- Object initialization and usage of the constructor with parameter
downloader = Downloader(url)
- We created a method called
download
inside our class for downloading
In the next chapter, we will be using a treeview to display the available video formats in a tabulated form so that the user can select (double-click) an item to download, we will also display the progress of download in a progress bar with the help of threading for multitasking.
The code for this chapter can be access on this website's repository or you can directly go into this link https://github.com/alexiusacademia/programming-with-python-course/tree/main/docs/course/chapter6
I hope you gained something from this. For any queries, jsut ask in the comment below. Thanks!