12

I'm using this url:

https://www.googleapis.com/youtube/v3/playlistItems?
    playlistId=FLFe0SGNFqZ9E2owO5ZDZpeg&
    part=snippet,contentDetails,status

to fetch YouTube playlistItems.

I included all the part I'm interested in, however I found no way to return the duration of the item.

https://developers.google.com/youtube/v3/docs/playlistItems/list

Is this possible?

currently I'm getting back only an item like this:

{
  "status": {
    "privacyStatus": "public"
  }, 
  "kind": "youtube#playlistItem", 
  "contentDetails": {
    "videoId": "tL-Ba86UhoE"
  }, 
  "snippet": {
    "playlistId": "FLFe0SGeFqZ9E2owO5ZDZpwg", 
    "thumbnails": {
      "default": {
        "url": "https://i1.ytimg.com/vi/tL-BA86Uhoh/default.jpg", 
        "width": 120, 
        "height": 90
      }, 
      "high": {
        "url": "https://i1.ytimg.com/vi/tL-BA86Uhoh/hqdefault.jpg", 
        "width": 480, 
        "height": 360
      }, 
      "medium": {
        "url": "https://i1.ytimg.com/vi/tL-BA86Uhoh/mqdefault.jpg", 
        "width": 320, 
        "height": 180
      }
    }, 
    "title": "Music Video", 
    "resourceId": {
      "kind": "youtube#video", 
      "videoId": "tL-BA86Uhoh"
    }, 
    "channelId": "UCFe0SGNFqZ9E2owO5ZDZpwg", 
    "publishedAt": "2013-07-06T18:41:43.000Z", 
    "channelTitle": "channeltitle", 
    "position": 0, 
    "description": "Video for"
  }, 
  "etag": "\"ePFRUfYBkeQ2ncpP9OLHvB0fDw4/CJiCG6tvFw4quQlzJq3gTrhvCNo\"", 
  "id": "FL-fX6VqgtTfCJWKNE4aVRODKSrRgEdGvw"
}, 

With no time indication at all. I'd rather avoid flooding YouTube with request to return a single video's duration.

John Smith
  • 1,726
  • 2
  • 18
  • 30

2 Answers2

3

Use link like this for get duration for many videos:

https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=idFirst,idSecond&key=YOUR_API_KEY
Max
  • 182
  • 13
0

Use this Function : [ only used with Youtube Api v3 ]

Browser :

you have to make another http request using :

https://www.googleapis.com/youtube/v3/videos?id={video id}&part=contentDetails&key={api Key}

and on

contentDetails

you will find the duration

Php :

function getDuration($apiKey,$video_id){        
    $link='https://www.googleapis.com/youtube/v3/videos?id='.$video_id.'&part=contentDetails&key='.$apiKey;
    $data = json_decode(file_get_contents($link),true);
    $duration =  $data['items'][0]['contentDetails']['duration'];   
    return $duration;
 }
Yahia
  • 691
  • 1
  • 7
  • 24