# Python YouTube API Tutorial: Calculating the Duration of a Playlist

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=coZbOM6E47I
- **Дата:** 10.06.2020
- **Длительность:** 37:38
- **Просмотры:** 62,671
- **Источник:** https://ekstraktznaniy.ru/video/11748

## Описание

In this Python Programming Tutorial, we'll be learning how to calculate the duration of a YouTube playlist. The duration of a playlist is likely one of the first things people will look for before watching, but YouTube doesn't currently have this information available on their site. So we will use the API to calculate this for us. Let's get started...

The code for this video can be found at:
https://bit.ly/YouTubeAPI-02

YouTube API Tutorial - https://youtu.be/th5_9woFJmk
Regular Expressions Tutorial - https://youtu.be/K8L6KVGG-7o
Datetime Tutorial - https://youtu.be/eirjjyP2qcQ

YouTube API Docs - https://developers.google.com/youtube/v3
Google API Python Client - https://github.com/googleapis/google-api-python-client

✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 

## Транскрипт

### <Untitled Chapter 1> []

hey there how's it going everybody in this video we're going to be using Python and the YouTube API to calculate the duration of a YouTube playlist now this is something that you would assume would be included within YouTube itself but for some reason playlists on YouTube don't tell you how long a playlist actually is to show you what I mean by this I have my Django tutorials open up here and this is my Django playlist and you can see that it gives me a you know the number of videos when it was last updated and things like that but it doesn't show you how long it would take to actually watch all of these videos and I feel like that would be one of the first questions that people might have when they go to a playlist is how much time would it take me to watch this entire series and learn this and this is something that I've run into a lot personally if I'm watching a YouTube course or something like that and want to know how long that course actually is then there's currently no way to access this information from within YouTube itself so we're going to use Python and the YouTube API to look at each video within a playlist and add up the duration of each video to see how long that playlist actually is so let's go ahead and get started now if you haven't watched my video on getting started with the YouTube API using Python then definitely watch that video first because that's where I show how to get a YouTube API key and connect to this YouTube service within Python so that we can start grabbing information and I'm using the script that we ended with in that video to get started okay so now that we have the API working here within Python now we can use this API to grab just about any public information that we want so like I said we're going to be using a script that calculates the duration of videos in a specific playlist so first I'm going to need to access a playlist and I'll grab just one of my playlists in order to get I can do a query to the API now you can also go to a specific playlist here and if we look up in the URL then it gives the playlist ID here within the URL but let me show you how to also do this within the API so to get a list of my playlists I'm gonna need my channel ID so I'll query my channel again here within this script but instead of only accessing the statistics here I'm also going to pass in content details and this will give us more information about a specific channel and if you don't know all of the arguments that we can pass into this part argument here then we can always go back to that channels reference and the documentation and it tells us everything that we can query there and again I covered that in the getting started video okay so if I run this then it should give me some information about my channel here and one of these pieces of information should be my channel ID okay so I can see here that we have this ID tag and this is my channel ID right here so I'm gonna copy that and now that I have my channel ID I'm going to list all of my playlists for my channel and I'll be making multiple requests in this video so I'm going to change the variable name here to tell these requests apart so I'm gonna call this PL underscore request instead and now I want to query for

### Query for Playlists [3:10]

playlists so instead of channels I'm gonna change this to playlists and now this doesn't have a four user name I don't believe but I'm gonna use the channel ID here instead and I'll just pass in my channel ID and now since I changed the name of my request here to PL underscore request I'm gonna change that as well and I'm also going to make this PL underscore response as well so let's print this out whoops and I got an error here because they don't have a statistics here for the parts that you can pass in again we can look at the documentation for this but I'm going to grab the snippet of that playlist instead of these statistics for that playlist so if I run this then I should get some details and snippets of my playlists and this is going to return the first five playlist on my channel if we want to grab more than a single page of values from a response then we'll see how that we can do that in just a bit but if I look down here in the output then I can see that it returns a dictionary and my playlists are in a list of values within this items key here so if I wanted to loop over these then instead of just printing out the entire response I could say for item in PL underscore response and access that items key to loop over those and now I'm just going to print out each item and then I'll put in another print statement here as well so that we have a line between each of these so now if I scroll up here now this is going to give us information specific to each playlist now I know that this might be a bit difficult to read here but if I look here at the title of this top one here then we can see that this one is my Panda stat oral playlist and if I look through this information then there won't be a total duration for this playlist it gives me you know the URL and things like that but the duration isn't within this information either so I'll just use this pandas tutorials playlist here and let's go ahead and calculate the duration of this pandas playlist to see how this is done and then I'll show how we can do this for any playlist so to do this let's just loop through all of the videos in this playlist and keep a running total of the duration of each video so to do this I'm going to grab the ID of the playlist so that I can loop through that playlist videos and it's here close to the top of this information here we can see this ID tag here this is the ID of this pan this playlist so now I've got that playlist ID copied there and now let's loop over the videos of this playlist and we can do this by changing our API query here and instead of you know going out to get a channels playlist I'm going to get playlist items and I'm going to remove snippet here because we're only going to want the content details of these playlist items and now instead of channel ID this will be playlist ID and now I'm going to paste in the ID for that playlist and now let me run this and see what we get for our result here ok so we're looping over the items in this response let me scroll down here a bit and this is the content details about the videos in the playlist and actually this is also just the first vibe five videos but we'll see how to get all the results here in just a bit but first let's look at the information that we're getting back for these videos so we can see that we're getting a video ID or here's the video ID here when the video was published but unfortunately this doesn't have the duration of each video listed it within here either so instead within this loop we're actually going to need to query the video resource from the API with it with each video ID to get more detailed information about each video so when I'm moving looping over the videos in a playlist let's only grab the video IDs and we can see right now each of these are within the dictionaries here within our information so the video ID here is actually within this key this content details key and that key has a value here of this information and that this is also a dictionary so we first need to access the content details key and then the video ID key within there so in order to do that I'm just going to print out for each item and this is an entire item here I'm gonna grab the content details key and then I'm going to grab within that value the video ID key here as well so now let me actually make this its own variable so that it's a little easier to see here so I'm gonna call this vid underscore ID and set this equal to that and now let's print that out and now if we run this now we're getting the first the video IDs of our first five videos within this pandas playlist so now that we're getting the video IDs for these first five videos let's go ahead and write the code to tally up the duration of these first five videos first and then we'll learn how we can then loop over all over the pages to get all of the videos for this playlist so I can grab additional information about these videos by sending a request to the videos resource using the API and we can automatically use multiple video IDs at once when making the query just to cut down on the number of calls we're making to the API so I'm gonna go ahead and add all of these IDs to a list so to do this I'm just going to create a list here above this for loop and call this vid IDs and set this equal to an empty list and now I will just append each of these video IDs to that vid IDs list now I could have done this using a list comprehension but our for loop works just as well okay so now outside of this loop I'm going to write a single query to the videos resource to grab all of these videos now I can't just pass in a Python list like we have here with the video IDs it actually needs to be a string of comma separated values of all the video IDs that I want to get information about so to get a comma

### Get a Comma Separated String from a List [10:01]

separated string from a list we can use the string join method so to see what this looks like we can specify a string so I can say you know a comma and then do a dot join on that string and then pass in a list so I'll pass in my list here of vid IDs and what this will do is it'll take all of the items in this list and separate them with a comma so if I run this then we can see that now we have all five of these video IDs as a string that are comma separated so now I'm gonna use this here and I'm going to

### Query the Videos Resource Using the Api [10:43]

query the videos resource using the API so I'm gonna have a new request here so I'll call this bid request which is different than our playlist request up here and now we're going to use that YouTube service and access that videos resource and we want to do a dot list so that we can list out information on these and again this takes a part argument and again I'm just going to type in content details here just to get some details of each video and now we can pass in the ID of the video that we want and we can pass in a comma-separated value of multiple IDs so I'm going to paste that in there now there is a limit of how many video IDs you can pass in which I believe is 50 and if we have more than 50 IDs then we'll see how to work with that here in just a bit I do have a couple of playlists that have more than 50 videos okay but for now let's see what we get for a response here so I'm gonna say vid response is equal to vid request dot execute and now just like we did earlier that should give us a response for our video items so let's loop over that response just like we did by saying for item in vid response and access that items key that would get back from the API and then I'll just print out each item and then I'll also leave a blank line after each item to kind of break that up a bit okay so now for each of these videos now we can see we're getting some more detailed information from this videos resource okay so again we have the video ID here and within the content details we can see that this time we are getting the duration here it's also given us the dimension and definition and things like that now in this video we are specifically interested in the duration so let me only grab that item from each of these results here so that is within this content details key and within there it's within the duration key so if I again I will capture this in its own variable here so I'll say that is the item then I want to access the content details key here I'll just copy and paste this so I don't miss type any and then within content details I want to grab at the duration okay and now let's print out this duration and now let's go through and actually print these out okay so we can see that this is a bit weird of a format here for a duration so we can see here that it starts with the letters of PT I'm not sure what that means it could possibly stand for play time or something like that and then we have a number here followed by an M so I'm assuming that is the number of minutes and then there is another number here followed by an S so I'm assuming that is the number of seconds so we need to parse these out so that we can tally up the duration of the entire playlist so to actually parse these out I'm going to use regular expressions now I know regular expressions can be a little intimidating for some people but we're going to be using very simple expressions in this video and I'll explain exactly what we're doing along the way but if you would like to know more about using regular expressions in Python then I do have a video on that topic specifically so I'll be sure to leave a link to that video in the description section below for anyone who's interested so to begin writing our

### Regular Expressions [14:37]

regular expressions I'm going to first import the regular expressions module at the top of the script so up here I will import our e and now I'm going to compile a few different expressions so first we know that the let's look at our pattern here again we know that the number of minutes is a number in this string that is followed by an M so to write this expression I will go ahead and put this up here right above our response actually let me just put this above the for loop here instead and I'll minimize this so we can see a little bit better well and actually this is the wrong for loop pair I want to be down here okay so let me create this expression right here so I'm gonna call this minutes pattern and set this equal to and now we're going to compile a regular expression so we can do re dot compile and now for our regular expression let's pass in and R and that just allows us to pass in certain text without python interpreting it in a certain way and now I'm going to do parentheses here and these parentheses allow us to capture a group of data okay so what do we want to capture well a digit so that is backslash d and also this could be more than one digit so down here we have a 23 so if we put in a plus sign here then that plus sign actually means that it's going to capture one or more digits okay so now what digits do we actually want to grab here well it's going to be all of the digits that lead up to an M so when we search through our regular expression pattern here it's going to say okay I'm have this group here of one or more digits before an M so when it searches through it should find oh okay I have 23 here are my digits before and M and we can basically do the exact same thing with seconds so if I copy this pattern here and I'm just going to call this seconds pattern this we want to grab the digits before a capital S okay and finally I know that none of these videos that are printed out here now showed this because they weren't long enough but I've written a script like this previously and I know that if the video is over an hour then it will also follow this same logic but instead it will have a number that is followed by a capital H so I'm going to make an hour's pattern as well so we'll paste this in here we will call this hours pattern and these are going to be digits followed by a capital H okay so now that we have our regular expressions compiled that can find our hours minutes and seconds and again if these are confusing to you then I do have a more in-depth video on using regular expressions within Python and I will that in the description section below okay so now let's go ahead and search for these values within our duration sprint string and print these out so we can do this here within the loop after we get the duration then I'm gonna come down here and I'm just going to say okay our hours are going to be equal to our hours pattern dot search and I want to search this duration okay and we're gonna do this for both or for all three of hours minutes and seconds so we'll do minutes here and we'll have this be minutes pattern seconds pattern and we will call this seconds and now let's print out all three of those hours minutes and seconds okay so now if I save this and run this and print this out then let's take a look here at what our results are so let's just look at this first result here so four hours it's actually printing out none we got no results four hours since this video is less than an hour and it didn't match anything in that string for that value and then we can see that our second value here we did get a match it tells us the span of that match which are which characters in the string matched our regular expression and also it gives us the match itself so these are we can see here which is 23 minutes and we can also see that we got a match for seconds as well with the same information where it can be found in the string and also what that match is okay so if you remember what I said above about our regular expressions we captured just the digits in a specific group so that we can parse those digits out specifically and out by accessing the group of these results here so for example above my print statement here if I was to instead say okay these minutes I instead just want these to be minutes dot group and grab that first group and this would give us the string of twenty-three instead of the entire match of 23 M for the minutes now I don't want this to be a string I actually an integer so I can also convert this to an integer just by wrapping this in the int built-in function here okay and this should work here so if I instead just print out these minutes instead of everything then okay we can see that we got 23 33 17 23 40 okay that looks good but there's actually one more thing that we need to do here because if we think about this then we need to remember that it is possible for us to not get a match for some of these regular expressions so for example our hours searched that search returned none since those videos were all less than an hour and if we tried to grab the group of a none value then it's going to give us an error saying hey this is none you're trying to access the group of a none value I don't know what you want me to do so we need to put in a conditional in place so that if our match is equal to none then we'll just assign a value of zero instead now you can use a regular if statement here if you're more comfortable with those but I'm going to use something called a

### Ternary Conditional [21:27]

ternary conditional here since it just makes it's a bit more simple in this case and it'll also make it a bit easier to write so with a ternary conditional we can just say okay I want this value here if minutes because if minutes is none then it won't be equal to this value we can put in an else zero so just to read this over one more time we're saying I want this value here if minutes and minutes just means that it has some value if it's none then it'll go to this else statement here and assign a zero again that's called a ternary conditional if you're not familiar with those are not comfortable then you can also just use you know a regular FL statement so now I'm also going to do this for hours and seconds and whoops I almost forgot here I need to replace this part here within the ternary conditional as well okay so now let's print out these hours minutes and seconds again to see what we are getting now so if I run this okay then we can see that this looks a little cleaner now we can see that for all these we have zero hours 23 minutes one second 33 minutes and so on so now we're getting the hours minutes and seconds for each video and now we need to tally up all of these video times and I think the easiest way to do this will be to convert the video time entirely into seconds and then we'll tally up all of the seconds and then convert them back to hours minutes and seconds once we've added all those up one way that we could convert these to seconds would be to use some simple math by you know multiplying by 60 and things like that but an even easier way would be to use the time delta class from the date/time library so let me import this here up at the top and I'll show you how this works so I'm going to say from date/time we want to import time Delta and now let me scroll back down here and we will see how to use this underneath here after we've calculated the hours minutes and seconds in order to get the total number of seconds for each video I can just say video underscore seconds is equal to and then we want to create a

### Time Delta [23:58]

time delta so within this time delta i want to say that the hours are going to be equal to hours here and we'll do the same thing for minutes and seconds so let me pass these in here okay and I don't need that trailing comma and now here at the end after we have this time delta now that it's created this Delta the time Delta has a total seconds

### Total Seconds Method [24:28]

method that we can run on this time Delta object that will give us the total seconds of everything that we just passed in so now let me print out video seconds instead of just hours minutes and seconds so I'll run this and now we can see that we are getting you know all of the total seconds for each of these and if you wanted to check this then you can open up a calculator so 13 80/60 a little over 23 minutes which is what that first one was so that seems to be working okay so we're definitely making progress here so now we're getting the total number of seconds for each video now remember we're only getting five videos from this playlist right now so let's see how we can actually get all of the videos from this playlist and then we can tally up all these seconds so to do this we're going to need to use page tokens which

### Page Tokens [25:23]

will allow us to get all of the results one page at a time now each page is returned gives us a reference to the next page and we can keep track of which page we're on using that page token so what I'm gonna do here I'm gonna scroll up to where we first made our video request for the videos of a certain playlist whoops and this is my video request here I actually want my playlist request that's why I named those differently there now what I'm gonna do here is I'm going to create

### Infinite Loop [25:58]

an infinite loop that keeps grabbing pages and so there are no more left and then it'll break out of that loop once we're out of pages so this is going to be a very long or this while loop is going to take up a lot of code so I'm just gonna say first I'll create something called a next page token and we'll see what this does here in just a second and for now I'm going to set this to none and then I'm gonna say while true which is an infinite loop now I'm gonna take all of the code that we've written so far to grab these videos and calculate the seconds and everything and I'm going to press tab to indent all of that within hour while loop here so that we can keep grabbing videos and doing these calculations as long as there are more videos left in the playlist so to do this we can pass in a few different arguments here to our playlist request now we can also pass in an argument for Max results so that we can get more results per page so that we don't make as many requests to the API so I'll just pass in the max number I can for max results and I believe that is 50 so I'll say max results is equal to 50 and I also want to pass in the page token to let the API know what page of results were currently on and now the first time through this loop our next page token is going to be equal to none which will just give us the first page of results so I can say page token this is a capital T here page token is equal to next page token and again the first time through that'll be none which will just give us the first page of results okay and after we get a page of results then we want to execute that and do everything that we did before get all the video IDs from that page make a video request using those video IDs parse out the hours minutes and seconds and then finally down here at the bottom once we have calculated the total seconds for each video then we need to have a way to break out of that infinite loop if we don't break out of that loop then it'll just go on forever so we're going to break out of the loop once there are no more pages left to query so to check this I'm gonna write some more code here now realize here that I'm not within this for loop anymore i back spaced here to where I'm no longer in that for loop so to check whether there are any more pages left we can use this next page token and set this equal to the playlist response remember we want the playlist response not the video response and we just want to get a string here of next page token and what this will do here is it will update our next page to Okin that tells the YouTube API what page of results were trying to fetch and if there are no more pages than this then this will just return none so if this is none after we try to fetch that token then we can just break out of the loop it means that we've exhausted all of the pages so I can just say if not next page token then just break out of the loop now actually before I run this I want to move a few things outside of our infinite loop now that we are inside of a loop here it doesn't make any sense compiling these regular expressions every time through the loop we only need to compile these once so I'm going to cut these out and let's move these here to a section above the while loop so I will just compile these up here at the top and I don't think there's any more code in here that needs to be removed from the while loop I think everything else is good yeah okay so if I didn't mess anything up here then if I run this then it should print out the total section the total seconds for every video in my playlist so if I look through here we can see that we're getting more than five I believe that there's 11 videos total and my pan is playlist so we got 2 3 4 5 6 7 8 9 10 11 okay yeah it seems to have gotten all those videos okay so now at this point all we need to do is add up all of these times for all these videos and then convert it back to hours minutes and seconds so above our while loop I'm going to create a variable called total underscore seconds and I'm just going to set this equal to zero and then within the loop here each time through the loop I'm going to remove our print statement since we know that that's working instead I am just going to keep adding to our total seconds for each videos seconds so this will keep a running total of all of these seconds for video and now outside of our loop on our main level here I'm just going to print out these total seconds and see if this looks correct so I'll go ahead and run this and now we can see that we get 19 thousand seconds 151 so that's definitely more seconds than any one video so that probably is getting that complete total okay so this is looking good now the total seconds here probably doesn't mean much to us I don't know about you guys but I can't just read 19,000 seconds and know exactly how long that is so now let's convert these back to hours minutes and seconds so there are a few different ways that we could do this one way that I like to do this is using the built-in div mod function so div mod allows us to divide two numbers and it returns a tuple of the quotient and the remainder so let's see what this looks like I think it'll make more sense so first I'm going to convert my total seconds to an integer because I believe that right now it's a float so just to be sure I want this to be an integer so I'll say total seconds is equal to and cast that to an integer there okay so first I'm going to use div mod to get

### Div Mod To Get the Number of Minutes and Seconds [32:23]

the number of minutes and seconds so if we were to say div mod and passed in the total seconds here and then what we wanted to divide total seconds by we want to divide it by 60 to give us the number of minutes so what div mod does is it returns a tuple of the quotient which was how many times 60 went into our total seconds and then it also the second value is the remainder which would be the number of seconds so just to show what this looks like we can say minutes and seconds is equal to that returned to pool so just to show you what this returns here so this is taking 19,000 roughly 19,000 seconds here if I print this out print out the number of minutes and the number of seconds then what this did here was that div mod said okay 60 goes into that 19,000 number 319 times which is 319 minutes with 11 seconds left over so now in order to get the number of hours now we can just take this minutes here and do the exact same thing we can divide it by 60 and say okay however many times 60 goes into those number of minutes that's how many hours we have so I can pretty much just do the same thing I did here but instead I'm going to say that now this is hours and minutes and we want to divide the minutes by 60 and anything left over is going to be left over minutes so now let's just print out these values so you could just print these out as you know hours minutes and seconds and if I run this then we can see that we got okay that pandas playlist is 5 hours 19 minutes and 11 seconds and you can format this however you like if you want to use an F string then we could say you know I want hours minutes and seconds here but I want them separated by a colon instead so if I save this and run it here then we can see that that's more of a standard time format so we have 5 hours 19 minutes 11 seconds now just to make sure that this is working with the pagination on the videos now let me try to run this script on a much larger playlist so if I open my browser back up I also have my Python tutorials playlist open here and we can see that this is a hundred and forty videos instead of just 11 videos and again if you ever want to get the ID of

### Get the Id of a Specific Playlist [35:09]

a specific playlist then you can grab it up here in the URL it'll say list is equal to and then have the ID here so I'm gonna grab that ID and open up our script here and now I'm gonna paste in my Python tutorials playlist into this playlist ID instead of that panda series so now if I save this and run it then that obviously took a little bit more time to calculate because it was going through those pages in the playlist and all those videos and making those queries to the API but it gives us a result here of 60 hours 26 minutes and 37 seconds so that's good information to know it's good to know that if you want to watch my pandas playlist then that would be five hours and something if you want to watch the all of the Python videos and there's about 60 hours of content on there so I've always thought that that's very good information to know and I use scripts like this all the time whenever I am you know trying to learn something and watch a YouTube course I want to know how long that course is so you know you don't only have to use a script like this on my playlist you know you can go out to any playlist grab that playlist ID paste it in to your script here and make those calculations for yourself okay so I think that's gonna do it for this video hopefully now you feel like you have a pretty good idea for how you can access playlists using the YouTube API and access videos using video IDs and you know use that information to make some calculations yourself again I use this all the time personally and I think it's extremely useful being able to access the API in this way and get this type of information but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest way is to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon or YouTube and there are links to those pages in the description section below be sure to subscribe for future videos and thank you all for watching you
