How can I show youtube thumbnails on my site instead of the embedded video
本问题已经有最佳答案,请猛点这里访问。
我有一个CMS和管理员可以链接显示视频我输入的标题,然后嵌入代码保存在数据库中。
当网站显示视频列表时,它会在其嵌入表单中显示所有视频。有没有办法让这些嵌入的视频脚本变成缩略图,从而减少带宽
这是我编写的一个函数,它为一个叫php的WordPressCMS获取YouTube的各种信息。
为了使用它,您必须从URL中提取视频代码。我还包含了我以前做过的功能。
请注意,您没有提到您正在使用的CMS。我在代码中评论了我最初获取URL的地方;因为我使用的是WordPress和高级自定义字段,所以我从ACF子字段中获取URL。你可以根据需要把它传进去。:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | // 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()函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | /* * 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; } |
号