Getting the duration of a youtube video file

I have a problem. how to get youtube video duration? Here is the script.

I have an input field in this field, let's say I enter the youtube URL, now I want to check that the video should only be 1 min, if so, then I save it in the database, otherwise I will show an error message.

Can this be done?

+4
source share
4 answers

You can use the data API to retrieve information for the video. You may need to extract the video id from the URL.

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

If you use Zend, you already have a class for hard work:

<?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.com/feeds/api/videos/{$videoId} and get an XML document for processing yourself.

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

+5
source

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

+4
source

In the code below you will get a thumbnail, title and duration of the video, FROM the URL. Just change the YouTube link from the end.

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

( note: use 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.com/mrss/'); $obj->title = $media->group->title; $obj->description = $media->group->description; // get <yt:duration> node for video length $yt = $media->children('http://gdata.youtube.com/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.com/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.com/vi/$v/default.jpg' width='150' />"; echo "<p>{$video->title}</p>"; echo "<p>".sprintf("%0.2f", $video->length/60) . " min. </p>"; } ?> 
+1
source

The video URL will contain the video id. You can use this ID when requesting video information using the YouTube API . In the response of the video stream, you return to where the <yt:duration> tag should be, which you can use to get the duration of the video. Just work a bit on the API and you can find what you need.

0
source

All Articles