提问
如果我有YouTube视频网址,有没有办法使用PHP和cURL从YouTube API获取相关缩略图?
最佳参考
每个YouTube视频都有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.jpg
,2.jpg
,3.jpg
之一)是:https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
对于高质量版本的缩略图,请使用与此类似的网址:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
还有一个中等质量版本的缩略图,使用类似于总部的网址:
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
所有上述网址都可通过http获取。此外,略短的主机名
i3.ytimg.com
代替上面示例网址中的img.youtube.com
。或者,您可以使用YouTube Data API(v3)获取缩略图。[78]
其它参考1
您可以使用 YouTube数据API 来检索视频缩略图,标题,说明,评分,统计信息等。 API版本3需要密钥*。获取密钥并创建视频:列表请求:[79] [80]
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请求的数量。但是,每天几百万的请求是免费的。
来源文章。[81]
其它参考2
亚萨说的是对的。但是,并非每个YouTube视频都包含所有九个缩略图,但每个视频都有七个缩略图。他们是:
(图像大小取决于视频。)
- 播放器背景缩略图(480x360像素)
https://i1.ytimg.com/vi/G0wGs3useV8/0.jpg
- 启动缩略图(120x90像素)
https://i1.ytimg.com/vi/G0wGs3useV8/1.jpg
- 中间缩略图(120x90像素)
https://i1.ytimg.com/vi/G0wGs3useV8/2.jpg
- 结束缩略图(120x90像素)
https://i1.ytimg.com/vi/G0wGs3useV8/3.jpg
- 高品质缩略图(480x360像素)
https://i1.ytimg.com/vi/G0wGs3useV8/hqdefault.jpg
- 中等质量缩略图(320x180像素)
https://i1.ytimg.com/vi/G0wGs3useV8/mqdefault.jpg
- 普通质量缩略图(120x90像素)
https://i1.ytimg.com/vi/G0wGs3useV8/default.jpg
此外,接下来的两个缩略图可能存在也可能不存在。对于HQ视频
它们存在。
- 标准清晰度缩略图(640x480像素)
https://i1.ytimg.com/vi/G0wGs3useV8/sddefault.jpg
- 最大分辨率缩略图(1920x1080像素)
https://i1.ytimg.com/vi/G0wGs3useV8/maxresdefault.jpg
您可以获取JavaScript和PHP脚本来检索缩略图和其他
YouTube中的信息
- 如何使用PHP获取YouTube视频信息
- 使用JavaScript检索YouTube视频详细信息 - JSON& API v2
还可以使用YouTube Video Information Generator工具获取所有内容
提交视频的YouTube视频信息
网址或视频ID。[82] [83] [84] [85]
其它参考3
在YouTube API V3中,我们还可以使用这些网址获取缩略图...它们会根据质量进行分类。
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的一个优点是这些URL不会被防火墙阻止。
其它参考4
如果您想从YouTube获取特定视频ID的最大图片,那么网址应如下所示:
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);
其它参考5
如果你想摆脱黑条并像YouTube一样去做,你可以使用:
https://i.ytimg.com/vi_webp/<video id>/mqdefault.webp
如果你不能使用
.webp
扩展,你可以这样做:https://i.ytimg.com/vi/<video id>/mqdefault.jpg
此外,如果您需要非标定版本,请使用
maxresdefault
而不是mqdefault
。注意:如果您打算使用
maxresdefault
,我不确定宽高比。其它参考6
我的功能是只从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;
}
其它参考7
在YouTube Data API v3中,您可以使用视频 - >列表功能获取视频缩略图。从snippet.thumbnails。(键),您可以选择默认,中等或高分辨率缩略图,并获取其宽度,高度和网址[86] [87] [88]
您还可以使用缩略图 - >设置功能更新缩略图。[89]
例如,您可以查看YouTube API示例项目。 (PHP的。)[90] [91]
其它参考8
您可以获取包含视频缩略图URL的视频条目。链接中有示例代码。或者,如果你想解析XML,那里有信息。返回的XML有一个
media:thumbnail
元素,它包含缩略图的URL。[92] [93]其它参考9
// 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;
其它参考10
YouTube由谷歌拥有,谷歌喜欢为不同的屏幕尺寸提供合理数量的图像,因此它的图像以不同的尺寸存储,下面是一个示例,说明您的缩略图将如何
低质量缩略图:
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
其它参考11
YOUTUBE API版本3 ** 2分钟内启动和运行**
如果你想要做的只是搜索YouTube并获得相关的属性:
- 获取PUBLIC API - 此链接提供了良好的指导
2.使用以下查询字符串。例如,url字符串中的搜索查询(由 q=表示)是 stackoverflow 。然后,YouTube会向您发送一个json回复,然后您可以解析缩略图,代码段,作者等[94]
https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=50&q=stackoverflow&key=YOUR_API_KEY_HERE
其它参考12
使用:
https://www.googleapis.com/youtube/v3/videoCategories?part=snippet,id&maxResults=100®ionCode=us&key=**Your YouTube ID**
以上是链接。使用它,您可以找到视频的YouTube特征。找到特征后,您可以获取所选类别的视频。之后,您可以使用 Asaph的答案找到所选的视频图像。
尝试上述方法,您可以解析YouTube API中的所有内容。[96]
其它参考13
我以这种方式使用了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阻止直接从其服务器中包含图像
其它参考14
我找到了这个漂亮的工具,可让您使用放置在图像上方的YouTube播放按钮创建图像:
- 安装在服务器上以进行脚本编写:https://github.com/halgatewood/youtube-thumbnail-enhancer
其它参考15
只是为了添加/扩展所给出的解决方案,我觉得有必要注意,因为我自己有这个问题,实际上可以通过一个HTTP请求获取多个YouTube视频内容,在这种情况下,缩略图:[97]]]
使用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);
?>
其它参考16
我为youtube缩略图创建的一个简单的php函数,类型是
- 默认
- 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;}
其它参考17
另一个好的选择是使用YouTube支持的oEmbed API。[98]
您只需将自己的YouTube网址添加到oEmbed网址,即可获得包含缩略图和嵌入的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
}
阅读文档以获取更多信息。[99]
其它参考18
如果您正在使用公共API,最好的方法是使用if语句。
如果视频是公开的或不公开的,则使用url方法设置缩略图。
如果视频是私有的,您可以使用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> Unlisted';
}
elseif($video_status == 'public'){
$video_thumbnail = 'http://img.youtube.com/vi/'.$video_url.'/mqdefault.jpg';
$video_status = '<i class="fa fa-eye"></i> Public';
}
elseif($video_status == 'private'){
$video_thumbnail = $playlistItem['snippet']['thumbnails']['maxres']['url'];
$video_status = '<i class="fa fa-lock"></i> Private';
}
其它参考19
YouTube数据API
YouTube通过Data API(v3)为我们提供每个视频的4个生成图像,对于Ex -
- https://i.ytimg.com/vi/V_zwalcR8DU/maxresdefault.jpg
- https://i.ytimg.com/vi/V_zwalcR8DU/sddefault.jpg
- https://i.ytimg.com/vi/V_zwalcR8DU/hqdefault.jpg
- https://i.ytimg.com/vi/V_zwalcR8DU/mqdefault.jpg
通过API访问图像
- 首先在Google API控制台上获取您的公共API密钥。
- 根据API文档中的每个YouTube缩略图参考,您需要访问snippet.thumbnails上的资源。
- 按此,你需要像这样说出你的网址 - [100] [101]
www.googleapis.com/youtube/v3/videos?part=snippet\u0026amp;id=yourVideoId
\u0026amp;key=yourApiKey
现在将yourVideoId和yourApiKey更改为您的相应视频ID和api-key,其响应将是一个JSON输出,为您提供片段变量缩略图中的4个链接(如果全部可用)。
其它参考20
我认为他们对缩略图有很多答案,但我想添加一些其他网址来轻松获取youtube缩略图。我只是从Asaph的回答中得到一些文字。这里有一些网址可以获得youtube缩略图
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/default.jpg
对于高质量版本的缩略图,请使用与此类似的网址:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
还有一个中等质量版本的缩略图,使用类似于总部的网址:
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
希望它能帮助不久的将来。
其它参考21
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');
其它参考22
您可以使用parse_url,parse_str从YouTube视频网址获取视频ID,然后插入图片的预测网址。感谢YouTube提供预测网址[102] [103]
$videoUrl = "https://www.youtube.com/watch?v=8zy7wGbQgfw";
parse_str( parse_url( $videoUrl, PHP_URL_QUERY ), $my_array_of_vars );
$ytID = $my_array_of_vars['v']; //gets video ID
print "https://img.youtube.com/vi/<?php print $ytID?>/maxresdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/mqdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/hqdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/sddefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/default.jpg";
您可以使用此工具生成YouTube缩略图
https://codeatools.com/get-youtube-video-thumbnails[104]
其它参考23
最佳选择针对手动使用进不带分隔符的视频ID令牌可以通过双击进行选择。
每个YouTube视频都有4个生成的图片。它们的格式可预测如下:
https://img.youtube.com/vi/YOUTUBEVIDEOID/0.jpg
https://img.youtube.com/vi/YOUTUBEVIDEOID/1.jpg
https://img.youtube.com/vi/YOUTUBEVIDEOID/2.jpg
https://img.youtube.com/vi/YOUTUBEVIDEOID/3.jpg
列表中的第一个是全尺寸图像,其他是缩略图图像。默认缩略图图像(即
1.jpg
,2.jpg
,3.jpg
之一)是:https://img.youtube.com/vi/YOUTUBEVIDEOID/default.jpg
对于高质量版本的缩略图,请使用与此类似的网址:
https://img.youtube.com/vi/YOUTUBEVIDEOID/hqdefault.jpg
还有一个中等质量版本的缩略图,使用类似于总部的网址:
https://img.youtube.com/vi/YOUTUBEVIDEOID/mqdefault.jpg
对于缩略图的标准定义版本,请使用与此类似的URL:
https://img.youtube.com/vi/YOUTUBEVIDEOID/sddefault.jpg
对于缩略图的最大分辨率版本,请使用与此类似的URL:
https://img.youtube.com/vi/YOUTUBEVIDEOID/maxresdefault.jpg
所有上述网址都可通过http获取。此外,略短的主机名
i3.ytimg.com
代替上面示例网址中的img.youtube.com
。或者,您可以使用YouTube Data API(v3)获取缩略图。[106]
其它参考24
方法1:
你可以找到youtube视频所有信息与json页面甚至有thumbnail_url
http://www.youtube.com/oembed?format=json\u0026amp;url={your video url goes here} [107]
喜欢最终网址外观+ php测试代码
$data = file_get_contents("https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=_7s-6V_0nwA");
$json = json_decode($data);
var_dump($json);
产量
object(stdClass)[1]
public 'width' => int 480
public 'version' => string '1.0' (length=3)
public 'thumbnail_width' => int 480
public 'title' => string 'how to reminder in window as display message' (length=44)
public 'provider_url' => string 'https://www.youtube.com/' (length=24)
public 'thumbnail_url' => string 'https://i.ytimg.com/vi/_7s-6V_0nwA/hqdefault.jpg' (length=48)
public 'author_name' => string 'H2 ZONE' (length=7)
public 'type' => string 'video' (length=5)
public 'author_url' => string 'https://www.youtube.com/channel/UC9M35YwDs8_PCWXd3qkiNzg' (length=56)
public 'provider_name' => string 'YouTube' (length=7)
public 'height' => int 270
public 'html' => string '<iframe width="480" height="270" src="https://www.youtube.com/embed/_7s-6V_0nwA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>' (length=171)
public 'thumbnail_height' => int 360
有关详细信息,您还可以访问https://www.youtube.com/watch?v=mXde7q59BI8
视频教程1 [108]
方法2:
使用youtube img链接
https://img.youtube.com/vi/'insert-youtube-video-id-here'/default.jpg[109]
方法3:
使用浏览器源代码使用视频网址链接获取缩略图
-go到视频源代码并搜索thumbnailurl
现在你可以使用这个url了
你的巫术代码:
{img src =https://img.youtube.com/vi/insert-youtube-video-id-here/default.jpg}
详情请见http://hzonesp.com/php/get-youtube-video-thumbnail-using-id/
要么
https://www.youtube.com/watch?v=9f6E8MeM6PI
视频教程2 [110] [111]
其它参考25
这是我为获取缩略图而创建的一个简单函数,它易于理解和使用。
$ link是与浏览器完全相同的youtube链接,例如https://www.youtube.com/watch?v=BQ0mxQXmLsk [112]
function get_youtube_thumb($link){
$new=str_replace('https://www.youtube.com/watch?v=','', $link);
$thumbnail='https://img.youtube.com/vi/'.$new.'/0.jpg';
return $thumbnail;
}
其它参考26
public const string tubeThumb = "http://i.ytimg.com/vi/[id]/hqdefault.jpg";
vid.Thumbnail = tubeThumb.Replace("[id]", vid.VideoID);
其它参考27
将文件另存为
.js
var maxVideos = 5;
$(document).ready(function(){
$.get(
"https://www.googleapis.com/youtube/v3/videos",{
part: 'snippet,contentDetails',
id:'your_video_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>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div class="main">
<ul id="thumbnail"> </ul>
</div>
</body>
</html>
package com.app.download_video_demo;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
// get Picasso jar file and put that jar file in libs folder
public class Youtube_Video_thumnail extends Activity
{
ImageView iv_youtube_thumnail,iv_play;
String videoId;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
super.setContentView(R.layout.youtube_video_activity);
init();
try
{
videoId=extractYoutubeId("http://www.youtube.com/watch?v=t7UxjpUaL3Y");
Log.e("VideoId is->","" + videoId);
String img_url="http://img.youtube.com/vi/"+videoId+"/0.jpg"; // this is link which will give u thumnail image of that video
// picasso jar file download image for u and set image in imagview
Picasso.with(Youtube_Video_thumnail.this)
.load(img_url)
.placeholder(R.drawable.ic_launcher)
.into(iv_youtube_thumnail);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
}
public void init()
{
iv_youtube_thumnail=(ImageView)findViewById(R.id.img_thumnail);
iv_play=(ImageView)findViewById(R.id.iv_play_pause);
}
// extract youtube video id and return that id
// ex--> "http://www.youtube.com/watch?v=t7UxjpUaL3Y"
// videoid is-->t7UxjpUaL3Y
public String extractYoutubeId(String url) throws MalformedURLException {
String query = new URL(url).getQuery();
String[] param = query.split("&");
String id = null;
for (String row : param) {
String[] param1 = row.split("=");
if (param1[0].equals("v")) {
id = param1[1];
}
}
return id;
}
}