如何正确使用oembed从youtube拉拇指

时间:2012-04-30 00:30:25

标签: youtube-api thumbnails oembed

我在youtube视频的主页上显示了很多拇指。我正在使用下面的这个函数从youtube网址抓取拇指,它可以快速工作但是对于像youtu.be/JSHDLSKL这样的缩写形式的网址不起作用。

function get_youtube_screen_link( $url = '', $type = 'default', $echo = true ) {
if( empty( $url ) )
    return false;

if( !isset( $type ) )
    $type = '';

$url = esc_url( $url );

preg_match("|[\\?&]v=([^&#]*)|",$url,$vid_id);

if( !isset( $vid_id[1] ) )
    return false;

$img_server_num =  'i'. rand(1,4);

switch( $type ) {
    case 'large':
        $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/0.jpg";
        break;
    case 'first':
        // Thumbnail of the first frame
        $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/1.jpg";
        break;
    case 'small':
        // Thumbnail of a later frame(i'm not sure how they determine this)
        $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/2.jpg";
        break;
    case 'default':
    case '':
    default:
        $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/default.jpg";
        break;
}
if( $echo )
    echo $img_link;
else
    return $img_link;

}

所以我尝试使用Oembed来取代拇指,它适用于youtube url的所有变体,但是它会检索480px / 360px拇指,导致大量的裁剪将其降低到我使用的120px / 90px尺寸显示它们。另一个问题是它导致我的页面速度增加了4秒,我猜这是我实现它的方式的问题。以下是我在循环中调用拇指的方法。

<?php 
 require_once(ABSPATH.'wp-includes/class-oembed.php');
 $oembed= new WP_oEmbed;
 $name = get_post_meta($post->ID,'video_code',true);
 $url = $name;
//As noted in the comments below, you can auto-detect the video provider with the following
 $provider = $oembed->discover($name);
//$provider = 'http://www.youtube.com/oembed';
 $video = $oembed->fetch($provider, $url, array('width' => 300, 'height' => 175));
 $thumb = $video->thumbnail_url; if ($thumb) { ?>
   <img src="<?php echo $thumb; ?>" width="120px" height="90px" />
<?php } ?>

那么我应该怎样做才能最大限度地提高效率呢?

2 个答案:

答案 0 :(得分:3)

我从youtube上发现了这个页面,解释了他们的oembed support,他们提到他们输出为json格式,所以我创建了一个获取json数据的函数,然后让你使用它。

随意询问您是否需要更多帮助。

<?php

$youtube_url = 'http://youtu.be/oHg5SJYRHA0'; // url to youtube video

function getJson($youtube_url){

    $baseurl = 'http://www.youtube.com/oembed?url='; // youtube oembed base url
    $url = $baseurl . $youtube_url . '&format=json'; // combines the url with format json

    $json = json_decode(file_get_contents($url)); // gets url and decodes the json

    return $json;

}

$json = getJson($youtube_url);

// from this point on you have all your data placed in variables.

$provider_url = $json->{'provider_url'};
$thumbnail_url = $json->{'thumbnail_url'};
$title = $json->{'title'};
$html = $json->{'html'};
$author_name = $json->{'author_name'};
$height = $json->{'height'};
$thumbnail_width = $json->{'thumbnail_width'};
$thumbnail_height = $json->{'thumbnail_height'};
$width = $json->{'width'};
$version = $json->{'version'};
$author_url = $json->{'author_url'};
$provider_name = $json->{'provider_name'};
$type = $json->{'type'};

echo '<img src="'.$thumbnail_url.'" />'; // echo'ing out the thumbnail image

答案 1 :(得分:0)

好的,我想出了其他一些问题的解决方案。首先,我们需要使用此功能从youtube的任何类型的网址获取ID。

function getVideoId($url)
{
$parsedUrl = parse_url($url);
if ($parsedUrl === false)
    return false;

if (!empty($parsedUrl['query']))
{
    $query = array();
    parse_str($parsedUrl['query'], $query);
    if (!empty($query['v']))
        return $query['v'];
}

if (strtolower($parsedUrl['host']) == 'youtu.be')
    return trim($parsedUrl['path'], '/');

return false;
}

现在,我们可以使用YouTube Data API从视频ID中获取缩略图。看起来像这样。

<?php
  $vid_id = getVideoId($video_code);
  $json = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/$vid_id?v=2&alt=jsonc"));
  echo '<img src="' . $json->data->thumbnail->sqDefault . '" width="176" height="126">'; 
?>

问题是导致额外的2秒加载时间,因此我只需使用$vid_id并将其放在http://i3.ytimg.com/vi/<?php echo $vid_id; ?>/default.jpg内,即可通过访问youtube api来消除2秒。