doudi8829 2013-06-29 00:36
浏览 35

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

I'm trying to pull out the video thumbnail from the TED video embed code. Why? Well, I'm using a WordPress theme that uses a custom field to handle video but the thumbnail function for that field isn't built for TED. I'm trying to re-jig it.

Here's the video thumbnail retrieval function (where YouTube and Vimeo are covered):

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;

 }

Here's a typical TED 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>

And the TED API docs if that helps at all: http://developer.ted.com/API_Docs

I seem to be having trouble customizing the preg_match and/or $get_vimeo_thumb portions (at least that's what I think is going on). Basically, I'm learning this portion of PHP and it's bumpy.

  • 写回答

2条回答 默认 最新

  • donglanzhan7151 2013-06-29 01:05
    关注

    I don't know what possessed me to answer this question, but here is a (tested working) quick and dirty. You'll probably want to throw some validation in there somewhere.. And if I were getting paid to do this it wouldn't be using file_get_contents and I'd probably use 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);
    

    We are taking the src of the iframe, getting the contents, and scrapping the JS embed variable to grab the image they use for the thumbnail.

    Obviously you won't echo the output, and who knows if this is against their TOS. As a matter of fact I'd bet they at least wouldn't let you use this unless you kept the logo (which is not the case). Use at your own risk.

    评论

报告相同问题?