我希望能够从 Vimeo 获取视频缩略图。
获取 Youtube 的图片只需要这样做:
http://img.youtube.com/vi/HwP5NG-3e8I/2.jpg
有没有关于如何在Vimeo上实现的想法?
我希望能够从 Vimeo 获取视频缩略图。
获取 Youtube 的图片只需要这样做:
http://img.youtube.com/vi/HwP5NG-3e8I/2.jpg
有没有关于如何在Vimeo上实现的想法?
这是一个快速巧妙的方法,也可以选择自定义尺寸。
我去这里:
http://vimeo.com/api/v2/video/[VIDEO ID].php
下载该文件,打开后找到宽为 640 像素的缩略图,格式应如下所示:
https://i.vimeocdn.com/video/[LONG NUMBER HERE]_640.jpg
您取得这个链接,将其中的640更改为1400(例如),然后您就可以得到类似于以下内容:
https://i.vimeocdn.com/video/[LONG NUMBER HERE]_1400.jpg
将其粘贴到您的浏览器搜索栏中并享受。祝福。使用Vimeo网址(https://player.vimeo.com/video/30572181),这是我的示例
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<title>Vimeo</title>
</head>
<body>
<div>
<img src="" id="thumbImg">
</div>
<script>
$(document).ready(function () {
var vimeoVideoUrl = 'https://player.vimeo.com/video/30572181';
var match = /vimeo.*\/(\d+)/i.exec(vimeoVideoUrl);
if (match) {
var vimeoVideoID = match[1];
$.getJSON('http://www.vimeo.com/api/v2/video/' + vimeoVideoID + '.json?callback=?', { format: "json" }, function (data) {
featuredImg = data[0].thumbnail_large;
$('#thumbImg').attr("src", featuredImg);
});
}
});
</script>
</body>
</html>
看起来api/v2已经失效了。
为了使用新的API,您需要注册您的应用程序,并将client_id
和client_secret
进行base64编码,并作为授权头部传递。
$.ajax({
type:'GET',
url: 'https://api.vimeo.com/videos/' + video_id,
dataType: 'json',
headers: {
'Authorization': 'Basic ' + window.btoa(client_id + ":" + client_secret);
},
success: function(data) {
var thumbnail_src = data.pictures.sizes[2].link;
$('#thumbImg').attr('src', thumbnail_src);
}
});
为了安全起见,你可以从服务器返回已编码的client_id
和client_secret
。
将Karthikeyan P的答案分解,以便在更广泛的场景中使用:
// Requires jQuery
function parseVimeoIdFromUrl(vimeoUrl) {
var match = /vimeo.*\/(\d+)/i.exec(vimeoUrl);
if (match)
return match[1];
return null;
};
function getVimeoThumbUrl(vimeoId) {
var deferred = $.Deferred();
$.ajax(
'//www.vimeo.com/api/v2/video/' + vimeoId + '.json',
{
dataType: 'jsonp',
cache: true
}
)
.done(function (data) {
// .thumbnail_small 100x75
// .thumbnail_medium 200x150
// 640 wide
var img = data[0].thumbnail_large;
deferred.resolve(img);
})
.fail(function(a, b, c) {
deferred.reject(a, b, c);
});
return deferred;
};
从 Vimeo 视频 URL 获取 Vimeo Id:
var vimeoId = parseVimeoIdFromUrl(vimeoUrl);
getVimeoThumbUrl(vimeoIds[0])
.done(function(img) {
$('div').append('<img src="' + img + '"/>');
});
function parseVideo(url) {
// - Supported YouTube URL formats:
// - http://www.youtube.com/watch?v=My2FRPA3Gf8
// - http://youtu.be/My2FRPA3Gf8
// - https://youtube.googleapis.com/v/My2FRPA3Gf8
// - Supported Vimeo URL formats:
// - http://vimeo.com/25451551
// - http://player.vimeo.com/video/25451551
// - Also supports relative URLs:
// - //player.vimeo.com/video/25451551
url.match(/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/);
if (RegExp.$3.indexOf('youtu') > -1) {
var type = 'youtube';
} else if (RegExp.$3.indexOf('vimeo') > -1) {
var type = 'vimeo';
}
return {
type: type,
id: RegExp.$6
};
}
function getVideoThumbnail(url, cb) {
var videoObj = parseVideo(url);
if (videoObj.type == 'youtube') {
cb('//img.youtube.com/vi/' + videoObj.id + '/maxresdefault.jpg');
} else if (videoObj.type == 'vimeo') {
$.get('http://vimeo.com/api/v2/video/' + videoObj.id + '.json', function(data) {
cb(data[0].thumbnail_large);
});
}
}
function getVimeoInfo($link)
{
if (preg_match('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~', $link, $match))
{
$id = $match[1];
}
else
{
$id = substr($link,10,strlen($link));
}
if (!function_exists('curl_init')) die('CURL is not installed!');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$id.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = unserialize(curl_exec($ch));
$output = $output[0];
curl_close($ch);
return $output;
}`
//在下面的函数中传递缩略图的URL。
function save_image_local($thumbnail_url)
{
//for save image at local server
$filename = time().'_hbk.jpg';
$fullpath = '../../app/webroot/img/videos/image/'.$filename;
file_put_contents ($fullpath,file_get_contents($thumbnail_url));
return $filename;
}
更新:此解决方案于2018年12月已停止使用。
我也在寻找同样的内容,看起来大多数答案都已过时,因为Vimeo API v2已被弃用。
我的php建议:
$vidID = 12345 // Vimeo Video ID
$tnLink = json_decode(file_get_contents('https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/' . $vidID))->thumbnail_url;
通过上述操作,您将获得Vimeo默认缩略图的链接。
如果您想使用不同尺寸的图片,可以添加类似以下内容:
$tnLink = substr($tnLink, strrpos($tnLink, '/') + 1);
$tnLink = substr($tnLink, 0, strrpos($tnLink, '_')); // You now have the thumbnail ID, which is different from Video ID
// And you can use it with link to one of the sizes of crunched by Vimeo thumbnail image, for example:
$tnLink = 'https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F' . $tnLink . '_1280x720.jpg&src1=https%3A%2F%2Ff.vimeocdn.com%2Fimages_v6%2Fshare%2Fplay_icon_overlay.png';
如果您不需要自动化解决方案,可以在此处输入vimeo ID以查找缩略图URL:http://video.depone.eu/
2020解决方案:
我编写了一个使用Vimeo Oembed API的PHP函数。
/**
* Get Vimeo.com video thumbnail URL
*
* Set the referer parameter if your video is domain restricted.
*
* @param int $videoid Video id
* @param URL $referer Your website domain
* @return bool/string Thumbnail URL or false if can't access the video
*/
function get_vimeo_thumbnail_url( $videoid, $referer=null ){
// if referer set, create context
$ctx = null;
if( isset($referer) ){
$ctxa = array(
'http' => array(
'header' => array("Referer: $referer\r\n"),
'request_fulluri' => true,
),
);
$ctx = stream_context_create($ctxa);
}
$resp = @file_get_contents("https://vimeo.com/api/oembed.json?url=https://vimeo.com/$videoid", False, $ctx);
$resp = json_decode($resp, true);
return $resp["thumbnail_url"]??false;
}
使用方法:
echo get_vimeo_thumbnail_url("1084537");
实际上,提出这个问题的人发布了自己的答案。
“Vimeo似乎希望我发出HTTP请求,并从他们返回的XML中提取缩略图URL...”
Vimeo API文档在此处:http://vimeo.com/api/docs/simple-api
简而言之,您的应用程序需要向以下URL发出GET请求:
http://vimeo.com/api/v2/video/video_id.output
解析返回的数据以获取所需的缩略图URL,然后下载该URL上的文件。
http://i.vimeocdn.com/video/201990344.webp
它会返回错误的缩略图。这是我的测试视频链接https://vimeo.com/201990344
。 - Farmer