<?php
/*
Filename: YouTube.php.
Copyright 2014/2015.
Author: Darby_Crash.
Email:
[email protected]
This Program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This Program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
error_reporting(0);
$version = "1.2";
strncasecmp(php_uname('s'), "Win", 3) == 0 ? $windows = true : $windows = false;
if ($windows) {
if (file_exists("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe")) {
$vlc = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
} else if (file_exists("C:\\Program Files\\VideoLAN\\VLC\\vlc.exe")) {
$vlc = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe";
} else {
$vlc = "C:\\Programmi\\VideoLAN\\VLC\\vlc.exe";
}
} else {
$vlc = "vlc";
$out = "2>/dev/null";
}
if ($argc < 2){head($argv, $version);}
function head($argv, $version){
echo "\r\n\r\n\r\n# ".$argv[0]." ".$version." by Darby_Crash";
echo "\r\n# Usage: php ".$argv[0]." \"YouTube's_video_url\"";
echo "\r\n# Required: php5-cli, php5-curl, livestreamer, wget\r\n\r\n\r\n";
die;
}
class cURL {
var $headers ;
var $user_agent ;
var $compression ;
var $cookie_file ;
var $proxy ;
function cURL ( $cookies = TRUE , $cookie = 'cookies.txt' , $compression = 'gzip' , $proxy = '' ) {
$this -> headers [] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg' ;
$this -> headers [] = 'Connection: Keep-Alive' ;
$this -> headers [] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8' ;
$this -> user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0' ;
$this -> compression = $compression ;
$this -> proxy = $proxy ;
$this -> cookies = $cookies ;
if ( $this -> cookies == TRUE ) $this -> cookie ( $cookie );
}
function cookie ( $cookie_file ) {
if ( file_exists ( $cookie_file )) {
$this -> cookie_file = $cookie_file ;
} else {
fopen ( $cookie_file , 'w' ) or $this -> error ( 'The cookie file could not be opened. Make sure this directory has the correct permissions' );
$this -> cookie_file = $cookie_file ;
fclose ( $this -> cookie_file );
}
}
function get ( $url ) {
$process = curl_init ( $url );
curl_setopt ( $process , CURLOPT_HTTPHEADER , $this -> headers );
curl_setopt ( $process , CURLOPT_HEADER , 1 );
curl_setopt ( $process , CURLOPT_USERAGENT , $this -> user_agent );
if ( $this -> cookies == TRUE ) curl_setopt ( $process , CURLOPT_COOKIEFILE , $this -> cookie_file );
if ( $this -> cookies == TRUE ) curl_setopt ( $process , CURLOPT_COOKIEJAR , $this -> cookie_file );
if ( $this -> cookies == TRUE ) curl_setopt ( $process , CURLOPT_COOKIESESSION , true );
curl_setopt ( $process , CURLOPT_ENCODING , $this -> compression );
curl_setopt ( $process , CURLOPT_TIMEOUT , 30 );
if ( $this -> proxy ) curl_setopt ( $process , CURLOPT_PROXY , $this -> proxy );
//curl_setopt ( $process , CURLOPT_PROXYTYPE , CURLPROXY_SOCKS5 );
curl_setopt ( $process , CURLOPT_SSL_VERIFYPEER , 0 );
curl_setopt ( $process , CURLOPT_SSL_VERIFYHOST , 0 );
curl_setopt ( $process , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt ( $process , CURLOPT_FOLLOWLOCATION , 0 );
$return = curl_exec ( $process );
curl_close ( $process );
return $return ;
}
}
function choose_quality($itag){
switch ($itag){
case "5":
$ret = "FLV - 240p - Sorenson H.263/MP3 64kbit/s";
$este = "flv";
break;
case "6":
$ret = "FLV - 270p - Sorenson H.263/MP3 64kbit/s";
$este = "flv";
break;
case "13":
$ret = "3GP - N/A - MPEG-4 Visual/AAC N/A";
$este = "3gp";
break;
case "17":
$ret = "3GP - 144p - MPEG-4 Visual/AAC 24kbit/s";
$este = "3gp";
break;
case "18":
$ret = "MP4 - 270p/360p - H.264/AAC 96kbit/s";
$este = "mp4";
break;
case "22":
$ret = "MP4 - 720p - H.264/AAC 192kbit/s";
$este = "mp4";
break;
case "34":
$ret = "FLV - 360p - H.264/AAC 128kbit/s";
$este = "flv";
break;
case "35":
$ret = "FLV - 480p - H.264/AAC 128kbit/s";
$este = "flv";
break;
case "36":
$ret = "3GP - 240p - MPEG-4 Visual/AAC 36kbit/s";
$este = "3gp";
break;
case "37":
$ret = "MP4 - 1080p - H.264/AAC 128kbit/s";
$este = "mp4";
break;
case "38":
$ret = "MP4 - 3072p - H.264/AAC 128kbit/s";
$este = "mp4";
break;
case "43":
$ret = "WebM - 360p - VP8/Vorbis 128kbit/s";
$este = "webm";
break;
case "44":
$ret = "WebM - 480p - VP8/Vorbis 128kbit/s";
$este = "webm";
break;
case "45":
$ret = "WebM - 720p - VP8/Vorbis 192kbit/s";
$este = "webm";
break;
case "46":
$ret = "WebM - 1080p - VP8/Vorbis 192kbit/s";
$este = "webm";
break;
case "82":
$ret = "MP4 - 360p - H.264/AAC 96kbit/s";
$este = "mp4";
break;
case "83":
$ret = "MP4 - 240p - H.264/AAC 96kbit/s";
$este = "mp4";
break;
case "84":
$ret = "MP4 - 720p - H.264/AAC 192kbit/s";
$este = "mp4";
break;
case "85":
$ret = "MP4 - 520p - H.264/AAC 192kbit/s";
$este = "mp4";
break;
case "100":
$ret = "WebM - 360p - VP8/Vorbis 128kbit/s";
$este = "webm";
break;
case "101":
$ret = "WebM - 360p - VP8/Vorbis 192kbit/s";
$este = "webm";
break;
case "102":
$ret = "WebM - 720p - VP8/Vorbis 192kbit/s";
$este = "webm";
break;
case "133":
$ret = "MP4 - 240p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "134":
$ret = "MP4 - 360p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "135":
$ret = "MP4 - 480p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "136":
$ret = "MP4 - 720p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "137":
$ret = "MP4 - 1080p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "138":
$ret = "MP4 - 2160p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "160":
$ret = "MP4 - 144p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "242":
$ret = "WebM - 240p - VP9";
$este = "webm";
$pvlc = true;
break;
case "243":
$ret = "WebM - 360p - VP9";
$este = "webm";
$pvlc = true;
break;
case "244":
$ret = "WebM - 480p - VP9";
$este = "webm";
$pvlc = true;
break;
case "247":
$ret = "WebM - 720p - VP9";
$este = "webm";
$pvlc = true;
break;
case "248":
$ret = "WebM - 1080p - VP9";
$este = "webm";
$pvlc = true;
break;
case "264":
$ret = "MP4 - 1440p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "271":
$ret = "WebM - 1440p - VP9";
$este = "webm";
$pvlc = true;
break;
case "272":
$ret = "WebM - 2160p - VP9";
$este = "webm";
$pvlc = true;
break;
case "278":
$ret = "WebM - 144p - VP9";
$este = "webm";
$pvlc = true;
break;
case "298":
$ret = "MP4 - 720p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "299":
$ret= "MP4 - 1080p - H.264";
$este = "mp4";
$pvlc = true;
break;
case "302":
$ret = "WebM - 720p - VP9";
$este = "webm";
$pvlc = true;
break;
case "303":
$ret = "WebM - 1080p - VP9";
$este = "webm";
$pvlc = true;
break;
case "139":
$ret = "MP4 - AAC 48kbit/s";
$este = "mp4";
$pvlc = true;
break;
case "140":
$ret = "MP4 - AAC 128kbit/s";
$este = "mp4";
$pvlc = true;
break;
case "141":
$ret = "MP4 - AAC 256kbit/s";
$este = "mp4";
$pvlc = true;
break;
case "171":
$ret = "WebM - Vorbis 128kbit/s";
$este = "webm";
$pvlc = true;
break;
case "172":
$ret = "WebM - Vorbis 192kbit/s";
$este = "webm";
$pvlc = true;
break;
case "92":
$ret = "TS - 240p - H.264/AAC 48kbit/s";
$este = "ts";
break;
case "93":
$ret = "TS - 360p - H.264/AAC 128kbit/s";
$este = "ts";
break;
case "94":
$ret = "TS - 480p - H.264/AAC 128kbit/s";
$este = "ts";
break;
case "95":
$ret = "TS - 720p - H.264/AAC 256kbit/s";
$este = "ts";
break;
case "96":
$ret = "TS - 1080p - H.264/AAC 256kbit/s";
$este = "ts";
break;
case "120":
$ret = "FLV - 720p - H.264/AAC 128kbit/s";
$este = "flv";
break;
case "127":
$ret = "TS - N/A - N/A AAC 96kbit/s";
$este = "ts";
break;
case "128":
$ret = "TS - N/A - N/A AAC 96kbit/s";
$este = "ts";
break;
case "132":
$ret = "TS - 240p - H.264/AAC 48kbit/s";
$este = "ts";
break;
case "151":
$ret = "TS - 72p - H.264/AAC 24kbit/s";
$este = "ts";
break;
default:
$ret = "Unknown quality";
$este = "unknown";
$pvlc = true;
break;
}
return array("codec" => $ret, "ext" => $este, "pvlc" => $pvlc);
}
function play_or_record($live, $url, $title, $info_quality, $vlc, $out){
while (!preg_match('@[pPrR]@', $scelta)){
echo "\r\nDo you want (p)lay or ®ecord? ";
$scelta = trim(fgets(STDIN));
}
if (preg_match('@[pP]@', $scelta)){
if ($live){
play_live($title, $url);
} else {
play($title, $url, $info_quality, $vlc, $out);
}
} elseif (preg_match('@[rR]@', $scelta)){
if ($live){
record_live($title, $url, $info_quality);
} else {
record($title, $url, $info_quality);
}
}
}
function play($title, $url, $info_quality, $vlc, $out){
echo "\r\nPlaying $title...\r\n";
if ($info_quality["pvlc"]){
$cmd = '"'.$vlc.'" --quiet --meta-title="'.$title.'" "'.$url.'" '.$out.'';
} else {
$cmd = 'livestreamer "httpstream://'.$url.'" best -l error';
}
shell_exec($cmd);
}
function record($title, $url, $info_quality){
echo "\r\nRecording $title...\r\n\r\n";
$cmd = 'wget -c -U "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/33.0" "'.$url.'" --no-check-certificate -O "'.$title.'.'.$info_quality["ext"].'"';
shell_exec($cmd);
}
function play_live($title, $url){
echo "\r\nPlaying $title...\r\n";
$cmd = 'livestreamer "hls://'.$url.'" best -l error';
shell_exec($cmd);
}
function record_live($title, $url, $info_quality){
echo "\r\nRecording $title...\r\n\r\n";
$cmd = 'livestreamer "hls://'.$url.'" best -o "'.$title.'.'.$info_quality["ext"].'"';
shell_exec($cmd);
}
function decode($cipher, $sig){
/*
Per il decoding delle signatures riporto il copyright del creatore di tale funzione originariamente scritta in PERL e che io ho portato in PHP:
Copyright © 2007-2015 Jamie Zawinski <
[email protected]>
Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. No representations are made about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.*/
$c = explode(" ", $cipher);
$s = str_split($sig);
foreach ($c as $step){
if ($step == ""){
} elseif ($step == "r"){
$s = array_reverse($s);
} elseif (preg_match("/^w(\d+)$/s", $step, $swap)){
$swap = $swap[1];
$temp = $s[0];
$s[0] = $s[$swap%count($s)];
$s[$swap] = $temp;
} elseif (preg_match("/^s(\d+)$/s", $step, $slice)){
$s = array_slice($s, $slice[1]);
}
}
return implode("", $s);
}
function grab_cipher($js){
$cc = new cURL;
preg_match('@-(.*?)/@si', $js, $cid);
echo "\r\nDownloading JS's page...($cid[1])\r\n";
$html = $cc->get($js);
unlink("cookies.txt");
echo "\r\nParsing JS's functions...\r\n";
$pat = '[\$a-zA-Z][a-zA-Z\d]*';
$pat = "$pat(?:\.$pat)?";
preg_match("/$pat = ( $pat ) \.sig \|\| ( $pat ) \( \\1 \.s \)/sx", $html, $fun);
if (empty($fun[2])){
die("\r\nUnparsable function!\r\n");
}
$fun = $fun[2];
preg_match("/\bfunction\s+\Q$fun\E\s*\($pat\)\s*{(.*?)}/sx", $html, $fun2);
$fun2 = preg_replace("/var\s($pat)=($pat)\[0\];\\2\[0\]=\\2\[(\d+)%\\2\.length\];\\2\[\\3\]=\\1;/", "$2=swap($2,$3);", $fun2[1]);
if (empty($fun2)) {
preg_match("/(?:\bvar\s+)?\Q$fun\E\s*=\s*function\s*\($pat\)\s*{(.*?)}/sx", $html, $fun);
$fun = preg_replace("/var\s($pat)=($pat)\[0\];\\2\[0\]=\\2\[(\d+)%\\2\.length\];\\2\[\\3\]=\\1;/", "$2=swap($2,$3);", $fun[1]);
} else {
$fun = $fun2;
}
if (empty($fun)){
die("\r\nUnparsable function!\r\n");
}
$pieces = explode(";", $fun);
$c = array();
foreach ($pieces as $piece){
$piece = trim($piece);
if (preg_match("/^($pat)=\\1\.$pat\(\"\"\)$/", $piece)){
} elseif (preg_match("/^($pat)=\\1\.$pat\(\)$/", $piece)){
$c[] = "r";
} elseif (preg_match("/^($pat)=\\1.$pat\((\d+)\)$/", $piece, $num)){
$c[] = "s".$num[2];
} elseif (preg_match("/^($pat)=($pat)\(\\1,(\d+)\)$/", $piece, $sw) || preg_match("@^()($pat)\($pat,(\d+)\)$@s", $piece, $sw)){
$n = $sw[3];
$f = preg_replace('@^.*\.@s', "", $sw[2]);
preg_match("@\b\Q$f\E:\s*function\s*\(.*?\)\s*({[^{}]+})@s", $html, $fn3);
if (preg_match("@var\s($pat)=($pat)\[0\];@s", $fn3[1])){
$c[] = "w$n";
} elseif (preg_match("@\b$pat\.reverse\(@s", $fn3[1])){
$c[] = "r";
} elseif (preg_match("@return\s*$pat\.slice@s", $fn3[1]) || preg_match("@\b$pat\.splice@s", $fn3[1])){
$c[] = "s$n";
}
} elseif (preg_match("@^return\s+$pat\.$pat\(\"\"\)$@s", $piece)){
}
}
$cipher = join(" ", $c);
echo "\r\nCipher is: $cipher\r\n";
return $cipher;
}
echo "\r\nYouTube by Darby_Crash\r\n\r\nDownloading video's page...\r\n";
if (!preg_match('@^https?://www\.youtube\.com/watch\?v=@i', $argv[1])){
die("\r\nInvalid url!\r\n");
}
preg_match('|^https?://www\.youtube\.com/watch\?v=(.*)$|i', $argv[1], $id);
$id = $id[1];
$cc = new cURL;
$html = $cc->get("https://www.youtube.com/watch?v=".$id);
if (empty($html)){
$html = $cc->get("http://www.youtube.com/watch?v=".$id);
if (empty($html)){
die("\r\nCan't get video information!!\r\n");
}
}
unlink("cookies.txt");
preg_match('@<meta property="og:restrictions:age" content="(.*?)">@', $html, $age_check);
if ($age_check[1] == "18+"){
die("\r\nThis video is not supported! (Age check)\r\n");
}
preg_match('/ytplayer.config = {(.*?)};/', $html, $data);
$info = json_decode('{'.$data[1].'}', true);
$title = str_replace(array('/',':','*','?','"','<','>','|'), '', $info["args"]["title"]);
$title = trim(stripslashes($title));
if (!$info["args"]["url_encoded_fmt_stream_map"]){
$hls_manifest = $info["args"]["hlsvp"];
if (empty($hls_manifest)){
die("\r\nCan't retrieve information!\r\n");
}
$live = true;
$manifest = $cc->get($hls_manifest);
unlink("cookies.txt");
preg_match_all('@http://.*@m', $manifest, $urls);
preg_match_all('@/itag/(.*?)/@', $manifest, $itags);
for ($i=0;$i<count($itags[1]);$i++){
$info_quality[] = choose_quality($itags[1][$i]);
}
while (!preg_match('@[0-9]@', $qua) || $qua >= count($itags[1])){
echo "\r\nQuality list:\r\n\r\n";
for ($i=0;$i<count($itags[1]);$i++){
echo $i.") ".$info_quality[$i]["codec"]."\r\n";
}
echo "\r\nChoose a quality: ";
$qua = trim(fgets(STDIN));
}
$url = $urls[0][$qua];
play_or_record($live, $url, $title, $info_quality[$qua], $vlc, $out);
echo "\r\nGoodbye...\r\n";
exit(0);
}
$formats = explode(',', $info["args"]["url_encoded_fmt_stream_map"]);
if ($info["args"]["adaptive_fmts"]){
$more_formats = explode(',', $info["args"]["adaptive_fmts"]);
for ($i=0;$i<count($more_formats);$i++){
$formats[] = $more_formats[$i];
}
}
for ($i=0;$i<count($formats);$i++){
parse_str($formats[$i], $string_map);
if ($string_map["s"]){
$sigs[] = $string_map["s"];
} elseif ($string_map["conn"]){
echo "\r\nRTMPE streams are not supported!\r\n";
exit(1);
}
$itags[] = $string_map["itag"];
$urls[] = $string_map["url"];
$hosts[] = $string_map["fallback_host"];
unset($string_map);
}
for ($i=0;$i<count($itags);$i++){
$info_quality[] = choose_quality($itags[$i]);
}
while (!preg_match('@[0-9]@', $qua) || $qua >= count($itags)){
echo "\r\nQuality list:\r\n\r\n";
for ($i=0;$i<count($itags);$i++){
echo $i.") ".$info_quality[$i]["codec"]."\r\n";
}
echo "\r\nChoose a quality: ";
$qua = trim(fgets(STDIN));
}
if ($sigs){
echo "\r\nFound encrypted signatures!\r\n";
$cipher = grab_cipher('https:'.$info["assets"]["js"]);
$sig = $sigs[$qua];
$url = $urls[$qua]."&signature=".decode($cipher, $sig);
} else {
$url = $urls[$qua];
}
play_or_record($live, $url, $title, $info_quality[$qua], $vlc, $out);
echo "\r\nGoodbye...\r\n";
exit(0);