如何从YouTube API获取YouTubevideo缩略图?

如果我有YouTubevideourl,有没有办法使用PHP和cURL从YouTube API获取关联的缩略图?

每个YouTubevideo都有4个生成的图像。 他们可以预见格式如下:

https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg https://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg https://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg https://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg 

列表中的第一个是全尺寸的图像,而其他的是缩略图图像。 默认缩略图(即1.jpg2.jpg3.jpg )是:

 https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg 

对于缩略图的高质量版本,请使用类似于以下的url:

 https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg 

还有一个中等质量的缩略图版本,使用类似于总部的url:

 https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg 

对于缩略图的标准定义版本,请使用类似以下的url:

 https://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg 

对于缩略图的最大分辨率版本,请使用与此类似的url:

 https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg 

所有上述的url都可以在http上使用。 此外,在上面的示例url中,略短的主机i3.ytimg.com可以代替img.youtube.com

或者,您可以使用YouTube数据API(v3)获取缩略图。

您可以使用YouTube数据API检索video缩略图,标题,说明,评分,统计数据等。 API版本3需要一个密钥*。 获取密钥并创buildvideo列表请求:

 https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=VIDEO_ID 

PHP代码示例

 $data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=T0Jqdjbed40"); $json = json_decode($data); var_dump($json->items[0]->snippet->thumbnails); 

产量

 object(stdClass)#5 (5) { ["default"]=> object(stdClass)#6 (3) { ["url"]=> string(46) "https://i.ytimg.com/vi/T0Jqdjbed40/default.jpg" ["width"]=> int(120) ["height"]=> int(90) } ["medium"]=> object(stdClass)#7 (3) { ["url"]=> string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/mqdefault.jpg" ["width"]=> int(320) ["height"]=> int(180) } ["high"]=> object(stdClass)#8 (3) { ["url"]=> string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/hqdefault.jpg" ["width"]=> int(480) ["height"]=> int(360) } ["standard"]=> object(stdClass)#9 (3) { ["url"]=> string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/sddefault.jpg" ["width"]=> int(640) ["height"]=> int(480) } ["maxres"]=> object(stdClass)#10 (3) { ["url"]=> string(52) "https://i.ytimg.com/vi/T0Jqdjbed40/maxresdefault.jpg" ["width"]=> int(1280) ["height"]=> int(720) } } 

*不仅您需要密钥,您可能会被要求提供账单信息,具体取决于您计划制作的API请求的数量。 但是,每天几百万的请求是免费的。

源文章 。

阿萨夫说的是对的。 但是,并非每个YouTubevideo都包含全部九个缩略图,但每个video都有七个缩略图。 他们是:

(图片大小取决于video。)

  • 播放器背景缩略图(480×360像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/0.jpg

  • 开始缩略图(120×90像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/1.jpg

  • 中间缩略图(120×90像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/2.jpg

  • 结束缩略图(120×90像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/3.jpg

  • 高品质缩略图(480×360像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/hqdefault.jpg

  • 中质量缩略图(320×180像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/mqdefault.jpg

  • 正常质量缩略图(120×90像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/default.jpg

另外,接下来的两个缩略图可能存在也可能不存在。 对于HQvideo他们存在。

  • 标准清晰度缩略图(640×480像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/sddefault.jpg

  • 最大分辨率缩略图(1920×1080像素)
    https://i1.ytimg.com/vi/G0wGs3useV8/maxresdefault.jpg

您可以通过JavaScript和PHP脚本来检索缩略图和其他YouTube信息

  • 如何使用PHP获取YouTubevideo信息
  • 使用JavaScript检索YouTubevideo详细信息 – JSON和API v2

也可以使用YouTubevideo信息生成工具通过提交url或videoID来获取YouTubevideo的所有信息。

在YouTube API V3中,我们也可以使用这些url来获取缩略图…他们根据其质量进行分类。

 https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/default.jpg - default https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg - medium https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg - high https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/sddefault.jpg - standard 

并为最高分辨率..

 https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg 

这些URL在第一个答案中的优点是这些URL不会被防火墙阻止。

如果您想从YouTube获得最大的图片来显示特定的videoID,那么url应该是这样的:

 http://i3.ytimg.com/vi/SomeVideoIDHere/0.jpg 

使用API​​,您可以选取默认的缩略图图像。 简单的代码应该是这样的:

 //Grab the default thumbnail image $attrs = $media->group->thumbnail[1]->attributes(); $thumbnail = $attrs['url']; $thumbnail = substr($thumbnail, 0, -5); $thumb1 = $thumbnail."default.jpg"; // Grab the third thumbnail image $thumb2 = $thumbnail."2.jpg"; // Grab the fourth thumbnail image. $thumb3 = $thumbnail."3.jpg"; // Using simple cURL to save it your server. // You can extend the cURL below if you want it as fancy, just like // the rest of the folks here. $ch = curl_init ("$thumb1"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $rawdata = curl_exec($ch); curl_close($ch); // Using fwrite to save the above $fp = fopen("SomeLocationInReferenceToYourScript/AnyNameYouWant.jpg", 'w'); // Write the file fwrite($fp, $rawdata); // And then close it. fclose($fp); 

如果你想摆脱“黑条”,像YouTube这样做,你可以使用:

 https://i.ytimg.com/vi_webp/<video id>/mqdefault.webp 

如果你不能使用.webp扩展你可以这样做:

 https://i.ytimg.com/vi/<video id>/mqdefault.jpg 

此外,如果您需要使用maxresdefault缩放版本,请使用maxresdefault而不是mqdefault

注意:如果您打算使用maxresdefault我不确定宽高比。

在YouTube Data API v3中 ,您可以使用videos-> listfunction获取video的缩略图。 从snippet.thumbnails(键) ,你可以select默认的,中等或高分辨率的缩略图,并获得其宽度,高度和url。

您还可以使用缩略图 – >设置function来更新缩略图。

例如,您可以查看YouTube API示例项目。 ( PHP的 )

您可以获取包含video缩略图的URL的video条目 。 链接中有示例代码。 或者,如果你想parsingXML,这里有信息。 返回的XML具有media:thumbnail元素,其中包含缩略图的URL。

我做了一个function,只从YouTube获取现有的图像

 function youtube_image($id) { $resolution = array ( 'maxresdefault', 'sddefault', 'mqdefault', 'hqdefault', 'default' ); for ($x = 0; $x < sizeof($resolution); $x++) { $url = '//img.youtube.com/vi/' . $id . '/' . $resolution[$x] . '.jpg'; if (get_headers($url)[0] == 'HTTP/1.0 200 OK') { break; } } return $url; } 
 // Get image form video URL $url = $video['video_url']; $urls = parse_url($url); //Expect the URL to be http://youtu.be/abcd, where abcd is the video ID if ($urls['host'] == 'youtu.be') : $imgPath = ltrim($urls['path'],'/'); //Expect the URL to be http://www.youtube.com/embed/abcd elseif (strpos($urls['path'],'embed') == 1) : $imgPath = end(explode('/',$urls['path'])); //Expect the URL to be abcd only elseif (strpos($url,'/') === false): $imgPath = $url; //Expect the URL to be http://www.youtube.com/watch?v=abcd else : parse_str($urls['query']); $imgPath = $v; endif; 

YouTube归Google所有,Google喜欢为不同的屏幕尺寸提供合理数量的图片,因此其图片以不同的尺寸存储,下面是您喜欢的缩略图样例

低质量缩略图:

 http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/sddefault.jpg 

中质量缩略图:

 http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/mqdefault.jpg 

高品质的缩略图:

 http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/hqdefault.jpg 

最高质量缩略图:

 http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/maxresdefault.jpg 

YOUTUBE API版本3 ** 2分钟内即可运行

如果您只想searchYouTube并获取关联的媒体资源:

  1. 获取公共API – 此链接提供了良好的方向

2.使用下面的查询string。 出于示例目的,urlstring中的search查询(由q =表示)是stackoverflow 。 然后,YouTube会向您发回一个json回复,然后您可以parsingThumbnail,Snippet,Author等。

 https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=50&q=stackoverflow&key=YOUR_API_KEY_HERE 

使用:

 https://www.googleapis.com/youtube/v3/videoCategories?part=snippet,id&maxResults=100&regionCode=us&key=**Your YouTube ID** 

以上是链接。 使用它,你可以findvideo的YouTube特征。 find特征后,您可以获取所选类别的video。 之后,您可以使用Asaph的答案find选定的video图像。

尝试上述方法,您可以parsingYouTube API中的所有内容。

我以这种方式使用了YouTube缩略图:

 $url = 'http://img.youtube.com/vi/' . $youtubeId . '/0.jpg'; $img = dirname(__FILE__) . '/youtubeThumbnail_' . $youtubeId . '.jpg'; file_put_contents($img, file_get_contents($url)); 

记住,YouTube禁止直接从他们的服务器中包含图像

我发现这个漂亮的工具,允许您创build图像的YouTube播放button放在图像上:

  • 在服务器上安装脚本: https : //github.com/halgatewood/youtube-thumbnail-enhancer

只是为了增加/扩展所提供的解决scheme,我觉得有必要指出,由于我自己有这个问题,实际上可以通过一个HTTP请求获取多个YouTubevideo内容,在这种情况下是缩略图:

在这种情况下,使用Rest Client,HTTPFUL,你可以这样做:

 <?php header("Content-type", "application/json"); //download the httpfull.phar file from http://phphttpclient.com include("httpful.phar"); $youtubeVidIds= array("nL-rk4bgJWU", "__kupr7KQos", "UCSynl4WbLQ", "joPjqEGJGqU", "PBwEBjX3D3Q"); $response = \Httpful\Request::get("https://www.googleapis.com/youtube/v3/videos?key=YourAPIKey4&part=snippet&id=".implode (",",$youtubeVidIds)."") ->send(); print ($response); ?> 

一个简单的PHP函数,我为YouTube的缩略图和types创build

  • 默认
  • hqdefault
  • mqdefault
  • sddefault
  • maxresdefault

     function get_youtube_thumb($link,$type){ $video_id = explode("?v=", $link); if (empty($video_id[1])){ $video_id = explode("/v/", $link); $video_id = explode("&", $video_id[1]); $video_id = $video_id[0]; } $thumb_link = ""; if($type == 'default' || $type == 'hqdefault' || $type == 'mqdefault' || $type == 'sddefault' || $type == 'maxresdefault'){ $thumb_link = 'http://img.youtube.com/vi/'.$video_id.'/'.$type.'.jpg'; }elseif($type == "id"){ $thumb_link = $video_id; } return $thumb_link;} 

如果您使用公共API,最好的方法是使用if语句。

如果video是公开或不公开的,请使用url方式设置缩略图。 如果video是私密的,则使用api获取缩略图。

 <?php if($video_status == 'unlisted'){ $video_thumbnail = 'http://img.youtube.com/vi/'.$video_url.'/mqdefault.jpg'; $video_status = '<i class="fa fa-lock"></i>&nbsp;Unlisted'; } elseif($video_status == 'public'){ $video_thumbnail = 'http://img.youtube.com/vi/'.$video_url.'/mqdefault.jpg'; $video_status = '<i class="fa fa-eye"></i>&nbsp;Public'; } elseif($video_status == 'private'){ $video_thumbnail = $playlistItem['snippet']['thumbnails']['maxres']['url']; $video_status = '<i class="fa fa-lock"></i>&nbsp;Private'; } 

另一个不错的select是使用YouTube支持的oEmbed API。

您只需将您的YouTubeurl添加到oEmbedurl,即可获得一个包含缩略图和用于embedded的html代码的JSON。

例:

 http://www.youtube.com/oembed?format=json&url=http%3A//youtube.com/watch%3Fv%3DDLzxrzFCyOs 

会给你:

 { thumbnail_url: "https://i.ytimg.com/vi/DLzxrzFCyOs/hqdefault.jpg", width: 459, author_name: "AllKindsOfStuff", version: "1.0", author_url: "https://www.youtube.com/channel/UCLNd5EtH77IyN1frExzwPRQ", thumbnail_width: 480, type: "video", provider_url: "https://www.youtube.com/", html: "<iframe width="459" height="344" src="https://www.youtube.com/embed/DLzxrzFCyOs?feature=oembed" frameborder="0" allowfullscreen></iframe>", title: "Some title bla bla foo bar", thumbnail_height: 360, provider_name: "YouTube", height: 344 } 

阅读文档以获取更多信息 。

我认为他们的缩略图是很多的答案,但我想添加一些其他的url,很容易得到youtube缩略图。 我只是从Asaph的回答中拿出一些文字。 这里有一些url来获取youtube缩略图

 https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/default.jpg 

对于缩略图的高质量版本,请使用类似如下的url:

 https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg 

还有一个中等质量的缩略图版本,使用类似于总部的url:

 https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg 

对于缩略图的标准定义版本,请使用类似以下的url:

 https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/sddefault.jpg 

对于缩略图的最大分辨率版本,请使用与此类似的url:

 https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg 

希望能在不久的将来帮助别人。

YouTube数据API

YouTube通过Data API(v3)为每个video提供4个生成的图像,

  1. https://i.ytimg.com/vi/V_zwalcR8DU/maxresdefault.jpg

  2. https://i.ytimg.com/vi/V_zwalcR8DU/sddefault.jpg

  3. https://i.ytimg.com/vi/V_zwalcR8DU/hqdefault.jpg

  4. https://i.ytimg.com/vi/V_zwalcR8DU/mqdefault.jpg

通过API访问图像

  1. 首先在Google API控制台上获取您的公开API密钥。
  2. 根据API文档中的YouTube缩略图参考,您需要访问snippet.thumbnails上的资源。
  3. 按照这个,你需要像这样短语你的URL –

    http://www.googleapis.com/youtube/v3/videos?part=snippet&id= yourVideoId &key = yourApiKey

现在将您的VideoId和yourApiKey更改为您的videoid和api-key,其响应将是JSON输出,为您提供片段variables(如果全部可用)的缩略图中的4个链接。

  function get_video_thumbnail( $src ) { $url_pieces = explode('/', $src); if( $url_pieces[2] == 'dai.ly'){ $id = $url_pieces[3]; $hash = json_decode(file_get_contents('https://api.dailymotion.com/video/'.$id.'?fields=thumbnail_large_url'), TRUE); $thumbnail = $hash['thumbnail_large_url']; }else if($url_pieces[2] == 'www.dailymotion.com'){ $id = $url_pieces[4]; $hash = json_decode(file_get_contents('https://api.dailymotion.com/video/'.$id.'?fields=thumbnail_large_url'), TRUE); $thumbnail = $hash['thumbnail_large_url']; }else if ( $url_pieces[2] == 'vimeo.com' ) { // If Vimeo $id = $url_pieces[3]; $hash = unserialize(file_get_contents('http://vimeo.com/api/v2/video/' . $id . '.php')); $thumbnail = $hash[0]['thumbnail_large']; } elseif ( $url_pieces[2] == 'youtu.be' ) { // If Youtube $extract_id = explode('?', $url_pieces[3]); $id = $extract_id[0]; $thumbnail = 'http://img.youtube.com/vi/' . $id . '/mqdefault.jpg'; }else if ( $url_pieces[2] == 'player.vimeo.com' ) { // If Vimeo $id = $url_pieces[4]; $hash = unserialize(file_get_contents('http://vimeo.com/api/v2/video/' . $id . '.php')); $thumbnail = $hash[0]['thumbnail_large']; } elseif ( $url_pieces[2] == 'www.youtube.com' ) { // If Youtube $extract_id = explode('=', $url_pieces[3]); $id = $extract_id[1]; $thumbnail = 'http://img.youtube.com/vi/' . $id . '/mqdefault.jpg'; } else{ $thumbnail = tim_thumb_default_image('video-icon.png', null, 147, 252); } return $thumbnail; } get_video_thumbnail('https://vimeo.com/154618727'); get_video_thumbnail('https://www.youtube.com/watch?v=SwU0I7_5Cmc'); get_video_thumbnail('https://youtu.be/pbzIfnekjtM'); get_video_thumbnail('http://www.dailymotion.com/video/x5thjyz'); 
 public const string tubeThumb = "http://i.ytimg.com/vi/[id]/hqdefault.jpg"; vid.Thumbnail = tubeThumb.Replace("[id]", vid.VideoID); 

将文件另存为.js

  var maxVideos = 5; $(document).ready(function(){ $.get( "https://www.googleapis.com/youtube/v3/videos",{ part: 'snippet,contentDetails', id:'your_vedio_id', kind: 'youtube#videoListResponse', maxResults: maxVideos, regionCode: 'IN', key: 'Your_API_KEY'}, function(data){ var output; $.each(data.items, function(i, item){ console.log(item); thumb = item.snippet.thumbnails.high.url; output = '<div id="img"><img src="' + thumb + '"></div>'; $('#thumbnail').append(output); }) } ); }); 
 .main{ width:1000px; margin:auto; } #img{ float:left; display:inline-block; margin:5px; } 
 <!DOCTYPE html> <html> <head> <title>Thumbnails</title> </head> <body> <div class="main"> <ul id="thumbnail"> </ul> </div> </body> </html>