Youtube API v3,如何获取视频播放列表的持续时间?

时间:2017-02-24 07:51:03

标签: video youtube google-api

我已尝试通过此GET url

获取视频时长的方法

并传递了这个参数

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我只需按代码查看播放列表的总持续时间。 是否有任何方法,我可以播放播放列表ID并获得总持续时间。

1 个答案:

答案 0 :(得分: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);
?>