2013-06-29 52 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在性反應

相關問題