I created a node in drupal7 which contains a youtube link. I installed youtube module in my site. with the help of this module i just created a field for inputting url to youtube video. every thing worked fine. It is showing me the thumbnail,video etc. But the problem is how to get the length of the youtube video ?

I am using views to generate a json web service the output is

            "title":"social networking",
            "field_eventcat":"Performing Arts",
            "field_eventdaytime":"06\/06\/2012 - 05:00",
            "field_eventvideos":"http:\/\/sample sites\/sites\/default\/files\/styles\/thumbnail\/public\/youtube\/6a_KF7TYKVc.png",

I want to get the video lenght with this object. pls help


1 Answer 1


You can follow the function youtube_get_remote_image on how to make a call to youtube api and retrieve video length.

Youtube public api provides this info without any authentication, for instance - http://gdata.youtube.com/feeds/api/videos/dc4UltkRJsw contains video length in the tag: yt:duration. I have not tested the below function, but should work.

function youtube_get_video_length($id = NULL) {
  $path = 'http://gdata.youtube.com/feeds/api/videos/' . $id;
  $query = array(
   'v' => '2',
   'alt' => 'jsonc'
  $url = url($path, array('query' => $query));
  $result = drupal_http_request($url);
  $data = json_decode($result->data);
  $video_length = $data->data->duration;
  return $video_length;

Also I dont suggest that you write this in youtube.inc of youtube module, write in your custom module or template.php of custom theme.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.