YouTube Video Downloader With Python!

Dhruv PatelDhruv Patel
4 min read

Introduction

Have you ever wanted to download videos off of YouTube? Well with python it's easier than ever to accomplish. With python there is a library you can download called pytube found at pytube.io

Set Up

You are going to want to open up your favorite python editor and create a new file called app.py then through the terminal of the code editor you are using run this line to import packages from the pytube library

pip3 install pytube

After you installed the pytube library you are going to want to import the module, so you can actually use the library. Use this line of code to bring in the library

//importing the pytube library
from pytube import YouTube

Your first lines of code using pytube

Congratulations! You just imported the pytube library and you can now start using it and its functions and classes. But first before you start doing anything you need to create an object of "YouTube" from the YouTube class library before you can do anything fun.

//now we have a YouTube object called yt
yt = YouTube()

Now that we created an object of YouTube we can use some built in functions to do stuff with it here are some examples

//putting url inside 
yt = YouTube('http://youtube.com/watch?v=2lAe1cqCOXo')

//shows title
yt.title

Making the Bot

Well with that information you can see how easy it is to grab information from YouTube with just those two lines of code now we can write a script to build our YouTube video downloader bot. So let's make a new folder on our desktops and let's call it "YouTube" and another folder to store all your YouTube video downloads.

asset1.png

After the files are made drag the "YouTube" folder in to VS studio code or code editor of choice and create a file named YouTubeBot.py. after that let's install pytube with pip3

asset2.png

now lets import YouTube from pytube to start using the library

from pytube import YouTube

Now lets you create a function to capture the URL from the end user i.e. you.

//used to grab url 
def get_url():
    url = input("Enter the URL of the video: ")
    return url

We now have a function that can receive a YouTube link from the console input. But we're not finished yet thats just the first step. Now we have to create a function that download's the video given the url parameter given to us by the user.

//function to download YouTube videos it is taking in the url from user as a parameter
def download_video(url):
// We are creating a variable called yt and assigning it to the YouTube class that takes user inputed url
    yt = YouTube(url)
//Creating a titles variable to capture the title of the video being downloaded
    title = yt.title
//Prints downloading video plus title of video
    print("Downloading video: " + title)
//This code does all the downloading 
    yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download('file path of your youtubedowloads folder')
//We print the title of the video.
    print("Video downloaded: " + title)
//Returns titles of the video
    return title

//Now we call the download video function giving it the get_url function as a parameter
download_video(get_url())

Now let's talk about what that long piece of code does that actually downloads the video from YouTube first we use the built in .filer() function to filter the streams to get the highest quality mp4 stream. Then we use the .order_by().desc function to order the streams by resolution and then descending order. Then finally we use the .first().download() function to get the first stream and download it. Put it all together and you get this

yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download('file path of your youtubedowloads folder')

After you run it it should go look like this

asset3.png

Conclusion

Congratulation's you have now made a fully funtional YouTube bot you should be proud now to take it to the next level on my YouTube channel I will be posting a full tutorial on how to make this bot and a bonus section on how to automate it with a script so you don't need to keep opening up the code editor and running it that way. Links to my YouTube and other social media platforms can be found at the top of my blog. Thanks for reading and enjoyed the YouTube Bot!

4
Subscribe to my newsletter

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

Written by

Dhruv Patel
Dhruv Patel

I am a C# .NET developer that has experience with MVC, Razer pages and SQL Server.