Home » Google » Get youtube video time duration

Get youtube video time duration

Use the following PHP function will return the YouTube video duration.Before that following the step given below
Steps for get Get youtube video time duration.
1.Create one Google API and get API key from https://console.developers.google.com
At the time of creating api please leave the option blank “creating api Accept requests from these HTTP referrers (web sites) (Optional)”. So your  requests will be accepted from any referrer. If API demands more security then add referers before using this key in production.Example: *.example.com/*

Php Code hint:put php tags before and after.
function getYoutubeDuration($vid) {
$videoDetails = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$vid."&part=contentDetails,statistics&key=AIzaSyDen99uTRRPl9VT_Ra9eAisvZSc4aZDEns");
$videoDetails =json_decode($videoDetails, true);
foreach ($videoDetails['items'] as $vidTime) {
$youtube_time = $vidTime['contentDetails']['duration'];
$hours = (strlen(floor($parts[0][0]/60))<1)?floor($parts[0][0]/60):'0'.floor($parts[0][0]/60);
$minutes = $parts[0][0]%60; (strlen($parts[0][0]%60)<1)?$parts[0][0]%60:'0'.$parts[0][0]%60;
$seconds = $parts[0][1];(strlen($parts[0][1])<1)?$parts[0][1]:'0'.$parts[0][1];
return 'Duration'.$hours.":".$minutes.":".$seconds;
echo getYoutubeDuration('wwAvoi2TDOk');//Youtube Video ID-wwAvoi2TDOk



Also Look on
How to take thumbnails from youtube video


Leave a Reply


Check Also

Google adds voice search in 8 Indian languages

Hot on the heels of the Wall Street Journal’s “The End of Typing: The Next ...