<?php

if ($argc != || in_array($argv[1], array('--help''-help''-h''-?'))) {
?>

This is a command line PHP script with one option.

 Usage:
 <?php echo $argv[0]; ?> <userName>

 <userName> can be name of Youtube user, which videos you want to download. With the --help, -help, -h,
 or -? options, you can get this help.

<?php
} else {
error_reporting(E_ALL);
require_once 
'Zend/Loader.php'// the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();

function 
getFlvUrl($videoWatchPageUrl)
{
    
usleep(rand(1,2000000));
    
$ch curl_init (); 
    
curl_setopt ($chCURLOPT_URL$videoWatchPageUrl );
    
curl_setopt ($chCURLOPT_COOKIEJAR'cookies.tmp' );
    
curl_setopt ($chCURLOPT_RETURNTRANSFERTRUE);
    
curl_setopt ($chCURLOPT_USERAGENT"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar)" ); 
    
$data=curl_exec($ch);
    if (
preg_match_all('~&t=[^&]*~'$data$matches))
    {
        
$t $matches[0][0];
        
$t preg_split('/=/'$t);
        
$t $t[1];
        
$v $videoWatchPageUrl;
        
$v preg_split ('/\?v=/'$v);
        
$v $v[1];
        return 
'http://www.youtube.com/get_video.php?video_id=' $v '&t=' .  $t '&.flv';        
    } else {
        return 
false;
    }    
}

function 
downloadVideoFile($videoFlvUrl,$videoWatchPageUrl)
{
    
usleep(rand(1,2000000));
    
$ch curl_init (); 
    
curl_setopt ($chCURLOPT_URL$videoFlvUrl );
    
curl_setopt ($chCURLOPT_REFERER$videoWatchPageUrl );
    
curl_setopt ($chCURLOPT_COOKIEJAR'cookies.tmp' );
    
//curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
    //curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
$fp fopen("temp.flv""w");
    
curl_setopt($chCURLOPT_FILE$fp);
    
curl_setopt ($chCURLOPT_BINARYTRANSFERTRUE);
    
curl_setopt ($chCURLOPT_FAILONERRORtrue); 
    
curl_setopt ($chCURLOPT_FOLLOWLOCATION1); 
    
curl_setopt ($chCURLOPT_USERAGENT"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar)" ); 
    
$result=curl_exec($ch);
    
$curl_error=curl_error($ch);
    
curl_close ($ch);
    
fclose($fp);
    if(!
$result)
    {
        echo 
"curl eror='$curl_error'";
        
unlink('temp.flv');
        return 
false;
    }
    return 
true;
}

function 
getVideoEntry($videoEntry,$userName
{
    
// the videoEntry object contains many helper functions that access the underlying mediaGroup object
    
$videoId=$videoEntry->getVideoId();
    
$videoTitle=$videoEntry->getVideoTitle();
    
$videoWatchPageUrl=$videoEntry->getVideoWatchPageUrl();
    echo 
"\nDownloading $videoTitle ...";
    
$fileName=preg_replace('~[\/:*?"<>|\']~','',$videoTitle)." ($videoId).flv";
    if(
file_exists($userName.'/'.$fileName))
    {
        echo 
"... Skipped";
        return;
    }
    for(
$ii=0$ii<5$ii++)
    {
        echo 
"*";
        
$videoFlvUrl=getFlvUrl($videoWatchPageUrl);
        if( 
downloadVideoFile($videoFlvUrl,$videoWatchPageUrl) )
        {
            
rename('temp.flv',$userName.'/'.$fileName);
            echo 
"... Ok";
            return;
        }
    }
    echo 
"... Failed";
}

function 
getVideoFeed($videoFeed,$userName
{
  global 
$c;
  foreach (
$videoFeed as $videoEntry) {
    echo 
"\n#".++$c."\t";
    
getVideoEntry($videoEntry,$userName);
  }
}

function 
getAndDownloadUserUploads($userName)
{     
  
$fp=fopen('cookies.tmp','w');
  
fclose($fp);
  if(!
file_exists($userName))
 {
      
mkdir($userName);
 }
  
$yt = new Zend_Gdata_YouTube();
  
$url="http://gdata.youtube.com/feeds/api/users/$userName/uploads";
  
$videoFeed $yt->getVideoFeed($url);
  
$resultsPerPage=50;
  
$pages=ceil$videoFeed->getTotalResults()->getText() / $resultsPerPage );
  for(
$ii=1$ii<=$pages$ii++)
  {
      
$startIndex=($ii-1)*$resultsPerPage+1;
      
getVideoFeed($videoFeed $yt->getVideoFeed($url."?max-results=$resultsPerPage&start-index=$startIndex"),$userName);
  }  
}

$c=0;
$userName=$argv[1];
echo 
"\nstarting downloading Youtube videos of $userName";
getAndDownloadUserUploads($userName);
`
tar -zcf $userName.tar.gz $userName`;
echo 
"\npack $userName.tar.gz is ready!\n";

}
?>