2017-02-24 107 views
1

我試圖讓視頻持續時間的方法,通過這種GET urlYoutube API v3,如何獲取視頻播放列表持續時間?

,並通過這個參數

https://www.googleapis.com/youtube/v3/videos? 
id=QoQ_dHCi-08& // video id 
list=RDUZWrltQLTzE& // playlist id (TODO from this i want full duration) 
part=snippet,contentDetails& 
key={MYAPIKEY}& 
format=json& 
fields=items(snippet(title),contentDetails(duration)) 

在這之後我只收到視頻時長不能播放列表總時間 也看到了方法,他們正在使用鏈接:http://youtube-playlist-analyzer ... i am getting total duration here but i want my own code to get this,我只需要通過代碼查看播放列表的總持續時間。 是否有任何方法,我可以paas播放列表id並獲得總持續時間。

回答

0
<?php 
    $result = array(); 
    function callone($playlistId, $nextpagetoken=''){ 
     $apikey = 'YOUR_API_KEY'; 
     $dur = file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?playlistId=".urlencode($playlistId)."&part=snippet&maxResults=50&key=$apikey&pageToken=$nextpagetoken&fields=items(snippet(resourceId(videoId))),nextPageToken"); 
     $youtubeInfo = json_decode($dur, true); 
     $sum=0; 
     for($i=0; $i<count($youtubeInfo['items']); $i++){ 
      $sum++; 
      $result[] = $youtubeInfo['items'][$i]['snippet']['resourceId']['videoId']; 
     } 
     echo '-'.$sum.'-'; 
     if(isset($youtubeInfo['nextPageToken'])){ 
      var_dump($youtubeInfo['nextPageToken']); 
      $result = array_merge($result, callone($playlistId, $youtubeInfo['nextPageToken'])); 

      unset($youtubeInfo['nextPageToken']); 
     } 
     return $result; 
    } 

// PLh-evOzpQ6aXxmdYLdHa2VjzkMA9340NO -> 20 songs 
// PLMC9KNkIncKtPzgY-5rmhvj7fax8fdxoj -> 176 songs 

$mani = callone('PLMC9KNkIncKtPzgY-5rmhvj7fax8fdxoj'); // playlistId with 176 songs 
echo "<pre>"; print_r($mani); 
?> 
相關問題