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のサイズに縮小するために多くのトリミングが行われます。もう1つの問題は、ページ速度が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

YouTubeからこのページに出くわし、oembedのサポートについて説明しました。彼らは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
于 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、YouTubeAPIにアクセスして追加された2秒が削除されます。

于 2012-05-01T21:10:11.190 に答える