[GET] Dailymotion Video Downloader (PHP)

HisInfernal

Registered Member
Joined
Dec 4, 2012
Messages
78
Reaction score
12
I decide to share one script with BHW community.
Its very simple Dailymotion video downloader.
Save this code in .php file (ex. getDailymotion.php) and execute it by: php getDailymotion.php "VIDEOURL"

Script will create directory called videos/ and place downloaded videos into it with random names.

PHP:
<?php
if(empty($argv[1])){
    print "======\nSyntax: ".$argv[0]." \"DAILYMOTIONURL\" \n======\n";die();
}else{$tubePage=$argv[1];}


$videosFolder="videos/";
if (!file_exists($videosFolder)){mkdir($videosFolder, 0777, true);}


function randomName($length=10) {
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}


function downloadFile($url, $path) {  
    $newfname = $path;
  $file = fopen ($url, "rb");
  if ($file) {
    $newf = fopen ($newfname, "wb");
        print "\nDownloading video";
    if ($newf)
    while(!feof($file)) {
      fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
      print ".";
    }
  }
  if ($file) {fclose($file);}
  if ($newf) {fclose($newf);}
}


function getVideoUrl($url){
    global $cookiejar;


    $userAgent = "Mozilla/5.0 (X11; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0";
    $headers = array("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                                                                                                                                    "Accept-Language: en-us,en;q=0.5");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_PORT , 80);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
    curl_setopt($ch, CURLOPT_COOKIE, 'family_filter=off;');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    preg_match("/name=\"flashvars\"\svalue=\"(.*?)\"/i",$output,$flashvars);
    if(isset($flashvars[1])){
        $flashvars=urldecode($flashvars[1]);
        preg_match("/\"video_url\":\"(.*?)\"/i",$flashvars,$flvUrl);
        if(empty($flvUrl[1])){
            preg_match("/\"sdURL\":\"(.*?)\"/i",$flashvars,$flvUrl);
        }
    }
    if(isset($flvUrl[1])){
        return urldecode($flvUrl[1]);
    }else{
        return false;
    }
}


if($flvUrl=getVideoUrl($tubePage)){
  $flvUrl=str_replace("\/","/",$flvUrl);
  $flvFilename=randomName();
  downloadFile($flvUrl,$videosFolder.$flvFilename.".mp4");
}else{echo "Unable to get video from Dailymotion.";}
?>
 
Can you give my help on how to use it on my remote server ?
I upload the php file by ftp and ???

Thanks
 
I found a way to add a parameter via HTTP_GET_VARS.


Thanks anyway :o)
 
Do you have a method to get the best quality possible of the video ?
Because your script only download the lowest quality.....

Thanks for helping me in advance.
 
Hi the below code is not working for me, I dont know where should we pass dailymotion url. Can anyone please help me with the below code

PHP:
<?php
if(empty($argv[1])){
    print "======\nSyntax: ".$argv[0]." \"DAILYMOTIONURL\" \n======\n";die();
}else{$tubePage=$argv[1];}


$videosFolder="videos/";
if (!file_exists($videosFolder)){mkdir($videosFolder, 0777, true);}


function randomName($length=10) {
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}


function downloadFile($url, $path) {  
    $newfname = $path;
  $file = fopen ($url, "rb");
  if ($file) {
    $newf = fopen ($newfname, "wb");
        print "\nDownloading video";
    if ($newf)
    while(!feof($file)) {
      fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
      print ".";
    }
  }
  if ($file) {fclose($file);}
  if ($newf) {fclose($newf);}
}


function getVideoUrl($url){
    global $cookiejar;


    $userAgent = "Mozilla/5.0 (X11; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0";
    $headers = array("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                                                                                                                                    "Accept-Language: en-us,en;q=0.5");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_PORT , 80);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
    curl_setopt($ch, CURLOPT_COOKIE, 'family_filter=off;');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    preg_match("/name=\"flashvars\"\svalue=\"(.*?)\"/i",$output,$flashvars);
    if(isset($flashvars[1])){
        $flashvars=urldecode($flashvars[1]);
        preg_match("/\"video_url\":\"(.*?)\"/i",$flashvars,$flvUrl);
        if(empty($flvUrl[1])){
            preg_match("/\"sdURL\":\"(.*?)\"/i",$flashvars,$flvUrl);
        }
    }
    if(isset($flvUrl[1])){
        return urldecode($flvUrl[1]);
    }else{
        return false;
    }
}


if($flvUrl=getVideoUrl($tubePage)){
  $flvUrl=str_replace("\/","/",$flvUrl);
  $flvFilename=randomName();
  downloadFile($flvUrl,$videosFolder.$flvFilename.".mp4");
}else{echo "Unable to get video from Dailymotion.";}
?>
[/QUOTE]
 
Back
Top