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



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

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

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

为了使用它,你必须从url中提取视频代码。我还包括了我用来做这个的功能。

请注意,您没有提到您正在使用的CMS。我在代码中注释了我最初获取URL的地方;因为我使用WordPress和高级自定义字段,我从ACF子字段抓取url。你可以把它交给任何你需要的人。:)

 // 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;
}

相关内容

  • 没有找到相关文章

最新更新