从TED嵌入代码中提取视频缩略图


Pulling video thumbnail from TED embed code

我正试图从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的这一部分,它很坎坷。

你可以试试这个

    $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

我不知道是什么驱使我回答这个问题,但这是一个(经过测试的工作)快速而肮脏的问题。你可能想在那里的某个地方进行一些验证。。如果我得到报酬来做这件事,那就不会使用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嵌入变量来获取他们用于缩略图的图像。

很明显,你不会回应输出,谁知道这是否违反了他们的TOS。事实上,我敢打赌他们至少不会让你使用这个,除非你保留这个标志(事实并非如此)。使用风险自负。