Python YouTube Downloader with pytube

Author:

Imdad Ahad

Keywords:

python youtube downloader with pytube,pthon youtube downloader with pytube,python pytube,pytube tutorial,pytube,pytube python tutorial,python youtube download,python tutorial,python,python programming,neuralnine,creative coding,competitive programming,python projects,web developer,full stack developer,remote developer,javascript 2020,web development 2020,competitive programming tutorial,tech with tim,react native,ap computer science,guido van rossum

Subtitles:
by the end of this video you're gonna have a really good understanding of how to go about downloading a youtube video with python by the way i'm giving away a free python guide that i usually sell so if you want access to that check out the link in the description below with that said i'm super excited i'm buzzing and i'm sure you are too so let's get started i'm gonna do is create a project folder so i'm gonna call this uh youtube video downloader by the way i'm doing this in my developer directory it's the directory where i store all of my development projects i highly recommend that if you don't have one of these that you create one because it can get messy if you decide to create um developer projects in the desktop or the downloads directory um it's best that you have one folder that has all of those so with that said uh created the folder here so what i'm going to do is open this up in my code editor today i'm going to be using vs code if you watch my previous videos i'd like to switch between vim pycharm and vs code but today i'm going to be using vs code by the way you know what i like about vs code it's got this integrated terminal so i hit command t and what it does is it opens up a terminal in the project folder so that's super handy because it means i can run my program right in this town so that's quite cool and we're going to use that later on but for now i can minimize it by hitting command t again so we're going to start off by creating uh the files and folders that we're going to need for this project luckily this is a small project so we're not going to do too much the first file we're going to create is the main.py file that's going to be the entry point for our program um in other words that's going to be the main program we run and then we're going to create a folder called downloads and that's going to store all of the downloaded youtube videos and then we'll create one more file and that's going to be requirements well let's create a file in the root um almost made a mistake there um we'll we'll create and they will be called requirements.txt and essentially this is going to store all the requirements or the python libraries we're going to use for the project actually for this video we're only going to use one but it doesn't matter even if we use 1 or 10 or 20. it's always important to have requirements.txt file it's good practice because if you pass over your project to someone or you share your code with someone they know what libraries they need to install uh to get the project up and running and that's really uh really key so uh let's get started by specifying the requirement that we're gonna need we use a library called pytube and pytube is a library that allows you to download youtube video so if i quickly search on google and show you what that looks like it's quite a cool library it's quite extensive and essentially the way it works is you give it a youtube url and it has all of these different features to for you to be able to you know uh download uh the video or even the audio behind the video so we'll talk about that a bit later on and it's quite cool again it's quite extensive so it's one we're gonna be using for the project now typically in your requirements.txt file um the way that you specify the version for this library is you uh type the library itself at the beginning then followed by double equals and then you put the version number here now if i go back to um safari here you can see that the version numbers here 10.00 or you can even go on pi pi so i'll just go in pi by and then if i type python by the way pi pi is the python package index this is basically where uh pip looks for all of the python libraries and the versions here you can see like 275 000 projects and you know all those fun details so if i type here pytube tap enter here you can see the pie tube library and here you can see 10.00 version you can copy that to quickly install it and you can also look at previous versions too so 10.0.0 is the latest version um however that is quite long right you don't have to keep going to pi pi and finding the version of a library so i've got a nice neat little trick that i'm going to show you that i use all the time i'm going to open up the terminal and then i'm going to type pip installed pytube so in this case the name of the library double equals and then i'm going to tap enter i'm not going to give it the version of my look at what happens when you do that what happens is it complains it says hey you wanted to install this library but we couldn't find a version that matched the one you gave of course because the one i gave was empty so i don't give it a version so you know it kind of just fell over itself and instead the caveat to that is it gives us a list of versions for that library that are available and as you can tell here 10.00 the one we want so that looks good so i use that all the time by the way especially when i want to find out the different versions available for a library so copy that from there paste it in there and we're good to go that's pie tube ready uh so let's start off by checking out the docs i think generally speaking whenever you install any library it's really good to go to the documentation just even if it's half a minute just to understand how it works and usually they have a quick stop guide so we'll click on the quick start we'll just go through this together the first thing that you notice is in this case it's going to show through an example of downloading the video and as you can tell it starts off by importing this youtube class and the way it works is um when you uh want to download the video you pass the video url um inside this youtube class and then what that does is it creates an object and here the object is the yt object right you can call it whatever you want but they've called it yt and the idea is that now this object represents the youtube video right and so what it means is that you can call a bunch of methods on it and so um you can also get access to uh some other details of that youtube video so you can get access to the title by typing yt.title you can get the thumbnail url so you can even download that and most importantly and this is the this is our focus for the video today uh you can get access to all of the streams so these are basically all of the different uh streams available for that youtube video right so i'm going to stick some youtube theory at you for a second did you know there's about a billion youtube video views a day or so on youtube mobile i digged up that stat the other day and that is just crazy right it just goes to show a lot of people watching youtube now what that means is youtube have to get really efficient at being able to stream video back to you and one of the ways they do that is uh splitting out uh streams so what do i mean when you watch a youtube video uh youtube video has what video and it has audio right it's all in one file but what youtube do these days anyway is they split out that file into two they split that out into video only streams so so you'll get some streams that are just video only so what they won't have sound and you get other streams that are audio only so they'll be the audio but without the video and the idea behind that is then they can get better at streaming back the video and the audio separately so i don't know if some of you have used youtube premium but if you minimize uh the youtube premium app it plays the audio in the background so as you can imagine what that's doing is it's not playing the video stream anymore or it's not delivering the video stream to you anymore it's only delivering you the audio stream right so that's even more efficient right so that's a bit youtube theory the reason why that's important because we want to there's a list of streams and we want to find the stream that is both video and audio right because some of these streams are video and audio and uh of those streams we only want the one with the highest quality right so it's we have to do a bit of searching and that's quite cool because the pie tube library gives us some features to be able to filter and search around for the stream that we're after start by importing the pytube library and i'm gonna write the if name main directive you've probably seen me do this before in my previous tutorials essentially this tells python where to start running the program and we're going to tell it start with um executing the main method we haven't yet created that but that's fine because we'll create that right now and we'll just leave it empty for now so we're going to break up this application the first thing we want to do is we want to ask the user for the youtube video url right we want to ask the user hey what video do you want to download for the stream with the highest resolution right highest resolution now i mentioned that the streams are split in audio and video and you get other streams that have both we want the one that has uh the stream with the highest resolution and is both audio and video right and then afterwards we want to then download that to the downloads directory and then just to make this uh program even more cool what we'll do is we'll get it to open uh the video after it's downloaded download it this is just an optional step i'm gonna do that because why not right so let's ask the user for the youtube video that's quite easy we'll create a variable called url and then the way you do that is you use make use of the input function in python which is quite cool what it will do is it'll ask user for some input and you specify the message that's shown to them when they're asked for that import so we'll say enter the video url video url put colon after that and what we'll do is ask it for the input and then it'll put it into that variable so that looks good now here comes the fun part this is where we're actually going to be implementing the download video logic so i'm going to type um well i'm going to say file location because after this file is downloaded we want to get the location of where it was downloaded to and then we're going to create a method called download video we'll pass it the url so that's the one that we got here and then we'll say where do we want to download it we want to put it into the downloads folder i haven't yet created that that's going to be a constant i'm going to define at the top so downloads folder and that's going to basically be this downloads directory so i'll put it into quotes and then if i can type slash downloads right so that's going to correspond to the downloads folder we created earlier on here and so what that's going to do this method we haven't implemented yet which is fine but that's going to take in the youtube video url and the downloads folder and it's going to download the video into that folder and then it's going to give us back the file location right so let's implement this download video method so i'm going to type def download unscrew video and then it's going to take a url and the folder to download i'm just going to call it output directory and then in here here's where we're going to type in the code to download the video so the first thing is we want to get the uh youtube object set up so we'll pipe yt as they described in docs and we're going to use youtube import the youtube class so let's import that above actually actually the import is not going to be import python it's going to be from pytube import youtube because we're only making use of one thing in the pi tube library right so we don't need to import the entire library we just want to import this youtube class from the python library so just one thing cool so in this youtube um constructor will pass in the url so that's a video url and then the next thing we need to do is we need to find the highest quality stream so i'm going to just create a variable called high or highest quality stream and what we're going to do is we're going to make that equal to yt dot stream so i remember i mentioned that streams give you back a list of all the streams and then the next thing we want to do is we want to invoke a method called filter right so pi tube has this really cool method that allows you to filter for uh different things and one of the things that lets you filter by is uh streams that have both audio and video and that's what we want so the way you do that is you give it the argument progressive and you set that equal to true now for now i'm just going to temporarily set a break point here so that you can see what's going on the way you set a breakpoint in python is import pdb um import pdb as you can tell i had to snip it for it but i'm going to type it out just so you see what i'm doing and then pdb set trace i think in newer python versions actually you don't need to um i think yeah in new python versions i think you can get away with just typing debugger i think that's a new thing in the new python versions do let me know in the comments below pretty sure it's in python 3 but i'm using python36 today so that's fine so i'm going to set a breakpoint here anyway i'm going to run the program so you can actually see uh what's going on here right so i'm going to run the program i'm going to type python main.py and then it's going to ask us for the youtube video url so i'm going to switch over to safari and i'm going to take one of my youtube videos which is the automating new music with python using the spotify api quite cool project so do check it out if i switch back over to vs code and i paste it and i hit enter uh what you'll see is uh we're gonna enter the matrix so we're now in the matrix uh so congratulations we have wound back time actually we're not but we're in the debugging environment so you can tell uh by the fact that it says pdb here and the cursor is blinking so it's waiting for us to do something now what i want to do is i want to show you the contents of this variable here the highest quality stream and if i paste that um and i tap enter what we've got is we've got a list we've got an array right um or list and in that list we've got two streams so we've got one stream that's 360p and one stream that's 720p so essentially this has given us back the streams that have both audio and video which is great now we want to do some filtering so that we can get only one stream which is the highest quality uh stream and then we want to download it into the downloads directory so i'm going to exit out of this i'm going to do that by typing c which just continues the execution of the program and then i'm going to hide this terminal and then i'm going to get rid of the break point and then what we want to do is um once we've passed in the progressive filter we then need to order by and so there's a function that's given by pi tube that allows you to order by an attribute and the attribute we want to order by is resolution so again we want the best quality resolution first so we order by resolution in a descending order right so that we get the best one first and then um this is gonna order by descending but it's gonna give us um you know it's gonna give us more than one so we only want the first one because it's in the descending order the first one will be the best quality one and then uh we do that using first and then really at this point we can call the download method which is now we've got the stream that we're after we just need to specify uh we can downloading it and you can either leave this empty which is what it will do is you'll just download the video and store in the current directory or you can give it your own uh directory location and it will download it into there and um i am going to give it the output directory that we passed in so uh that looks good if i are my tabs working okay i was a bit worried there okay so now that's done so that's going to download it into the output directory and that looks good really so i think the last thing we need to do is we just need to return the highest quality stream this will actually now store the location of the file which is quite handy uh because we'll return that back to the main method cool so that looks good and the last thing we want to do is we want to open the video after it's been downloaded the way we're going to do that is we're going to import a library from the standard library in python and that library is called subprocess subprocess is a library that allows us to execute shell commands what's a shell command you ask well a shell command is a command that you run inside the terminal for example if i want to open up a file i would type open followed by the name of the file and then that would open the file and this other command you can of course run inside the terminal but this is one example right so we want to be able to run this command open and the name of the file now this is a shell command and to run a shell command in python you need to import the subprocess library and then there's a method that you can call which is called subprocess.open what that does is it takes an array uh the first argument is the name of the command um sorry it's not sub-process to open it's sub-process.cord it's getting mixed up there but the name of the command is open and then usually what happens is you can the reason why it takes in an array or a list is because you can give it multiple arguments right um but in this case i'm just going to give it one argument which is the name of the file that we want to open and in this case the name of the file is provided to us handily from here so post it paste that there so that looks good and i think that's everything to now run our application so everything looks good so let's go to the terminal and now i'm going to type python main dot py and then when i enter the youtube video url hit enter and what it will start doing is it will start downloading as you can tell it's blinking so if i go i can as you can see now the files here right now but obviously it hasn't fully downloaded yet but as you can tell it's downloading and so any second now what will happen is it will get to the point where it's downloaded the full file and it'll open up and it's done that exactly here and you've got this mug shot of me let's fast forward it so it doesn't look as bad um but yeah this is the video that's opened up in quicktime i'm using quicktime but yeah so that's the program in action and that works so that's fantastic make sure you like and subscribe for more programming tutorials and videos like this and until then i'll see you next time see ya peace [Music] you

Loading