Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get video duration using YouTube API? [duplicate]

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?

like image 460
Developer Avatar asked Sep 20 '13 15:09

Developer


People also ask

How do you find the duration of a YouTube API?

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 $ ...

Can you still use YouTube API v2?

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.

How can I get YouTube video duration in php?

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. ...

What is snippet in YouTube API?

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.


3 Answers

Due to the upcoming deprecation of the YouTube v2 API, here is an updated solution.

  1. Start by cloning the Google API PHP Client here
  2. Next, you'll want to register an application with Google here by creating a new Project, turning on the YouTube Data API under the "APIs and auth → API" section, and
  3. Create a new OAuth Client ID under "APIs and auth → Credentials" and add the full path to your redirect URI list. (i.e. http://example.com/get_youtube_duration.php)
  4. Use this code (which was adapted from this sample code), while making sure to fill in the $OAUTH2_CLIENT_ID and $OAUTH2_CLIENT_SECRET variables with your own values from step 3.
  5. Hardcode a value for the $videoId variable or set it using the video_id get parameter (i.e. http://example.com/get_youtube_duration.php?video_id=XXXXXXX)
  6. Visit your script, click the link to authorize the app and use your google account to get a token, which redirects you back to your code and will display the duration.

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

The following works for the V2 API only.

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
like image 96
Chris Rasco Avatar answered Sep 20 '22 22:09

Chris Rasco


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'];
}
like image 35
Alaa Sadik Avatar answered Sep 21 '22 22:09

Alaa Sadik


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);
}
like image 24
Amal Murali Avatar answered Sep 20 '22 22:09

Amal Murali