Posts Tagged ‘dailymotion’

How to get video thumbnails from dailymotion?

Sunday, January 6th, 2008

You can get thumbnails from YouTube easily using their API, but Dailymotion does not have API or any other intuitive way to download jpeg from specific video. I made a little research and found a solution, that at least work for me. When you are scanning webpages for videos you find embed tags with videos similiar to the one below:

<embed src="http://www.dailymotion.com/swf/IHnP80wxWqFSlg4ms" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" height="363" width="425"></embed>

At first I thought that I can get thumbnail using swf parameter IHnP80wxWqFSlg4ms. But it was wrong. We need first get the effective URL (after HTTP 302, or HTTP Location header forwarding). We can do it using libcurl:

function getEffectiveUrl($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_exec($ch);>

  $newurl = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
  curl_close($ch);

  return urldecode($newurl);
}

Our effective URL looks like this:

http://www.dailymotion.com/flash/flvplayer.swf?rev=1198003823&
statEnabled=1&selfURL=http://www.dailymotion.com/Gregouze7
/video/x2a39c_just-jack-starz-in-their-eyes_music&
(it is really long!)

Now we can get from the effective URL two important information - key and title using the following simple regular expression:

#video/([a-z0-9]+)_([a-z0-9-]+)#i"

So our key is x2a39c. We fetch the thumbnail from:

http://www.dailymotion.com/thumbnail/320×240/video/[key]

And that’s it. There also other sizes available like 160×120.