我想获得Youtube视频的持续时间。这是我试过的代码:
$vidID="voNEBqRZmBc";
//http://www.youtube.com/watch?v=voNEBqRZmBc
$url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
$doc = new DOMDocument;
$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
请检查XML文件。我得到了这个视频的标题。我想从<yt:duration seconds='29'/>
获得持续时间。
如何获取此seconds
代码的<yt>
属性?
答案 0 :(得分:17)
这是你的输出:
Video Duration
0 mins
29 secs
一些额外资源: https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration https://developers.google.com/youtube/v3/docs/videos/list
这对我有用,因为假设Feed中只有一个持续时间元素。
<?php
$vidID="voNEBqRZmBc";
//http://www.youtube.com/watch?v=voNEBqRZmBc
$url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
$doc = new DOMDocument;
$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
$duration = $doc->getElementsByTagName('duration')->item(0)->getAttribute('seconds');
print "TITLE: ".$title."<br />";
print "Duration: ".$duration ."<br />";
输出:
TITLE: Introducing iTime
Duration: 29
答案 1 :(得分:8)
这是YouTube API V3,其中包含获取视频时长的示例,但请不要忘记在https://console.developers.google.com/project
创建API密钥 $vidkey = "cHPMH2sa2Q" ; video key for example
$apikey = "xxxxxxxxxxxxxxxxxxxxx" ;
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey");
$VidDuration =json_decode($dur, true);
foreach ($VidDuration['items'] as $vidTime)
{
$VidDuration= $vidTime['contentDetails']['duration'];
}
答案 2 :(得分:3)
使用SimpleXML的简短解决方案:
function getYouTubeVideoDuration($id) {
$xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$id);
return strval($xml->xpath('//yt:duration[@seconds]')[0]->attributes()->seconds);
}
答案 3 :(得分:2)
所有上述答案都以文本格式提供持续时间:
此解决方案将为您提供转换为您想要的任何格式的小时/分钟/秒:
function getDurationInSeconds($talkId){
$vidkey = $talkId;
$apikey = "xxxxx";
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey");
$VidDuration =json_decode($dur, true);
foreach ($VidDuration['items'] as $vidTime)
{
$VidDuration= $vidTime['contentDetails']['duration'];
}
preg_match_all('/(\d+)/', $VidDuration, $parts);
$hours = intval(floor($parts[0][0]/60) * 60 * 60);
$minutes = intval($parts[0][0]%60 * 60);
$seconds = intval($parts[0][1]);
$totalSec = $hours + $minutes + $seconds; // This is the example in seconds
return $totalSec;
}
答案 4 :(得分:0)
非常简单
function getYoutubeDuration($videoid) {
$xml = simplexml_load_file('https://gdata.youtube.com/feeds/api/videos/' . $videoid . '?v=2');
$result = $xml->xpath('//yt:duration[@seconds]');
$total_seconds = (int) $result[0]->attributes()->seconds;
return $total_seconds;
}
//now call this pretty function. As parameter I gave a video id to my function and bam!
echo getYoutubeDuration("y5nKxHn4yVA");
答案 5 :(得分:-2)
您还可以在JSON中获得持续时间
$video_id = "<VIDEO_ID>";
http://gdata.youtube.com/feeds/api/videos/$video_id?v=2&alt=jsonc
哦!对不起,您可以通过以下方式获取:
$duration = $xml->getElementsByTagName('duration')->item(0)->getAttribute('seconds');