Craft Your Personal YouTube Video Downloader using Python: A Step-by-Step Tutorial
YouTube Downloader Hacks
This is the thing we are trying to make today for this article
The code for this software is available on my GitHub, the link is given below.
If you haven’t checked out my previous article on the same topic but not the GUI version, please check it out.
So before starting to code let's check if you have all the requirements beforehand:
Pre-requirements:
Anaconda or mini conda or python 3.7 compiler
Any text editor of your choice
Libraries to download:
Pytube
Ffmpy
PyQt5
This would be sectioned into the following structure:
Creating the UI in Qt designer
Converting UI file into Python file
Creating relevant functions
Creating the UI in Qt designer
After opening Qt designer you can create a similar structure or something else or you can just download the code from my Github repo.
Qt designer layout we will be used for designing the app
The below section depicts the mapping between class and its instance, so if you cannot follow the code comeback to this image and it will be clear or you obviously can check out the entire code in my Github repo.
Total elements in the YouTube video downloader
Converting UI file into Python file
After creating the UI for your software, you’ll use this command to convert the UI file into a py file.
pyuic5 -x filename.ui -o filename.py
Now, you can check out the py file and understand how the Qt designer has automatically created the code for PyQt5 with two methods inside Ui_Main class.
The overall structure of the software
Ui_Main class is the main class where the UI is defined and setupUi() and retranslateUi() methods that define the structure in PyQt5.
after creating a py file with the command
These are the variable names that I am using, this information will be useful for you to understand the code.
These will be the variable names that we will be using in the app
Again the radioButton variable name mapping with UI just so that you can understand what the code is trying to do.
radio buttons names that I have given
Add click event handling to submit and browse buttons.
Adding the event handler for the two buttons
The browse() button function is simple, it is used to set the download directory of your mp4 or mp3 files.
Browse button function
The Show_pop() function is used to display “Download is complete”.
Showing a completion message after successful download
The submit button function is where the magic happens. The following is the structure of the submit function. It is taking the input from the textbox and displaying it on the list view.
The conditional statements are used to check whether radio buttons are clicked or not.
The following is a mapping of radio button variables to the functionality they will be providing if checked.
radioButton — mp4
radioButton_2 — mp3
radioButton_3 — video
radioButton_4 — playlist
Submit button function structure
This is code for downloading the YouTube video or playlist in mp4 format.
The nested conditional statements are for checking if video or playlist radio buttons are checked or not.
If mp4 is checked
These two radio buttons are to separate whether we are downloading a video or an entire playlist.
Code for if the video or playlist or none is checked
This code is used to download the YouTube video or playlist in mp3 format. Pytube doesn’t allow in mp3 format but you can download the file in mp4 and convert it into mp3 using ffmpy.
If mp3 is checked
This code is used to download YouTube videos in mp3 format in your chosen download directory.
If mp3 is checked and if the video is checked
This code is used to download the YouTube playlist in mp3 format in your chosen download directory.
If mp3 is checked and if the playlist is checked
The nested else condition is just to check if the video or playlist radio buttons are checked or not.
If none of the radio buttons among video or playlist is checked
The outer else condition is just to check if the mp3 or mp4 radio buttons are checked or not.
If none of the mp3 or mp4 radio buttons is checked
Now you will be able to download YouTube videos or entire playlists in mp3 or mp4 format.
Thank you for your attention.
Check out the code in my Github
Please do check out the references without which this article wasn’t possible:
References:
Subscribe to my newsletter
Read articles from Rahul Bhatt directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by