Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a youtube's video static image using javascript?

In video gallerys YouTube shows images of the videos instead of the flash player. If you click on the image, you are redirected to a page where is the flash video player. I want to show the first static images.

How can I do that programmatically?

like image 462
eKek0 Avatar asked Aug 05 '09 03:08

eKek0


2 Answers

For Javascript: (I'm assuming you tagged it as flash because Youtube is a flash video player)

function getScreen( url, size )
{
  if(url === null){ return ""; }

  size = (size === null) ? "big" : size;
  var vid;
  var results;

  results = url.match("[\\?&]v=([^&#]*)");

  vid = ( results === null ) ? url : results[1];

  if(size == "small"){
    return "http://img.youtube.com/vi/"+vid+"/2.jpg";
  }else {
    return "http://img.youtube.com/vi/"+vid+"/0.jpg";
  }
}

Found here.

Going off of this function, basically you just have to take the v=ladlfasd parameter and put it in this url:

http://img.youtube.com/vi/(v= parameter)/2.jpg

Where it is 2.jpg for small, 0.jpg for large

like image 191
Tyler Carter Avatar answered Nov 15 '22 07:11

Tyler Carter


function getYoutubeThumbnail($url)
{

    if(preg_match('![?&]{1}v=([^&]+)!', $url . '&', $m))
    {
        $videoid = $m[1];
    }
    else if(preg_match('~/v/([0-9a-z_]+)~i', $url, $m))
    {
        $videoid = $m[1];
    }

    $youtube_thumbnail = 'http://img.youtube.com/vi/' . $videoid . '/default.jpg';

    $c = curl_init();

    $url = trim($url);

    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($c, CURLOPT_URL, $url);

    $contents = curl_exec($c);

    curl_close($c);


    $feed = "http://gdata.youtube.com/feeds/api/videos/".$videoid;

    $newInfo = trim(@file_get_contents($feed));

    preg_match('/<media:title(.*?)<\/media:title>/', $newInfo, $result);

    $title = strip_tags($result[0]);

    preg_match('/<media:keywords(.*?)<\/media:keywords>/', $newInfo, $result);

     $desc = strip_tags(str_replace(",", "", $result[0]));

     //embed path
     $embed_path = "http://www.youtube.com/embed/".$videoid;



    $youtube_info = array('videoid' => $videoid,'title' => $title, 'description' => $desc,'youtube_thumbnail' => $youtube_thumbnail,'embed_path' => $embed_path) ;

    return $youtube_info;

}
like image 41
chetanspeed511987 Avatar answered Nov 15 '22 06:11

chetanspeed511987