如何在我的网站上显示youtube缩略图而不是嵌入的视频

时间:2013-05-21 20:31:36

标签: php youtube youtube-api

我有一个CMS,管理员可以链接显示我输入标题的视频,然后是保存在数据库中的嵌入代码。

当网站显示视频列表时,它会以嵌入形式显示所有视频。有没有办法让这些嵌入视频脚本成为缩略图,使用更少的带宽

1 个答案:

答案 0 :(得分:1)

这是我写的一个函数,用于获取WordPress CMS的各种YouTube信息,即PHP。

要使用它,您必须从网址中提取视频代码。我还包括我曾经做过的功能。

请注意,您没有提及您正在使用的CMS。我在我的代码中评论了我最初获取的URL;因为我正在使用WordPress和高级自定义字段,所以我从ACF子字段中获取网址。您可以根据需要传递它。 :)

 // Grab the video's url from your CMS. 
 // get_sub_field() is an Advanced Custom Fields function from WordPress
 // Make sure to swap it out with however you plan on passing this in
 // The URL can be formed as http://www.youtube.com/watch?v=9bZkp7q19f0
 // Note that I didn't include functionality for http://youtu.be type of urls, 
 // but you could work that out. :) In my web app, I also have a similar Vimeo function, 
 // which is why I even bother to check the url in the first place. 
 $video_url = get_sub_field('CMS_video_url');
 $video_url_a = parse_url($video_url);

 // Check if this is a youtube video. You could add in youtu.be logic here.
 if($video_url_a['host'] == 'www.youtube.com' || $video_url_a['host'] == 'youtube.com'){
      $array = explode("&", $video_url_a['query']);
      $video_id = substr($array[0],2);

      // Grab the info for a large thumbnail. You could also grab a small thumb,
      // as well as the title, the description, the author, or the author's uri.
      // See the get_youtube_info() function below
      $videothumb = get_youtube_info($video_id, 'thumbnail_large');

      // So here's an example of grabbing the video title for the alt tag. :)
      $videotitle = get_youtube_info($video_id, 'title');
 } else {
      // enter whatever fail functionality you want
 }

 echo '<img class="video-thumb" src="' . $videothumb . '" alt="' . $videotitle . '" />'

这是get_youtube_info()函数:

 /* 
  *  Here's a function to get a limited set of youtube info
  * see switch in function
  * an example JSON returned: Gungnam Style!
  * http://gdata.youtube.com/feeds/api/videos/9bZkp7q19f0?v=2&alt=json-in-script&prettyprint=true
 */
function get_youtube_info ( $vid, $info ) {
    $youtube = "http://gdata.youtube.com/feeds/api/videos/$vid?v=2&alt=json&feature=related";
    $ch = curl_init($youtube);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);

    //If $assoc = true doesn't work, try:
    //$output = json_decode($output, true);
    $output = json_decode($output, $assoc = true);

    //Add the ['feed'] in if it exists.
    if ($output['feed']) {
        $path = &$output['feed']['entry'];
    } else {
        $path = &$output['entry'];
    }

    //set up a switch to return various data bits to return.
    switch($info) {
        case 'title':
            $output = $path['title']['$t'];
            break;
        case 'description':
            $output = $path['media$group']['media$description']['$t'];
            break;
        case 'author':
            $output = $path['author'][0]['name'];
            break;
        case 'author_uri':
            $output = $path['author'][0]['uri'];
            break;
        case 'thumbnail_small':
            $output = $path['media$group']['media$thumbnail'][0]['url'];
            break;
        case 'thumbnail_medium':
            $output = $path['media$group']['media$thumbnail'][2]['url'];
            break;
        case 'thumbnail_large':
            $output = $path['media$group']['media$thumbnail'][3]['url'];
            break;
        default:
            return $output;
            break;
    }
    return $output;
}