All the current tutorials on making your own youtube video downloader are outdated. The whole token insertion from video info does not work, and neither does using the javas
Why do you want to reinvent the wheel? use Rapidleech script!
Btw, if you still need to reinvent the wheel, here is source-code of the Youtube plugin of Rapidleech (it's highly readable:
class youtube_com extends DownloadClass {
/*
Some blah blah about the age verification and erroneous URLs
$fmt is quality/format number and is an integer
*/
public function Download($link) {
$this->fmts = array(38,37,22,45,35,44,34,43,18,5,17);
$yt_fmt = empty($_REQUEST['yt_fmt']) ? '' : $_REQUEST['yt_fmt'];
$this->fmturlmaps = $this->GetVideosArr($fmt_url_maps);
if (empty($yt_fmt) && !isset($_GET["audl"])) return $this->QSelector($link);
elseif (isset($_REQUEST['ytube_mp4']) && $_REQUEST['ytube_mp4'] == 'on' && !empty($yt_fmt)) {
//look for and download the highest quality we can find?
if ($yt_fmt == 'highest') {
foreach ($this->fmts as $fmt) {
if (array_key_exists($fmt, $this->fmturlmaps)) {
$furl = $this->fmturlmaps[$fmt];
break;
}
}
} else { //get the format the user specified (making sure it actually exists)
if (!$furl = $this->fmturlmaps[$yt_fmt]) html_error ('Specified video format not found');
$fmt = $yt_fmt;
}
} else { //just get the one Youtube plays by default (in some cases it could also be the highest quality format)
$fmt = key($this->fmturlmaps);
$furl = $this->fmturlmaps[$fmt];
}
if (preg_match ('%^5|34|35$%', $fmt)) $ext = '.flv';
elseif (preg_match ('%^17$%', $fmt)) $ext = '.3gp';
elseif (preg_match ('%^18|22|37|38$%', $fmt)) $ext = '.mp4';
elseif (preg_match ('%^43|44|45$%', $fmt)) $ext = '.webm';
else $ext = '.flv';
if (!preg_match('#(.*)\s+-\sYouTube[\r|\n|\t|\s]* #Us', $this->page, $title)) html_error('No video title found! Download halted.');
if (!preg_match ('/video_id=(.+?)(\\\|"|&|(\\\u0026))/', $this->page, $video_id)) html_error('Video id not found.');
$FileName = str_replace (Array ("\\", "/", ":", "*", "?", "\"", "<", ">", "|"), "_", html_entity_decode(trim($title[1]), ENT_QUOTES)) . "-[{$video_id[1]}][f$fmt]$ext";
if (stristr($furl, '|')) {
$u_arr = explode('|', $furl);
$furl = preg_replace('#://([^/]+)#', "://".$u_arr[2], $u_arr[0]);
}
if (isset($_REQUEST['ytdirect']) && $_REQUEST['ytdirect'] == 'on')
{
echo "
Click here or copy the link to your download manager to download
";
echo "";
}
else
{
$this->RedirectDownload (urldecode($furl), $FileName, $this->cookie, 0, 0, $FileName);
}
}