Youtube API v3,如何获得video持续时间?

我正在使用Youtube API v3来searchYouTube。

https://developers.google.com/youtube/v3/docs/search

正如你可以看到的响应JSON不包含video持续时间。 有没有办法获得video持续时间? 最好不要再为结果中的每个元素调用API(除非这是获得持续时间的唯一方法)。

在进行search调用之后,您将不得不打电话给YouTube数据API的video资源。 您可以在search中放置50个videoID,因此您不必为每个元素调用它。

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

你会想设置part = contentDetails,因为持续时间在那里。

例如下面的调用:

https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails&key={YOUR_API_KEY} 

给出这个结果:

 { "kind": "youtube#videoListResponse", "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/ny1S4th-ku477VARrY_U4tIqcTw\"", "items": [ { "id": "9bZkp7q19f0", "kind": "youtube#video", "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/HN8ILnw-DBXyCcTsc7JG0z51BGg\"", "contentDetails": { "duration": "PT4M13S", "dimension": "2d", "definition": "hd", "caption": "false", "licensedContent": true, "regionRestriction": { "blocked": [ "DE" ] } } } ] } 

时间被格式化为ISO 8601string。 PT代表时间持续时间,4M是4分钟,13S是13秒。

得到它了!

 $dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vId&key=dldfsd981asGhkxHxFf6JqyNrTqIeJ9sjMKFcX4"); $duration =json_decode($dur, true); foreach ($duration['items'] as $vidTime) { $vTime= $vidTime['contentDetails']['duration']; 

在那里它返回Youtube API V3的时间。 [关键是由方式;]我使用了$vId ,我已经从渠道的video的返回列表中,我正在显示的video从… …

它的工作.. :)谷歌真的需要包括在片段的持续时间,所以你可以得到它所有的一个电话,而不是两个……叹……这是在他们的'wontfix'列表。

我使用YouTube API v3编写了以下课程来获得YouTubevideo的持续时间。 (它也会返回缩略图)

 class Youtube { static $api_key = '<API_KEY>'; static $api_base = 'https://www.googleapis.com/youtube/v3/videos'; static $thumbnail_base = 'https://i.ytimg.com/vi/'; // $vid - video id in youtube // returns - video info public static function getVideoInfo($vid) { $params = array( 'part' => 'contentDetails', 'id' => $vid, 'key' => self::$api_key, ); $api_url = Youtube::$api_base . '?' . http_build_query($params); $result = json_decode(@file_get_contents($api_url), true); if(empty($result['items'][0]['contentDetails'])) return null; $vinfo = $result['items'][0]['contentDetails']; $interval = new DateInterval($vinfo['duration']); $vinfo['duration_sec'] = $interval->h * 3600 + $interval->i * 60 + $interval->s; $vinfo['thumbnail']['default'] = self::$thumbnail_base . $vid . '/default.jpg'; $vinfo['thumbnail']['mqDefault'] = self::$thumbnail_base . $vid . '/mqdefault.jpg'; $vinfo['thumbnail']['hqDefault'] = self::$thumbnail_base . $vid . '/hqdefault.jpg'; $vinfo['thumbnail']['sdDefault'] = self::$thumbnail_base . $vid . '/sddefault.jpg'; $vinfo['thumbnail']['maxresDefault'] = self::$thumbnail_base . $vid . '/maxresdefault.jpg'; return $vinfo; } } 

请注意,您需要使用API​​_KEY才能使用YouTube API:

  1. 在此创build一个新项目: https : //console.developers.google.com/project
  2. 在“APIs&auth” – > API下启用“YouTube数据API”
  3. 在“APIs&auth” – > Credentials下创build新的服务器密钥

使用正则expression式获取持续时间(以Python 3testing)

 import re def parse_ISO8601(dur): test1 = rex("PT(.+?)H", dur) if test1: return int(test1.group(1))*3600 test2 = rex("PT(.+?)H(.+?)M",dur) if test2: h,m = [int(x) for x in test2.groups()] return 3600*h+60*m test3 = rex("PT(.+?)H(.+?)S",dur) if test3: h,s = [int(x) for x in test3.groups()] return 3600*h+s test4 = rex("PT(.+?)H(.+?)M(.+?)S",dur) if test4: h,m,s = [int(x) for x in test4.groups()] return 3600*h+60*h+s test5 = rex("PT(.+?)M", dur) if test5: return int(test5.group(1))*60 test6 = rex("PT(.+?)M(.+?)S",dur) if test6: m,s = [int(x) for x in test6.groups()] return 60*m+s test7 = rex("PT(.+?)S",dur) if test7: return int(test7.group(1)) print("CAN'T PARSE FUCKING GOOGLE FORMATTING:",dur) return False #I'm out ... 

此代码通过传递videoID使用Youtube API v3提取Youtubevideo时长,它为我工作。

 <?php function getDuration($videoID){ $apikey = "YOUR-Youtube-API-KEY"; // Like this AIcvSyBsLA8znZn-i-aPLWFrsPOlWMkEyVaXAcv $dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$videoID&key=$apikey"); $VidDuration =json_decode($dur, true); foreach ($VidDuration['items'] as $vidTime) { $VidDuration= $vidTime['contentDetails']['duration']; } preg_match_all('/(\d+)/',$VidDuration,$parts); return $parts[0][0].":".$parts[0][1].":".$parts[0][2]; // Return 1:11:46 (ie) HH:MM:SS } echo getDuration("zyeubYQxHyY"); // Video ID ?> 

您可以在https://console.developers.google.com上获取您的域名自己的YouTube API密钥,并根据您自己的要求生成凭证。

使用Python 2.7和YouTube API v3的持续时间(以秒为单位):

  try: dur = entry['contentDetails']['duration'] try: minutes = int(dur[2:4]) * 60 except: minutes = 0 try: hours = int(dur[:2]) * 60 * 60 except: hours = 0 secs = int(dur[5:7]) print hours, minutes, secs video.duration = hours + minutes + secs print video.duration except Exception as e: print "Couldnt extract time: %s" % e pass