Home > Flash and Actionscript > Getting Dailymotion Video (FLV) URL

Getting Dailymotion Video (FLV) URL

Some developers asked me, if it is easy to get dailymotion flv URL? I thought, I would post how to do that.

If you know the dailymotion video-id, believe me it’s very simple to do that. In case, you don’t know how to get dailymotion video-id?  Let me give you some hints, highlighted text is the video-id:-

Once you have video-id, you can use the following php-script (dailymotion.php.txt) to do either of two things:-

  • Get flv and thumbnail-url
    • http://yourserver/dailymotion.php?video_id=<video_id>&redirect=false
      • Returns: flv=<flv_url>thumbnail=<thumbnail_url>
  • Redirect to dailymotion flv-url directly. It can be used directly from Netstream’s play (<url>) instance method
    • http://yourserver/dailymotion.php?video_id=<video_id>&redirect=true
      • Redirects (HTTP 303) to flv_url.

<?php
// PHP code for getting dailymotion flv-path and more info.
//
// Author: Abdul Qabiz
// Jan 22, 2008
//
//Params:- Requires dailymotion video_id
// USAGE:-
// 1) For information
// http://server/dailymotion.php?video_id=&redirect=true
// Above invocation would return:-
// flv=&thumbnail=

// Example:-
// URL: http://server/dailymotion.php?video_id=x44ls3&redirect=false
//
// Result:-
// flv=http://www.dailymotion.com/get/15/320x240/flv/6933315.flv?key=69f63205c80b5b5d188a10e0b7656a421316ee3&previewURL=http://limelight-315.static.dailymotion.com/dyn/preview/320x240/6933315.jpg?20080122041
//
// 2) For redirection to flv i.e. video_id to flv
// http://server/dailymotion.php?video_id=&redirect=true
// Above invocation would redirect to dailymotion flv URL
if (isset ($_GET ['video_id']) == FALSE)
{
echo "video_id is required";
exit;
}
// Requires dailymotion video_id
$url = 'http://www.dailymotion.com/swf/' . $_GET['video_id'];
if (isset($_GET['redirect']) == FALSE)
{
$redirect = 'true';
}
//Start the Curl session
$session = curl_init($url);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
curl_setopt($session, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call
$response = curl_exec($session);
$error = curl_error($session);
$result = array( 'header' => '',
'body' => '',
'curl_error' => '',
'http_code' => '',
'last_url' => '');
if ( $error != "" )
{
$result['curl_error'] = $error;
}
else
{
$header_size = curl_getinfo($session,CURLINFO_HEADER_SIZE);
$result['header'] = substr($response, 0, $header_size);
$result['body'] = substr( $response, $header_size );
$result['http_code'] = curl_getinfo($session, CURLINFO_HTTP_CODE);
$result['last_url'] = curl_getinfo($session, CURLINFO_EFFECTIVE_URL);
list($header,  $result['header']) = explode("\n\n",  $result['header'], 2);
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$urlInfo = parse_url(trim(array_pop($matches)));
//$newUrl = $urlInfo['scheme'] . '://' . $urlInfo['host'] . $urlInfo['path'] . ($urlInfo['query']?'?'.$urlInfo['query']:'');
parse_str($urlInfo['query'], $output);
$flvURL = $output ['url'];
$thumbnailURL = $output['previewURL'];
if ($redirect == "true")
{
header ("Location: ". $flvURL, TRUE, 303);
}
else
{
echo sprintf ("flv=%s&thumbnail=%s", $flvURL, $thumbnailURL);
}
}
curl_close($session);
?>

Let me tell you, you can do the entire thing on client-side without using any server-side script, though logic remains same. Look at this post and learn what technique to use :-) . Another hint, if you are consuming dailymotion rss-feed(s), you don’t need any php script to construct flv-url, you can just replace “80×40″ text by “320×240″ in media:content url.

As always, please look at the code, change, optimize and fix it to meet your requirements. I wrote it in ten minutes and just as a proof-of-concept, this is no way the best method and php-code can be much better.

Technorati Tags: , ,

Categories: Flash and Actionscript Tags:
  1. February 1st, 2008 at 13:01 | #1

    were should i keep the video_id .
    when i call this code its saying video_id is required

  2. February 7th, 2008 at 19:48 | #2

    Hi,
    I’ve copy your script on a test.php file, but it doesn’t work.
    Any Idea ?

  3. February 7th, 2008 at 20:42 | #3

    @etienne: It seems you don’t have CURL module installed on your server… It works fine, I tested..
    Install the PHP CURL extension..
    -abdul

  4. February 9th, 2008 at 11:05 | #4

    Your script has stopped working after the recent changes by dailymotion. Could you look into it?
    Regards,
    Luke

  5. Khan
    February 19th, 2008 at 14:33 | #5

    I am developer and followed exactly, but the result i no flv url not thumb url
    resut is “flv=&thumbnail=”
    Do you think dailymotion has changed the mechanism ?
    regards

  6. February 25th, 2008 at 06:10 | #6

    i am also getting only the “flv=&thumbnail=” with this script. so i have tried with
    http://www.dailymotion.com/rss/video/x44ls3
    the video info xml contain the flv url with a dynamic key. i am able to grab the flv url from my local server. but when i tried it in the remote server the flv is not downloding.
    please can you help me?

  7. suresh
    March 24th, 2008 at 21:57 | #7

    I am also doing myself on this task. But still i don’t get any result using this code.
    http://www.dailymotion.com/featured/video/x4m46o_2001-a-space-odyssey_creation
    When i am giving this url it dosen’t show anything. Mr.Khan can u send your code which ever you can get thumbnail and photo to use this code. If u send it i am very thank full to you.

  8. July 3rd, 2008 at 19:54 | #8

    It works OK, but Dailymotion has changed their system again, so the script to grab their FLV’s need to be changed too. You can find the FLV url in the source code of the video page, at addVariable(”video”,….) with preg_match and then get headers of this URL with CURL. I use it on my site and it works fine now.

  9. July 3rd, 2008 at 19:57 | #9

    @Uzeik: You are right, they have changed the system and the method you mentioned is the one that works now. I investigate and found the same :-)
    Thanks
    -abdul

  10. praveen castelino
    July 24th, 2008 at 18:39 | #10

    does anybody know to construct webshots download url?

  11. July 29th, 2008 at 14:25 | #11

    Parece que DailyMotion genera el parametro KEY=xxxx acorde a la IP del usuario.
    Yo leo el RSS del video. luego Parseo la direccion buscando el FLV…. But error 403… :(
    Heeeeelp @UZEIK

  12. August 5th, 2008 at 00:23 | #12

    Yo mismo me contesto, Solucion: Leer el RSS de el lado del cliente asi el KEY generado es el Key que sirve para hacer la descarga(No leer el RSS en el Server, para luego devolver el KEY)

  13. Durai
    September 17th, 2008 at 19:40 | #13

    Hi,
    so.addVariable(’video’, ‘dailymotion.php?video_id=x6msvc_dancing-traffic-cop_fun&redirect=true’)
    and I’ve copy your script(above php script) on a dailymotion.php
    but it doesn’t play the video,please help me..
    please give me the code

  14. SAM
    September 17th, 2008 at 20:15 | #14

    =>I followed the method posted by “Suresh” , but stil am not getting the “FLV URL”….
    vieo_id= ‘xxxxxxxxxxxxxxxx’
    so.addVariable(’video’,dailymotion.php?video_id=videoid&redirect=true’)
    and i’ve copied the above script on dailymotion.php..
    but still not working.

  15. Ayyadurai
    October 4th, 2008 at 17:02 | #15

    Hi Friends,
    This is correct FLV Path for dailymotion video
    try this i have implemented this is working..
    http://www.dailymotion.com/get/17/320×240/flv/11034301.flv?key=78db4df648c088468478aaefd429d4701371041

  16. rmic
    November 12th, 2008 at 16:25 | #16

    can somebody post the last php content with the corrections please :) thank you

  17. Roman
    December 29th, 2008 at 21:12 | #17

    Dear Abdul,
    it’s a bit offtopic for this post, but hope you would forgive me :)
    Do you know of any solution to get a final (after redirect) url for progressively downloaded FLV? Either AS2 or AS3.
    We have Sockets in AS3 and can use it to send a request and parse reply, get new location and start netstream with that location. But my specific setup is a cross-domain request and I need a cookie in a request. I can make a javascript call to the second domain and get cookie as a js reply and provide this cookie to SWF via flashvars.
    But this is the least desired method.
    It would be handy to get this string directly from netstream, but looks like there’s no such option. Probably we can rewrite or extend netstream class with the desired behavior.
    Thank you for any responce!
    BR, Roman

  18. Ed
    February 19th, 2009 at 04:58 | #18

    Any way to get this to work now?

  19. March 7th, 2009 at 01:52 | #19

    Hi Guys
    It’s been while since I updated this script, not sure it works now or not. Dailymotion keeps changing the way they do things.
    Sorry for delaying replying. Specially, Roman sorry I have been so occupied couldn’t even read some comments. Shame on me :-(
    I hope, you guys have figured things you asked here.
    I would take care of these in future.
    Thanks
    -abdul

  20. doonrothmani
    March 16th, 2009 at 23:03 | #20

    the string “http://www.dailymotion.com/rss/video/” is the correct feed to get the underlying flv. running this on your local server will work. it’s a sandbox violation though, if you run it from a web server
    if would seem that a solution for this might be
    feeding a php file with “http://www.dailymotion.com/rss/video/” as the url parameter, but this returns an incorrect flv key value from daily motion. if you do this in a browser you will get a “cute” message from dailymotion chiding you for trying to “sneak around” so this result is obviously deliberate on dailymotion’s part.
    parsing the html from http://www.dailymotion.com/video/ will also provide you with an incorrect flv key value.
    so there seems to be no OBVIOUS way to broadcast or download the underlying daily motion flv from a web server.
    the good news is its fairly easy, using the rss feed, to download and broadcast dailymotion flvs from your pc.

  21. Ayyadurai
    April 16th, 2009 at 16:20 | #21

    Hi Guys,
    I think the dailymotion video flv has been changed Please can any post it.