Hello everyone!
For the past few days, I've been solving problems on CodeWars. It has been great so far. Sometimes, the problems I attempt aren't so much of a task. Other times, I'm spending almost an entire afternoon thinking of how to fix my code. Overall, the experience has been really interesting.
While I was doing my usual problems on CodeWars, I got a thought of making a Python project out of the blue - something that normally wouldn't cross my mind. If I was going to make a project, I figured that it would have to be something that I would use, and probably isn't so far out of my skill level.
After pondering on what I should create, I finally decided to make a YouTube Video Downloader using Python. Even though this seems kind of common to make, it's actually something that I kind of need; Besides, it looks like something that isn't so far out of my skill level to create. I haven't had the best experience with YouTube video downloaders in the past, and this could probably replace them all.
And so, we begin...
At first, I had no idea of what I needed to start developing. So, I had to search online for what packages I would need to begin this somewhat stressing programming journey. It turned out all I needed was the PyTube module which is basically a module that is used to interact with YouTube and probably other video streaming sites (Well this is my thought since it is possible to import YouTube from the PyTube module).
I thought of making the downloader and using the console to download YouTube videos (Creating it without a user interface), but I figured that would be too easy. Besides that, I'll definitely get tired of opening my IDE all the time whenever I want to download YouTube videos at a point (It takes a while to run on my laptop).
So, I decided to make a user interface for the program, and I could already see the difficulty of this project rising significantly. For the UI, I used Tkinter for Python since that appears to be the most popular UI framework for this job.
To be honest, until now, I had never written a line of code for a GUI, and I had no idea on how to even do that. So, I spent around 2 to 3 hours learning how to use Tkinter. Luckily, there is a Tkinter documentation page that shows how to use it. I'm really thankful for that. If I hadn't found it, I probably would be moving in circles.
The first thing to do was to create the main window of the application. I wasn't sure of how big I wanted the window to be, so I gave it a width of 500 and a height of 300.
root = tk.Tk()
# Window size
width = 500
height = 300
Together with this, I also learnt a way of making the window stay on the center of the screen when it is opened.
# Screen Size
scr_width = root.winfo_screenwidth()
scr_height = root.winfo_screenheight()
# Center Positions
center_x = int(scr_width/2 - width/2)
center_y = int(scr_height/2 - width/2)
root.geometry(f'{width}x{height}+{center_x}+{center_y}')
root.resizable(False, False)
root.title('VideoTube')
root.mainloop()
From the code, root.winfo_screenwidth()
and root.winfo_screenheight()
get the size of the computer screen and the center x and y positions for the screen are calculated and assigned to separate variables. Then, using the geometry
method, I specified the x and y positions of the window when it opens, alongside the width and height of the window. I also specified that the window should not be resizable because I could already tell that would cause some serious issues.
And this is the result of it:
Now that I got the main window down, it was time to start filling it up with widgets.
I didn't have a solid idea of what to add to the window, so all I added was just a couple of labels (One as the header of the window, and one indicating where to enter the YouTube link), an input entry bar to put the YouTube link in, and a download button.
I also noticed that after creating a variable for the tkinter widget (Buttons, text fields and stuff), you have to use the pack command on it (like variable.pack()
). From what I've seen, I think that is used to render the widget on the main window.
label = ttk.Label(
root,
text='A Simple YouTube Video Downloader',
font=('Helvetica', 12)
)
link_label = ttk.Label(
root,
text='Enter your link here',
font=('Helvetica bold', 12),
)
link = tk.StringVar()
link_entry = ttk.Entry(
root,
textvariable=link,
)
download_icon = tk.PhotoImage(file="./assets/download icon.png")
download_button = ttk.Button(
root,
image = download_icon,
text='Download',
compound=tk.LEFT,
# command=download
)
label.pack()
link_label.pack()
link_entry.pack()
link_entry.focus()
download_button.pack()
This is what I've been able to come up with so far:
For the download button, I got a random download icon that was on the tkinter documentation website and used that together with my download button, and it worked quite well.
Out of curiosity, I decided to try using pad methods according to what I saw in the tkinter examples to give the widgets a bit of space because I didn't like how everything was so jampacked. What the pad method does is that it adds some space around a widget either vertically or horizontally.
With all of that out of the way, the basic appearance of my YouTube video downloader was set.
Now to work on the main download function of the program.
Working on this one wasn't that difficult to be honest. What I did was to create a variable that gets the YouTube link from the text box, create another variable that stores the video stream as an mp4 video file with a resolution of 360p (Made it this way to test if it actually works). Once it has gotten the video stream, it proceeds to download it.
def download():
url = YouTube(str(link.get()))
video = url.streams.filter(file_extension='mp4').get_by_resolution('360p')
video.download(filename=video.title, output_path='./')
showinfo(title='Success', message='Download Completed')
I also made it to show a notification when the video has been downloaded.
I had to download a random video from YouTube to check if it's actually working. It worked, but I forgot to add the format for the video in the program. Whoops.
I forgot that sometimes, you have to be explicit when programming.
Whenever it's downloading a video, Windows always thinks that it's not responding. I almost closed it at a point thinking that it wasn't responding too.
So far, not much has been done on it. I believe that will be able to polish it up after a few days. I also plan to add a bunch of features to the program as I keep working on it.
Python does have lots of cool features and libraries. I would never have imagined that building a YouTube downloader will be this easy.
I'll be looking forward to seeing the end product of your project.
And if it's fine by you, I'd like to know what you regularly use Python for. I'm asking because I recently started learning Python for web development and I'm trying to connect with people who share similar interests.
Normally, I use Python for web, software and hopefully, game development. For game development, I don't plan to use Python mainly for it. Just for starting up and learning the ropes of game dev.
Also, thanks for reading this.
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.
Congratulations @sidkay588! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):
Your next target is to reach 2250 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Check out the last post from @hivebuzz: