<script> function getScreen( url, size ) { if(url === null){ return ""; } size = (size === null) ? "big" : size; var vid; var results; results = url.match("[\\?&]v=([^&#]*)"); vid = ( results === null ) ? url : results[1]; if(size == "small"){ return "http://img.youtube.com/vi/"+vid+"/2.jpg"; }else { return "http://img.youtube.com/vi/"+vid+"/0.jpg"; } } imgUrl_big = getScreen("http://www.youtube.com/watch?v=9lp0IWv8QZY&feature=featured"); imgUrl_big2 = getScreen("uVLQhRiEXZs"); imgUrl_small = getScreen("uVLQhRiEXZs", 'small'); document.write('<img src="' + imgUrl_big + '" /><br><br>'); document.write('<img src="' + imgUrl_big2 + '" /><br><br>'); document.write('<img src="' + imgUrl_small + '" />'); </script>
You can pass a YouTube video URL or video id and the function will return a path to the video image. The second function argument is optional.
You can specify the size of returned image.
It can be big
(320x240) or small
(128x96), defaults to big
.
php 版
<?php
/**
* parse_youtube_url() PHP function
* Author: takien
* URL: http://takien.com
*
* @param string $url URL to be parsed, eg:
* http://youtu.be/zc0s358b3Ys,
* http://www.youtube.com/embed/zc0s358b3Ys
* http://www.youtube.com/watch?v=zc0s358b3Ys
* @param string $return what to return
* - embed, return embed code
* - thumb, return URL to thumbnail image
* - hqthumb, return URL to high quality thumbnail image.
* @param string $width width of embeded video, default 560
* @param string $height height of embeded video, default 349
* @param string $rel whether embeded video to show related video after play or not.
*/
function parse_youtube_url($url,$return='embed',$width='',$height='',$rel=0)
{
$urls = parse_url($url);
//expect url is http://youtu.be/abcd, where abcd is video iD
if($urls['host'] == 'youtu.be') $id = ltrim($urls['path'],'/');
//expect url is http://www.youtube.com/embed/abcd
else if(strpos($urls['path'],'embed') == 1) $id = end(explode('/',$urls['path']));
//expect url is abcd only
else if(strpos($url,'/')===false) $id = $url;
//expect url is http://www.youtube.com/watch?v=abcd
else{
parse_str($urls['query']);
$id = $v;
}
//return embed iframe
if($return == 'embed') return '<iframe width="'.($width?$width:560).'" height="'.($height?$height:349).'" src="http://www.youtube.com/embed/'.$id.'?rel='.$rel.'" frameborder="0" allowfullscreen>';
//return normal thumb
else if($return == 'thumb') return 'http://i1.ytimg.com/vi/'.$id.'/default.jpg';
//return hqthumb
else if($return == 'hqthumb') return 'http://i1.ytimg.com/vi/'.$id.'/hqdefault.jpg';
// else return id
else return $id;
}
// example
echo '<img src="'.parse_youtube_url('http://www.youtube.com/watch?v=QM-CvD8GQS4&feature=player_embedded','hqthumb').'" />'; //return http://i1.ytimg.com/vi/zc0s358b3Ys/hqdefault.jpg
echo parse_youtube_url('http://www.youtube.com/watch?v=QM-CvD8GQS4&feature=player_embedded','embed'); //return embed code (iframe)
?>
以上php可以获取id值,利用id值就可以获取他的图片或者标题,
获取标题:
<?
$vidID = $_POST['vidID'];
$url = "http://gdata.youtube.com/feeds/api/videos/".$vidID;
$doc = new DOMDocument;
$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
?>
<html>
<head>
<title>Get Video Name</title>
</head>
<body>
<form method="post">
<input type="text" value="ID Here" name="vidID" />
<input type="submit" value="Get Name" />
</form>
<div id="page">URL: [<?= $url ?>]</div>
<div id="title">Title: [<?= $title ?>]</div>
</body>
</html>
或者:
<?php
$video_id = 'BGCqmjxQGOE';
$content = file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id);
parse_str($content, $ytarr);
echo $ytarr['title'];
?>