Reputation: 393
I am using this youtube api sample, to get duration of my uploaded videos. In this Resource representation https://developers.google.com/youtube/v3/docs/videos#snippet I can see structure of json, but can't get this part
Currently I am managed to get contentDetails with 'videoPublishedAt' it looks like this ({u'videoPublishedAt': u'2013-03-13T00:05:41.000Z', u'videoId': u'6PKHl3Kvppk'})
I added 'contentDetails' to 'part'
playlistitems_list_request = youtube.playlistItems().list(
playlistId=uploads_list_id,
part="snippet,contentDetails",
maxResults=50
)
And then changed video_id = playlist_item["contentDetails"]
in last section while playlistitems_list_request:
But video_id = playlist_item["contentDetails"]["duration"]
give KeyError: 'duration'
Here is full code without authentication part and imports. Full version could be found here https://github.com/youtube/api-samples/blob/master/python/my_uploads.py
# Retrieve the contentDetails part of the channel resource for the
# authenticated user's channel.
channels_response = youtube.channels().list(
mine=True,
part="contentDetails"
).execute()
for channel in channels_response["items"]:
# From the API response, extract the playlist ID that identifies the list
# of videos uploaded to the authenticated user's channel.
uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
print "Videos in list %s" % uploads_list_id
# Retrieve the list of videos uploaded to the authenticated user's channel.
playlistitems_list_request = youtube.playlistItems().list(
playlistId=uploads_list_id,
part="snippet,contentDetails",
maxResults=50
)
while playlistitems_list_request:
playlistitems_list_response = playlistitems_list_request.execute()
# Print information about each video.
for playlist_item in playlistitems_list_response["items"]:
title = playlist_item["snippet"]["title"]
video_id = playlist_item["contentDetails"]
print "%s (%s)" % (title, video_id)
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response)
print
Upvotes: 0
Views: 1285
Reputation: 17613
Here's how to get the duration. I'll just give you the Try-it using the Youtube API Explorer Videos.list and just implement it on your code.
I supplied the parameters for id which is the videoId of your youtube vid and contentDetails for part.
A successful response returned the duration of my video along with other metadata:
"contentDetails": {
"duration": "PT1M28S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": false,
"projection": "rectangular",
"hasCustomThumbnail": false
}
Here, it's 1 minute and 28 seconds. Check Youtube Videos.list for additional reference.
Upvotes: 3