3

我在我的主页上显示了很多来自 youtube 视频的大拇指。我正在使用下面的这个函数从一个运行速度很快的 youtube url 中获取拇指,但它不适用于像 youtu.be/JSHDLSKL 这样的缩短形式的 url。

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 } ?>

那么我应该如何做到这一点以最大限度地提高效率呢?

4

2 回答 2

3

I came across this page from youtube explaining their oembed support, They mentioned that they output to json format so I made a function that gets the json data and then enables you to use it.

Feel free to ask if you need more help.

<?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
于 2012-05-01T17:05:52.230 回答
0

好的,我从其他一些问题中想出了一个解决方案。首先,我们需要使用此函数从 youtube 具有的任何类型的 url 获取 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 秒。

于 2012-05-01T21:10:11.190 回答