我使用Youtube API v3来搜索youtube。Youtube API v3,如何获得视频持续时间?
https://developers.google.com/youtube/v3/docs/search
正如你可以看到JSON不包含视频持续时间的响应。有没有办法获得视频的持续时间?最好不要再为结果中的每个元素调用API(除非这是获得持续时间的唯一方法)。
我使用Youtube API v3来搜索youtube。Youtube API v3,如何获得视频持续时间?
https://developers.google.com/youtube/v3/docs/search
正如你可以看到JSON不包含视频持续时间的响应。有没有办法获得视频的持续时间?最好不要再为结果中的每个元素调用API(除非这是获得持续时间的唯一方法)。
在您进行搜索调用后,您将不得不拨打Youtube Data API的视频资源。您可以在搜索中放置50个视频ID,因此您不必为每个元素调用它。
https://developers.google.com/youtube/v3/docs/videos/list
你要设定的部分= 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 8601的字符串。 PT代表时间持续时间,4M是4分钟,13S是13秒。
太棒了!谢谢! – sleepwalkerfx 2013-03-25 05:57:13
这是什么时间格式?它是Objective C支持的标准吗? – sleepwalkerfx 2013-03-30 17:28:48
最新评论,但您可能想要明星并密切关注此故障单,请求时长包含在搜索响应中:https://code.google.com/p/gdata-issues/issues/detail?id = 4294 – 2014-03-06 06:54:51
Got it!
$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
,我已经从渠道的视频的返回列表中脱离我显示的视频从...
它的作品.. :)谷歌真的需要在片段中包含持续时间,以便您可以通过一次调用而不是两次......叹息.......这是在他们的'wontfix'列表中。
我编写了以下课程,使用YouTube API v3获取YouTube视频时长。 (它返回缩略图以及)
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:
持续时间在使用Python 2.7和YouTube API第3版秒:以小时,分钟,并且使用正则表达式秒
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
获取的持续时间(在Python 3测试)
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 ...
你用什么库来做雷克斯?它是rex还是rex。我用python-rex库试了你的代码(假设是那个),它说你有一个正则表达式错误。 – Spartacus 2015-11-11 21:52:16
@Spartacus我认为我在rex()有一些lambda,做re.search或者某事。 'rex = lambda x,y:re.search(x,y).groups()' – mikroskeem 2015-11-13 07:01:36
Gotcha。谢谢! – Spartacus 2015-11-20 14:45:36
此代码提取的Youtube视频持续时间使用Youtube API v3通过传递视频ID,它为我工作。
<?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 (i.e) HH:MM:SS
}
echo getDuration("zyeubYQxHyY"); // Video ID
?>
您可以获取域的自己的YouTube API密钥https://console.developers.google.com并生成自己需要的凭据。
典型的谷歌,采取了一些行之有效的工作,并破坏它。 – Synetech 2015-05-30 15:09:27