I want to get the duration of a Youtube video. Here's the code I tried:
$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;
Please check the XML file. I got the title of this video. I want to get duration from <yt:duration seconds='29'/>
.
How to get the seconds
attribute this <yt>
tag?
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 $ ...
What should I do? You can continue using the v2 API for comments and uploading video captions for now, and we'll be adding this functionality into the v3 API soon. While we don't have specific dates yet, we will release that functionality so that developers have as much time as possible to migrate to v3.
php function getDuration($url){ parse_str(parse_url($url,PHP_URL_QUERY),$arr); $video_id=$arr['v']; $data=@file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$video_id.'?v=2&alt=jsonc'); if (false===$data) return false; $obj=json_decode($data); return $obj->data->duration; } echo getDuration('http://www. ...
The snippet object contains basic details about the video, such as its title, description, and category. The date and time that the video was published. Note that this time might be different than the time that the video was uploaded.
Here is your output:
Video Duration
0 mins
29 secs
Some additional resources: https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration https://developers.google.com/youtube/v3/docs/videos/list
This worked for me based on the assumption that there is only one duration element in the 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 />";
Output:
TITLE: Introducing iTime
Duration: 29
Here it is YouTube API V3, with example of getting video duration, but do not forget to create your API key at https://console.developers.google.com/project
$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'];
}
A short and simple solution using 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With