Youtube API - 从播放列表中获取视频的持续时间

时间:2018-03-27 06:00:46

标签: php optimization foreach google-api youtube-api

我需要获取Youtube播放列表中所有视频的持续时间。 我知道API在搜索所有视频时没有显示每个视频的持续时间,但是如果查询是针对特定视频的话,它会显示它。

通过PHP,我尝试从播放列表中收集所有ID,然后分析每个ID以从视频中获取数据,但脚本太慢了,尽管应该强调它运行良好,有没有办法优化它?...

    function youtube_automusic($listas, $api_key, $resultados){
        $nresultados = $resultados;
        $lista_reproduccion_random = $listas;
        $lista_reproduccion = $lista_reproduccion_random[array_rand($lista_reproduccion_random)];
        $url_playlist = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&fields=items(snippet(resourceId(videoId)))&type=video&videoCategoryId=10&maxResults=".$nresultados."&playlistId=".$lista_reproduccion."&key=".$api_key;
        $data = dlPage($url_playlist);
        $data_decode = json_decode($data, true);

        $number_song = 1;
        $respuesta = array();
        foreach ($data_decode as $items){
            foreach ($items as $item){
                $lista_ids =$item['snippet']['resourceId']['videoId'];
                $url_video = "https://www.googleapis.com/youtube/v3/videos?id=".$lista_ids."&part=snippet,contentDetails&fields=items(etag,id,snippet(publishedAt,title,thumbnails(default(url)),tags),contentDetails(duration))&key=".$api_key;
                $data_video = dlPage($url_video);
                $data_video_decode = json_decode($data_video, true);
                foreach ($data_video_decode as $items_videos){
                    foreach ($items_videos as $item_video){
                        $data_final = array(
                            'etag' => $item_video['etag'],
                            'idvideo' => $item_video['id'],
                            'titulovideo' => $item_video['snippet']['title'],
                            'thumbnail' => $item_video['snippet']['thumbnails']['default']['url'],
                            'duracion' => $item_video['contentDetails']['duration'],
                            'videoplay' => $number_song++
                        );
                        array_push($respuesta, $data_final);
                    }
                }   
            }
        }
        return json_encode($respuesta);         
    }

2 个答案:

答案 0 :(得分:2)

将代码放在50项播放列表上,需要51个API调用。

不是对播放列表中的每个视频执行单个视频请求,而是首先获取播放列表中的所有视频ID,然后一次最多50个视频请求(ID参数采用以逗号分隔的列表至50项)。

然后一个50项播放列表只需要2个API调用。

应该快得多。

答案 1 :(得分:0)

我刚刚进行了测试here

请求:

  

获取https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails&id=Ks-_Mh1QhMc&fields=items(etag%2Cid%2Csnippet(publishedAt%2Ctitle%2Cthumbnails(default(url))%2Ctags)%2CcontentDetails(duration))&key= {YOUR_API_KEY}

结果:

{
 "items": [
  {
   "etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/aCBUdsaX0W34z3It8a8FCh5uteo\"",
   "id": "Ks-_Mh1QhMc",
   "snippet": {
    "publishedAt": "2012-10-01T15:27:35.000Z",
    "title": "Your body language may shape who you are | Amy Cuddy",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/Ks-_Mh1QhMc/default.jpg"
     }
    },
    "tags": [
     "Amy Cuddy",
     "TED",
     "TEDTalk",
     "TEDTalks",
     "TED Talk",
     "TED Talks",
     "TEDGlobal",
     "brain",
     "business",
     "psychology",
     "self",
     "success"
    ]
   },
   "contentDetails": {
    "duration": "PT21M3S"
   }
  }
 ]
}

我建议您使用Google API资源管理器使用您遇到问题的视频ID运行相同的请求,以验证API不存在问题,而不是返回您的持续时间。