我正在尝试从TED视频嵌入代码中提取视频缩略图。为什么?我使用的是一个WordPress主题,它使用了一个自定义字段来处理视频,但是该字段的缩略图函数并不是为TED构建的。我在试着重新拼接它。
这里是视频缩略图检索功能(其中包括YouTube和Vimeo ):
function woo_get_video_image($embed) {
$video_thumb = '';
/* Let's start by looking for YouTube, then Vimeo */
if ( preg_match( '/youtube/', $embed ) ) {
// YouTube - get the video code if this is an embed code (old embed)
preg_match( '/youtube\.com\/v\/([\w\-]+)/', $embed, $match);
// YouTube - if old embed returned an empty ID, try capuring the ID from the new iframe embed
if( !isset($match[1]) )
preg_match( '/youtube\.com\/embed\/([\w\-]+)/', $embed, $match);
// YouTube - if it is not an embed code, get the video code from the youtube URL
if( !isset($match[1]) )
preg_match( '/v\=(.+)&/',$embed ,$match);
// YouTube - get the corresponding thumbnail images
if( isset($match[1]) )
$video_thumb = "http://img.youtube.com/vi/".$match[1]."/0.jpg";
} else if ( preg_match( '/vimeo/', $embed ) ) {
// Vimeo - get the video thumbnail
preg_match( '#http://player.vimeo.com/video/([0-9]+)#s', $embed, $match );
if ( isset($match[1]) ) {
$video_id = $match[1];
// Try to get a thumbnail from Vimeo
$get_vimeo_thumb = unserialize(file_get_contents_curl('http://vimeo.com/api/v2/video/'. $video_id .'.php'));
$video_thumb = $get_vimeo_thumb[0]['thumbnail_large'];
}
}
// return whichever thumbnail image you would like to retrieve
return $video_thumb;
}这是一个典型的TED嵌入:
<iframe
src="http://embed.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes.html"
width="560" height="315"
frameborder="0"
scrolling="no"
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>TED API文档如果有帮助的话:http://developer.ted.com/API_Docs
我似乎在定制preg_match和/或$get_vimeo_thumb部分时遇到了麻烦(至少我是这么认为的)。基本上,我正在学习PHP的这一部分,它是颠簸的。
发布于 2018-01-09 19:54:01
你可以试试这个
$source = 'http://www.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes';
$tedJson = json_decode(file_get_contents('http://www.ted.com/talks/oembed.json?url='.urlencode($source)), TRUE);
pr($tedJson);您将得到json作为响应。
发布于 2013-06-29 09:05:12
我不知道是什么驱使我回答这个问题,但这里有一个(经过测试的)快速和肮脏的工作。您可能希望在其中的某个地方添加一些验证..如果我这样做是有报酬的,我就不会使用file_get_contents,我可能会使用DOMDocument。
$embed = '<iframe
src="http://embed.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes.html"
width="560" height="315"
frameborder="0"
scrolling="no"
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>';
function getThumbnail($embed){
preg_match("/src\=\"(.+)?\"/", $embed, $matches);
$uri = $matches[1];
preg_match("/posterUrl\s=\s\'(.+)?\'/", file_get_contents($uri), $matches);
echo $matches[1];
}
getThumbnail($embed);我们将获取iframe的src,获取内容,并删除JS embed变量以获取他们用于缩略图的图像。
显然,您不会回显输出,谁知道这是否违反了他们的TOS。事实上,我敢打赌,他们至少不会让你使用这个,除非你保留了这个徽标(事实并非如此)。使用风险自负。
https://stackoverflow.com/questions/17375550
复制相似问题