Craft Your Personal YouTube Video Downloader using Python: A Step-by-Step Tutorial

Rahul BhattRahul Bhatt
4 min read

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:

  1. Qt designer

  2. Anaconda or mini conda or python 3.7 compiler

  3. Any text editor of your choice

Libraries to download:

  1. Pytube

  2. Ffmpy

  3. PyQt5

This would be sectioned into the following structure:

  1. Creating the UI in Qt designer

  2. Converting UI file into Python file

  3. 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.

  1. radioButton — mp4

  2. radioButton_2 — mp3

  3. radioButton_3 — video

  4. 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

YouTube-Playlist-Downloader/YTDownloaderGUI at main · rahulbhatt1899/YouTube-Playlist-Downloader
*All my python scripts mentioned on my medium blog @rahulbhatt1899 - YouTube-Playlist-Downloader/YTDownloaderGUI at main…*github.com

Please do check out the references without which this article wasn’t possible:

References:

Python UI | Design GUI with Python | Python Bindings for Qt
*Learn about this official set of Python bindings for Qt that enable the use of Qt APIs in Python applications. Explore…*www.qt.io

pytube - pytube 11.0.0 documentation
*This part of the documentation begins with some background information about the project, then focuses on step-by-step…*pytube.io

ffmpy - ffmpy 0.2.2 documentation
*ffmpy resembles the command line approach FFmpeg uses. It can read from an arbitrary number of input "files" (regular…*ffmpy.readthedocs.io

0
Subscribe to my newsletter

Read articles from Rahul Bhatt directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Bhatt
Rahul Bhatt