How to Upload Videos with the YouTube API (using Python)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is tony teaches tech i'm tony and in this video i'm going to show you how to upload a video to youtube via the youtube data api now if you're not familiar an api is an application programming interface and we're going to be able to do this with the python programming language okay so there's some setup that we have to do here as far as creating a project and getting our credentials all in place but once we do all of that that's once and done then we can use the api execute a python program and upload a video that way directly from your local computer whether it's a laptop desktop anything like that so if that's something you want to learn how to do i'm going to walk you through every step of the process let's go ahead and get right on into it okay so here is um a youtube channel that i just created there is no subscribers no videos we can check under the content section nothing like that so by the end of this video like i said the goal is to upload a video directly to youtube via the api with python okay so how are we going to do that we're going to use the youtube data api okay and here at developers.google.com youtube version 3 getting started if you come down here you'll see a pretty good overview of what you're able to do with the api uh but specifically what we're interested in interested in today is interacting with the api with python so you'll see these are all the different ways that you can interact with the youtube api we're interested in python so let's click on python and some of the prerequisites here are python 2.7 or 3.5 we're going to use 3 dot something i think it's probably 3.8 actually let's check that right now so open up a terminal window on your computer and type in python [Music] 3 dash capital v so we're going to use python 3.8.9 so things might be slightly different if you're not using that same exact version but for the most part as long as you're using a modern version of python then things should be pretty similar now i don't recommend that you use the installed version of python directly so if we do which python right now uh which sorry pyt python3 it's the version out of user local bin okay we want to create a virtual environment for this this project that we're working on so what i'm going to do is type in python3 mvenv for virtual environment and then we're going to just put that in an end folder and we'll call it uh since i already have demo in there i'll just call it api okay so that's going to go ahead and basically take a copy of the python binary and put it into that directory and then if we want to activate that virtual environment we can source the api uh subdirectory bin and then activate so now when you see uh this that means you're working within your virtual environment and if we do which python now um well we should do witch python 3 as well we're working out of user tony florida the environment that we just created and there's our python binary and we can do python-v and you'll see that we're still using 3.8.9 okay so now that we have that out of the way let's go back to the developers page here for the the youtube api and let's install some of the packages that it recommends so we'll do pip install uh the google api python client hit enter and that will do its thing while that's going let's get the other one pip install google auth oauth library and google auth http lib2 so if this is done we can go ahead and execute that yep so we'll do pip install that and there will be another package that we have to install but we'll get to that in a little bit this is based on the recommendations here that's what we need at this point okay so um now that we have that what we need to do is create a project within the google cloud platform to represent our interactions with the api so in order to do that we want to go to cloud.google.com open up a new tab go to cloud.google.com and if you don't already have a google cloud account you'll go you'll have to create one but once you do have a google cloud account you can click on this console button up here to take you to your google cloud console and what we want to do is to create a project um to associate with our uh you know our program that we're going to interact with the api so we'll make a new project i'll just call this [Music] python youtube demo okay that's fine for now we'll click create and that's going to take a few seconds to create that project but while that's happening what we want to do and okay so it's done so we'll select the project and now you'll see here at the top we're working within the python youtube demo project okay so everything that we do from this point forward in google cloud console will be associated with this project all right so what we want to do is open up the left-hand navigation menu over here and go to the api and services section and what we want to do is enable the youtube api for this project so in here we can do that if we go to the library we'll search for youtube and we want to interact with the youtube data api version three click on that and we will enable that api for this project okay so that'll take a second or two to enable and we should see it [Music] not on this page but if we go back to the apis and service pages i think we'll see a green check box or something next to it uh youtube here the youtube data api version three uh how do we know all right this is this is how we know it's enabled because it's the we have the option to disable it okay so that's good for now what we want to do is to create credentials for our api so the reason you have to do this is because the when you execute our command on the python uh when you execute the python command in the terminal window um it needs to know which which uh how should i describe it which project it's interacting with not only that but it needs to know it needs to give the user a way to sign into their youtube account okay so what we're going to do next is enabling all of that to happen so right now we don't have any api keys or oauth clients and we'll need to get that and just to make this things crystal clear here if we go back to the the guide here for the youtube api and we click on upload a video you'll see that we need a client id and a client seeker so that's basically what we're working towards at this point okay so what we want to do is create credentials we want to create oauth client id okay and this is telling us first that we have to configure the consent screen so let's do that first this is going to be available externally so the other option is internally just within your business so this is going to be available um for anybody but you will need to go through a verification process if you're going to make this you know available to everyone but for now we'll explicitly and this will make sense in a second we're going to explicitly say which users are able to use this api okay so the actual application name um again we'll pick something similar like python youtube app the user support email tony teaches tech gmail.com um the app logo i actually have i think this is required but if not just put something up here for your app logo the application domain i don't know if you need all this stuff but uh i'll go ahead and put it tony teaches dot tech um i don't have a privacy policy link or terms of service link and then uh authorized domains i don't think we need to put anything in for that but developer contact we'll say tony teaches tech at gmail.com and it looks like i have an error up here so it seems to be https colon slash tony teaches dot tech okay missing i guess we do have to add the domain name so tony teaches dot tech okay so fill out that information save and continue hopefully that lets us through okay and now um this is asking us to uh pretty much we enabled our project to interact with the youtube api but there's certain like functionalities within the youtube api that we only want to work with so we can add that scope here and basically what we only want to be able to do excuse me is to upload a video so let's look for that scope in here the upload scope and that's this one right here youtube.upload so we want to add that to our api access so you'll see that's down here so we'll save and continue and then like i was saying before the only people that are going to be able to use this app until it's approved by google are the people that you explicitly give access so i'm going to add a test user and i'm going to add myself as a test user so tony teaches tech gmail.com and you know if you had a buddy that you want to test out your app then you can give put the email of your buddy here too okay so we'll save that and this is just a final preview screen this is what we have enabled so we'll go back to the dashboard and now we can go ahead and add the oauth credentials so if we go back to credentials um try to do the same thing that we did before create credentials oauth client id and application it's going to be desktop app because we're going to be running python from the command line and you know we'll just say desktop client for now so we'll create that okay now we got our secret and our our client id and our client secret so you can copy and paste them somewhere else but they'll always be in here if we need it so we can click on ok and if we need to get this i believe we can hit the edit box here and yeah we'll see your client id and client secret so at this point what we're going to do is go back to the youtube api overview page upload a video section and as it says here to use oauth 2.0 you'll need to create a clientsecrets.json file that contains this information so let's go ahead and do that let's open up a terminal window and we'll make a new file called clientsecrets.json and basically copy this entire section right here into there paste it in and what we want to do is replace these brackets right so everything between this open bracket and the close bracket client id let's go ahead and go ahead and grab the client id which is this one right here so we'll copy that you guys can see where this is going i guess paste that in and then the same thing for the client secret everything between the brackets get rid of that just want to put it in the quotes so we'll come back here get our client secret and paste that in okay everything else we can leave the same we'll go ahead and save this file and at this point let's go back to the youtube api overview and this is this is really cool because it actually gives you somewhere on this page an entire sample code for python that actually allows you to upload a video to the api so a lot of the work has been done for us with this sample code so i'm going to copy this entire thing we're going to make some modifications to it but i'm going to copy this entire thing and put it into a file called uploadvideo.pi so back in your terminal or in your text editor whatever you're working with you can make a file called uploadvideo.pi and paste in that code okay now like i said um if i i've noticed because i'm familiar with python that there are some this this python code right here let's look at it on the web page is written for python version two and you can immediately tell that by the print statements okay so where's the print statement here we go print they're saying print without any parentheses so that tells me that this is python version two so we're gonna have to come through here and fix that to be to update to basically upgrade this code to python version three so um let's find all the print lines here and sorry this is a manual process but it's again once and done thing to fix this code so it's python 3 compatible i don't think there's too many of these instances but we'll make sure we get them all so that because if we don't do this then when we execute the python program it's going to give us an error say that there's a syntax error and we don't want that so okay so there's five or six different instances of that that i just fixed um also there's uh these exceptions right that are not python 3 compatible so i think if we look for error http error except http error for python 3 that has to be as e right um what else do we got yeah this one has to be changed to as um i think that's it let me see look at the other exceptions uh this one would probably change to as e and okay so i think that's good um there's something else i've had to change here uh oh yeah http lib i don't think that's python 3 compatible so get rid of this first line here uh we only want to use http lib2 and then get rid of these references down here to http live so that's this line this line this line and this section here so i i'm not saying this code is going to be completely like uh uh production ready but it uh it will get us to interact with the api which is the goal here okay so i don't is there any other references to that nope everything else is http 2. um and now like i said there's one other package that we have to work to install to be compatible with python 3 and that is the oauth 2 client package so go ahead and install that that'll be really quick and now we're finally ready to upload a video to the youtube api via python so let's go ahead and do that we'll open this just so we can keep an eye on it as that's happening in the background and actually we'll minimize that for a second i have this video on my desktop uh helloworld.mp4 it's just like a 10 second video of me sitting here in the studio and that's what we're going to be uploading to youtube so as it says here actually back here it gives you some sample commands that you can execute uh right here so python you're calling the python program upload.pi and you're passing it in these parameters okay so just to save some time i've already copied that i mean i'll copy it and paste it into the terminal window here and i'll walk you through it so we're going to execute the upload.pi file that we just created the file name that we're going to upload is on my desktop it's called helloworld.mp4 the title of the video on youtube is going to be hello youtube data api like comment subscribe um the description of the video on youtube is going to be the results of uploading my first video to the youtube to youtube via api the keywords keywords i will point that out are going to be youtube api hello world like comment subscribe category is going to be 22. that's an id associated with one of the categories and then the privacy status is private so it's not going to be public by default so when we execute this fingers crossed if we got everything right it should start the upload to youtube up i forgot we have to sign in like i talked about in the beginning of this video we have to um tell youtube where to upload this to which who whose account are we going to upload this to it's going to be tony teaches tech gmail.com so we're giving uh i should go back a second here this is the name that we gave to our application in the developer console python youtube app so that's where that comes full circle that's how that's uh coming into the picture here so we're asking the python youtube app is seeing where am i going to upload this video to we're going to upload it to tony teaches tech gmail.com and again because this isn't verified by google it's just a developed app at this point we know the developer it's myself so we can go ahead and continue with the unverified app and it's asking us about the permissions remember we explicitly said the scope of the youtube api is going to be uploading the video so that's why you see that here they're going to allow that and one final confirmation here to manage your youtube videos hit allow authentication flow has completed if we go back here uh let's we've got to be quick here um uploading file alright so our program is uploading the file to youtube it shouldn't take that long and if we refresh the page back here on youtube we should see start to see some of that happening there we go processing will begin shortly let's see if the the terminal window has finished still uploading uh it's finished so the video id nl446 ends in queue uh i'll show you where that comes into the picture so if you go back here uh go to the content which we're already at the content um go to the details for this you'll see that the the the information that we passed in on the command line hello youtube data api like comment subscribe that's the title uh we got the description um down here under show more we have the the tags that we associate it with this and somewhere the category so the category with id of 22 is people and blogs there you can look that up somewhere else that's that's uh these are predetermined categories that have an id associated with them um so that's all the information that we pass it and of course the youtube the video file itself which is now has a visibility of private but if we wanted to we could make this public uh very easily so um yeah that's that's pretty cool i the first time i did this i was really uh uh impressed that youtube gives us as users the capability to do this because um you know that's that's very powerful especially for somebody like me who produces multiple videos per week uh integrating it building a project for myself program that will upload videos uh so we don't have to manually do it is extremely helpful in my opinion now before we wrap up this video i just want to point out a couple other things if you do oh uh the video id the nl446 uh that is part of the the url for your video so that's where that comes into the picture um but anyway like i was saying um the actual i'll show you over here this code is depending on command line arguments right as you saw so dash dash file dash dash title dash description these are command line arguments and these are being parsed by arg parser if you're familiar with that so arg parser is taking all these variables uh in from the command line and actually making them into variables in python so if you want to see an example of how you can you don't need to use arg parser to do that if you just want to pass them in via a function like via a function parameters i actually wrote an entire program called video flow on github which you can check that out github.com tonyflow video flow and in here uh this this taps into the youtube api you can try it out if you want um this is this is my workflow for tony to just tech how i upload videos uh to youtube from this point forward and you can come through look through some of these python scripts to see how i interact with the youtube api uh without passing in command line parameters or anything like that so i just wanted to make you guys that that is available for you to look at free on github check it out follow me on github um and yeah definitely subscribe to me on this channel too because uh i like making content like this this stuff like this excites me um hopefully it excites you too because if it does that's why you should subscribe so you can see more videos like this from me in the future give this video a thumbs up if you liked it and if you do end up subscribing i will see you in the next video [Music] you
Info
Channel: Tony Teaches Tech
Views: 4,682
Rating: undefined out of 5
Keywords: youtube api, youtube api key, youtube data api, youtube api python, youtube api example, youtube video api, youtube api upload video, youtube api new video, youtube upload api, youtube api upload video python, upload youtube video api, youtube api v3 upload video, youtube upload api v3, api youtube upload video, Add an item
Id: aFwZgth790Q
Channel Id: undefined
Length: 22min 58sec (1378 seconds)
Published: Tue Jul 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.