Hello, I am looking for a php script, take a youtube url as parameter, return the duration (how many seconds) the video will play.
Any advices?
Thanks!
Hello, I am looking for a php script, take a youtube url as parameter, return the duration (how many seconds) the video will play.
Any advices?
Thanks!
You could simply utilize the YouTube API and read the atom feed from:
http://gdata.youtube.com/feeds/api/videos/{VIDEO_ID}
Replace {VIDEO_ID} with whatever youtube video ID you want. You can use [man]parse_url/man to help get the query strings and some simple regex will get you the video id.
Then you can look at the output and get the "yt:duration" off of it. An example response is:
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
<id>http://gdata.youtube.com/feeds/api/videos/Ahg6qcgoay4</id>
<published>2008-03-10T17:30:17.000Z</published>
<updated>2009-12-10T13:25:48.000Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/>
<category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Howto' label='Howto & Style'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='test'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='awareness'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='basketball'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='visual'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='do'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='the'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='attention'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='span'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='bike'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='ad'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='cycling'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='TFL'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='safety'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='moonwalking'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='bear'/>
<title type='text'>Test Your Awareness: Do The Test</title>
<content type='text'>How many passes does the team in white make? Test your awareness and Do the Test! TFL cycling safetly advert! Count the number of passes the White team makes. Do you see the Moonwalking bear?</content>
<link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=Ahg6qcgoay4&feature=youtube_gdata'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/Ahg6qcgoay4/responses'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/Ahg6qcgoay4/related'/>
<link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/Ahg6qcgoay4'/>
<author>
<name>dothetest</name>
<uri>http://gdata.youtube.com/feeds/api/users/dothetest</uri>
</author>
<gd:comments>
<gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/Ahg6qcgoay4/comments' countHint='7127'/>
</gd:comments>
<yt:location>60 Great Portland Street, London, W1W 7RT</yt:location>
<media:group>
<media:category label='Howto & Style' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Howto</media:category>
<media:content url='http://www.youtube.com/v/Ahg6qcgoay4?f=videos&app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='69' yt:format='5'/>
<media:content url='rtsp://v4.cache4.c.youtube.com/CiILENy73wIaGQkuayjIqToYAhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='69' yt:format='1'/>
<media:content url='rtsp://v2.cache4.c.youtube.com/CiILENy73wIaGQkuayjIqToYAhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='69' yt:format='6'/>
<media:description type='plain'>How many passes does the team in white make? Test your awareness and Do the Test! TFL cycling safetly advert! Count the number of passes the White team makes. Do you see the Moonwalking bear?</media:description>
<media:keywords>test, awareness, basketball, visual, do, the, attention, span, bike, ad, cycling, TFL, safety, moonwalking, bear</media:keywords>
<media:player url='http://www.youtube.com/watch?v=Ahg6qcgoay4&feature=youtube_gdata'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ahg6qcgoay4/2.jpg' height='90' width='120' time='00:00:34.500'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ahg6qcgoay4/1.jpg' height='90' width='120' time='00:00:17.250'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ahg6qcgoay4/3.jpg' height='90' width='120' time='00:00:51.750'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ahg6qcgoay4/0.jpg' height='240' width='320' time='00:00:34.500'/>
<media:title type='plain'>Test Your Awareness: Do The Test</media:title>
<yt:duration seconds='69'/>
</media:group>
<gd:rating average='4.8769546' max='5' min='1' numRaters='10809' rel='http://schemas.google.com/g/2005#overall'/>
<yt:statistics favoriteCount='42492' viewCount='7749229'/>
</entry>
( The video ID for this was Ahg6qcgoay4 )
Take a look in <media:group> and at the entry <yt:duration>. It has a seconds right there for you.
Thanks!
Alternative example code:
<?php
$url = 'http://youtu.be/qPTfXwPf_HM';
//or:
//$url = 'https://www.youtube.com/watch?v=qPTfXwPf_HM';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/youtube_info.php?url=' . $url));
if (@$data->success !== 1)
{
die('Failed');
}
echo 'Video ID=' . $data->id . '<br>';
echo 'Title=' . $data->title . '<br>';
echo 'Duration sec=' . $data->duration . '<br>';
echo 'Thumbnail 1 URL=' . $data->thumb1_url . '<br>';
//rest of the thumbnails:
echo '<pre>';
print_r($data);