1

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

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

4

1 回答 1

1

Here's a function I wrote to grab various bits of YouTube information for a WordPress CMS, which is PHP.

In order to use it, you have to extract the video code from the url. I'm also including the functionality I used to do that.

Please note, you didn't mention with CMS you're using. I commented in my code where I'm getting the URL originally; since I'm using WordPress and Advanced Custom Fields, I'm grabbing the url from an ACF subfield. You can pass it in however you need. :)

 // 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 . '" />'

And here's the get_youtube_info() function:

 /* 
  *  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;
}
于 2013-05-21T20:59:12.363 回答