2013-06-29 51 views
0

我试图从TED嵌入代码中提取视频缩略图。为什么?那么,我使用的WordPress主题使用自定义字段来处理视频,但该字段的缩略图功能不是为TED构建的。我试图重新调整它。从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的这一部分,它很颠簸。

回答

0

我不知道什么让我回答这个问题,但这里是一个(测试工作)快速和肮脏。你可能会想在那里抛出一些验证..如果我得到报酬来做到这一点,它不会使用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嵌入变量抢他们使用缩略图图像。

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

+0

如果在'src'属性具有单引号''',而不是双引号'“'?正则表达式休息。使用正则表达式来解析HTML文档是个不错的主意,请查看DomDocument .HTML不是“常规”的,所以REgex不应该是一个选项。 –

0

你可以试试这个

$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在性反应

相关问题