Python Programming: YouTube Video Downloader Program

LoganTheDudeRox
3 min readApr 26, 2021
Photo by Sara Kurfeß on Unsplash

YouTube is a very well known social media platform, that is used by millions of people daily. On the application, there will always be a video or a few that you would want to save onto your computer. The downside of this is that to download videos, you would need YouTube premium which would add up over time! The following article will teach you how to make a YouTube video downloader with Python for completely free!

Note: This code requires Python’s pytube module to work! You need to open up Command Prompt and run the following commands for this to work!

Windows
py -3 -m pip install -U pytube
MacOS or Linux
python3 -m pip install -U pytube

Imports (Step 1):

For this code to work, we will need the actual imports. The imports needed for this is pytube .

from pytube import YouTube

This code simply uses pytube to take the file known as YouTube so we can convert the videos.

Users Video And It’s Data (Step 2):

Now to add some extra fancy stuff before we actually download the video, we would want a list of the videos data!

link = input("Enter the YouTube link: ")
video = YouTube(link)
data = "{video.title}'s YouTube Data\nViewcount: {video.views}\nRating: {video.rating:.2f} \nDuration: {video.length / 60:.2f} minutes"print(data)

This code uses Python’s input feature to get the users video link, and convert it for use with the imports were using. The variable known as video is keeping all data of the video link stored in it. That’s why we can use video.title, video.rating, video.duration, and a lot more to organize this data.

Downloading the Video (Step 3):

It’s time for what you’ve been waiting for… downloading the video to your own computer!

youtube_filter = yt.filter('mp4')
yt.set_filename(video.title}
folder = "C:\example\example\example"video = yt.get(youtube_filter[-1].extension, youtube_filter[-1].resolution)video.download(folder)print("Video successfully downloaded!")

We start by creating a variable called, youtube_filter that filters out all of the possible file extensions. We set it to an mp4, but you can set it to whatever you want (for example, ‘mov’).

After that, we create a variable called folder. This is the file directory that the file will be saved in. This is required if you want to save the video. We then create another variable called video. This takes the maximum resolution of the video, and the actual video file.

video.download(folder) is the part that actually downloads the video to your selected folder.

Running Your Code (Step 4):

You have completed all of the code! Now, you need to run it.

There are 3 main possible methods of running, and here are all of them!

Method 1: Repl.it

If you are using repl.it to code your program, simply press the button on the top that looks like a play button to run your code! Then type in the black box in response to everything your being asked!

Method 2: Original Python IDE

If you are using the normal Python IDE, the ones that comes when you are downloading Python, you simply will press F5 on your keyboard, or click on “Run” then “Run Module.” This will pop open a new window to use your code!

Method 3: Notepad or any Programming IDE

You would simply open the file directory that your Python file is in, and in that directory open command prompt. In command prompt, you will type py <filename>.py, and you can replace the first py with what you have Python installed as. It could be python, python3, py3, and a few other things!

Thank you for reading this, and hopefully you learned something new! The full code to this in a single file is at this GitHub repository:

https://github.com/LoganLikesToCode/Medium-Articles/blob/main/medium6.py

--

--

LoganTheDudeRox

Hey, I'm a Python Programmer and I love making Discord bots and teaching people!