本文介绍了如何正确使用oembed从YouTube上拔下大拇指的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在YouTube影片的首页上显示了很多赞誉.我在下面使用此功能从youtube网址中抓取大拇指,该网址工作速度很快,但不适用于缩写形式的网址(如youtu.be/JSHDLSKL).

I show a lot of thumbs on my homepage from youtube videos. I was using this function below to grab the thumb from a youtube url which works fast but it doesn't work for url's in the shortned form like 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秒,我想这是我实现它的方式的问题.这是我在循环内称呼拇指的方式.

So I tried to use Oembed to get the thumbs instead which works for all variations of the youtube url but it retrieves the 480px/360px thumb which causes a lot of cropping to get it down to the 120px/90px size I use to display them. The other issue was it caused my page speed to increase by 4 seconds which Im guessing is a problem with the way I'm implementing it. Here's how I call the thumb inside a loop.

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

那么我应该如何做才能最大程度地提高效率?

So how should I be doing this to maximize efficiency?

推荐答案

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

Ok I came up with a solution from pieces of other questions. First we need to get the id from any type of url youtube has using this function.

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数据API从视频ID获取缩略图.看起来像这样.

Now we can get use YouTube Data API to get the thumbnail from the video id. Looks like this.

<?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秒钟.

The problem is that is causing an extra 2 seconds load time so I simply use the $vid_id and place it inside http://i3.ytimg.com/vi/<?php echo $vid_id; ?>/default.jpg which gets rid of the 2 seconds added by accessing the youtube api.

这篇关于如何正确使用oembed从YouTube上拔下大拇指的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:15