admin管理员组

文章数量:1246620

I am having a problem. how to get the time duration of youtube video? Here is the scenario.

I have a input field in this field say i enter youtube url now i want to put a validation that video should only be of 1 min, if yes then i store this in database else i show an error message.

is it possible to do this thing?

I am having a problem. how to get the time duration of youtube video? Here is the scenario.

I have a input field in this field say i enter youtube url now i want to put a validation that video should only be of 1 min, if yes then i store this in database else i show an error message.

is it possible to do this thing?

Share Improve this question asked Oct 14, 2011 at 19:46 theGametheGame 3934 gold badges6 silver badges18 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

You can use the data API to get the information for a video. You might need to extract the video's identifier from the URL.

http://code.google./apis/youtube/2.0/developers_guide_php.html#Retrieving_Video_Entry

If you're using Zend, there's already a class to do the heavy lifting:

<?php
require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();
$videoEntry = $yt->getVideoEntry('the0KZLEacs');
$duration = $videoEntry->getVideoDuration();

If not, you can go to http://gdata.youtube./feeds/api/videos/{$videoId} and get an XML document to process yourself.

For example, http://gdata.youtube./feeds/api/videos/KURI9EQV3dY returns an XML document for a video which contains information including the duration: <yt:duration seconds='153'/>

Per their docs, In a video feed entry, the <yt:duration> tag specifies a video's length.

The code below will get you the video thumbnail, title, and duration, FROM THE URL. Just change the youtube link from the end.

Demo: http://100ro.ro/wp-includes/ajaxdemo/test.php?y=www.youtube./watch?v=V80jm1rs2UQ :

(note: use the youtube url in y=youtubeurl without http:// )

<?php

      getYoutubeImage($_GET["y"]);

            function getYoutubeImage($e){
            //GET THE URL
            $url = $e;

            $queryString = parse_url($url, PHP_URL_QUERY);

            parse_str($queryString, $params);

            $v = $params['v'];  




        // function to parse a video <entry>
        function parseVideoEntry($entry) {      
          $obj= new stdClass;

          // get nodes in media: namespace for media information
          $media = $entry->children('http://search.yahoo./mrss/');
          $obj->title = $media->group->title;
          $obj->description = $media->group->description;



          // get <yt:duration> node for video length
          $yt = $media->children('http://gdata.youtube./schemas/2007');
          $attrs = $yt->duration->attributes();
          $obj->length = $attrs['seconds']; 


          // return object to caller  
          return $obj;      
        }   

        // get video ID from $_GET 
        if (!isset($v)) {
          die ('ERROR: Missing video ID');  
        } else {
          $vid = $v;
        }

        // set video data feed URL
        $feedURL = 'http://gdata.youtube./feeds/api/videos/' . $v;

        // read feed into SimpleXML object
        $entry = simplexml_load_file($feedURL);

        // parse video entry
        $video = parseVideoEntry($entry);

        // display video image, title and duration
        echo "<img src='http://i3.ytimg./vi/$v/default.jpg' width='150' />";
        echo "<p>{$video->title}</p>";
        echo "<p>".sprintf("%0.2f", $video->length/60) . " min. </p>";


        } 

        ?>

The URL of the video will contain the video ID. You can use this ID when requesting video information using YouTube's API. In the video feed response you get back there should be a <yt:duration> tag that you can use to get the duration of the video. Just take a bit to go over the API and you should be able to find what you need.

本文标签: phpGetting time length of youtube video fileStack Overflow