Skip to content

Instantly share code, notes, and snippets.

@lhuxman-lhux
Last active June 15, 2023 19:06
Show Gist options
  • Save lhuxman-lhux/bade6474d6b4616d29594541a9e11888 to your computer and use it in GitHub Desktop.
Save lhuxman-lhux/bade6474d6b4616d29594541a9e11888 to your computer and use it in GitHub Desktop.
youtube download
<?php
if (file_exists('sig1.php')) {
unlink('sig1.php');
}
function getc($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function cut_yt($ganti){
$arayganti=array('Youtube','youtube','YouTube');
$ganti=str_replace($arayganti,'',$ganti);
return $ganti;
}
function quality($itag) {
switch ($itag) {
case "17":
return "144P";
break;
case "278":
return "144P";
break;
case "36":
return "240P";
break;
case "242":
return "240P";
break;
case "18":
return "360P";
break;
case "243":
return "360P";
break;
case "43":
return "360P";
break;
case "35":
return "480P";
break;
case "44":
return "480P";
break;
case "135":
return "480P";
break;
case "244":
return "480P";
break;
case "22":
return "720P";
break;
case "136":
return "720P";
break;
case "247":
return "720P";
break;
case "137":
return "1080P";
break;
case "248":
return "1080P";
break;
case "299":
return "1080P (60 FPS)";
break;
case "138":
return "2K";
break;
case "264":
return "2K";
break;
case "271":
return "2K";
break;
case "266":
return "4K";
break;
case "313":
return "4K (60 FPS)";
break;
case "139":
return " 48 Kbps";
break;
case "140":
return "128 Kbps";
break;
case "141":
return " 128 Kbps";
break;
case "171":
return " 128 Kbps";
break;
case "249":
return " 50k";
break;
case "250":
return " 70k";
break;
case "251":
return " 160k";
break;
default:
return $itag;
break;
}
}
if (isset($_GET['id'])) {
$id=$_GET['id'];
}else{
die('hhhh');
}
$a= getc('https://www.youtube.com/embed/'.$id);
$ccc=explode('s/player/', $a);
$ddd=explode('/www-player.css', $ccc[1]);
$jdl=explode('<title>', $a);
$jdl=explode('</title>', $jdl[1]);
$judul=cut_yt($jdl[0]);
$gsts= file_get_contents('https://www.youtube.com/s/player/'.$ddd[0].'/player_ias.vflset/en_US/base.js');
function getchiper($decipherScript){
$decipherPatterns = explode('.split("")', $decipherScript);
unset($decipherPatterns[0]);
foreach ($decipherPatterns as $value) {
$value = explode('.join("")', explode('}', $value)[0]);
if (count($value) === 2) {
$value = explode(';', $value[0]);
array_pop($value);
unset($value[0]);
$decipherPatterns = implode(';', $value);
break;
}
}
preg_match_all('/(?<=;).*?(?=\[|\.)/', $decipherPatterns, $deciphers);
if ($deciphers && count($deciphers[0]) >= 2) {
$deciphers = $deciphers[0][0];
$deciphersObjectVar = $decipherPatterns ;
$decipher = explode($deciphers . '={', $decipherScript)[1];
$decipher = str_replace(["\n", "\r"], '', $decipher);
$decipher = explode('}};', $decipher)[0];
$decipher = explode('},', $decipher);
// Convert deciphers to object
$deciphers = [];
foreach ($decipher as &$function) {
$deciphers[explode(':function', $function)[0]] = explode('){', $function)[1];
}
// Convert pattern to array
$decipherPatterns = str_replace($deciphersObjectVar . '.', '', $decipherPatterns);
$decipherPatterns = str_replace($deciphersObjectVar . '[', '', $decipherPatterns);
$decipherPatterns = str_replace(['](a,', '(a,'], '->(', $decipherPatterns);
$decipherPatterns = explode(';', $decipherPatterns);
$patterns =$decipherPatterns;
$deciphers =$deciphers;
for ($i=0; $i < count($patterns); $i++) {
$executes = explode('->', $patterns[$i]);
$execute=explode('.', $executes[0]);
$number = intval(str_replace(['(', ')'], '', $executes[1]));
$execute = $deciphers[$execute[1]];
switch ($execute) {
case 'a.reverse()':
$processSignature = '$reverse';
break;
case 'var c=a[0];a[0]=a[b%a.length];a[b]=c':
$processSignature= '$length';
break;
case 'var c=a[0];a[0]=a[b%a.length];a[b%a.length]=c':
$processSignature= '$lengtha';
break;
case 'a.splice(0,b)':
$processSignature= '$splice';
break;
default:
die("\n==== Decipher dictionary was not found ====");
break;
}
$myfile = fopen("sig1.php", "a+") or die("Unable to open file!");
if ($i==0) {
fwrite($myfile, '<?php $a = str_split($s);');
}
fwrite($myfile, $processSignature.'($a,'.$number.');');
fclose($myfile);
}
}
}
function sig($s){
$reverse=function(&$a){
$a = array_reverse($a);
};
$splice=function(&$a, $b){
$a = array_slice($a, $b);
};
$length = function(&$a, $b){
$c = $a[0];
$a[0] = $a[$b % count($a)];
$a[$b] = $c;
};
$lengtha = function(&$a, $b){
$c = $a[0];
$a[0] = $a[$b%count($a)];
$a[$b%count($a)] = $c;
};
include('sig1.php');
return join('',$a);
}
getchiper($gsts);
$data = getc("https://www.youtube.com/get_video_info?video_id=".$id."&asv=3&el=detailpage&hl=en_US");
parse_str($data,$info);
$streams = $info['player_response'];
$jsn_str=str_replace("\u0026","&",$streams);
$streamin_data_json=json_decode($jsn_str, true);
$str=$streamin_data_json["streamingData"]["formats"];
foreach ($str as $stream) {
if (isset($stream["signatureCipher"])) {
parse_str($stream["signatureCipher"],$dturl);
$json['default']=$dturl['url'].'&sig='.sig($dturl['s']);
}else{
if (!empty($str['url'])) {
$json['default']=$str['url'];
}
}
}
$aud = array();
$vid = array();
foreach ($streamin_data_json["streamingData"]["adaptiveFormats"] as $stream) {
if (isset($stream["signatureCipher"])) {
parse_str($stream["signatureCipher"],$dturl);
$stream['url']=$dturl['url'].'&sig='.sig($dturl['s']);
}
if (preg_match('/audio/', $stream['mimeType'])) {
$url = $stream['url'];
$values = array(
'quality' => quality($stream['itag']),
'url' => $url,
);
array_push($aud, $values);
}
if (preg_match('/video/', $stream['mimeType'])) {
$url = $stream['url'];
$values = array(
'quality' => quality($stream['itag']),
'url' => $url,
);
array_push($vid, $values);
}
};
$json['audio']=$aud;
$json['video']=$vid;
$json['title']=$judul;
echo json_encode($json, JSON_FORCE_OBJECT);
@lhuxman-lhux
Copy link
Author

lhuxman-lhux commented Jan 28, 2020

yt.php?id=your-id

@sb69
Copy link

sb69 commented Jan 28, 2020

I would do like this the first 3 videos with sound
$stream=$streamin_data_json["streamingData"]["formats"][0];
if (isset($stream["cipher"])) {
parse_str($stream["cipher"],$dturl);
echo 'Download '.quality($stream["itag"]).'
';
}else{
echo 'Download '.quality($stream["itag"]).'
';
}

Replace with
foreach ($streamin_data_json["streamingData"]["formats"] as $stream) {
if (isset($stream["cipher"])) {
parse_str($stream["cipher"],$dturl);
print_r($dturl);
echo 'Download '.quality($stream["itag"]).'
';
}else{
echo 'Download '.quality($stream["itag"]).'
';
}

@egyjs
Copy link

egyjs commented Jan 28, 2020

please contact me:
[email protected]

@egyjs
Copy link

egyjs commented Jan 28, 2020

no audio i any video

@lhuxman-lhux
Copy link
Author

still under development

@JuliusRockeiro
Copy link

ty works nice
but why i got very slow download speeed?

@daveamato
Copy link

Awesome script!

@programirati
Copy link

programirati commented Feb 22, 2020

Hi
two days ago code warked perfect, but now do not working at all.
Can you check it and fix it?

@daveamato
Copy link

It's still working on php7. You should try checking your dpkg log on the server for any updates that may have changed your configuration.

@programirati
Copy link

It's still working on php7. You should try checking your dpkg log on the server for any updates that may have changed your configuration.

I have this error on PHP 5.6

PHP Warning: file_get_contents(https://www.youtube.com/get_video_info?video_id=f9COOUUkkw8&amp;asv=3&amp;el=detailpage&amp;hl=en_US): failed to open stream: HTTP request failed! HTTP/1.0 429 Too Many Requests
in /home/my_site/public_html/yt.php on line 209

@lhuxman-lhux
Copy link
Author

function curl($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);

    return $data;

}

change file_get_contents() to curl()

@programirati
Copy link

Thanks working now.
can you check this link, it is not posible to download.
https://www.youtube.com/watch?v=OrKPqj6AVb4

@daveamato
Copy link

Nice fix for php5!

@Sice55
Copy link

Sice55 commented Apr 17, 2020

Tell me please, why do you get videos in 720p quality without sound?
thanks.

@daveamato
Copy link

Not working for me

@inshadbajwa333
Copy link

I can download video but without audio.can you help me to solve this.

@daveamato
Copy link

Interesting... cause I can no longer view any videos. Can you post your code?

@inshadbajwa333
Copy link

function curl($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);

    return $data;

}

change file_get_contents() to curl()

Warning: file_get_contents(https://www.youtube.com/yts/jsbin/player_ias/en_US/base.js): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in C:\xampp\htdocs\youtube.php on line 207

showing this error

@lhuxman-lhux
Copy link
Author

lhuxman-lhux commented Sep 13, 2020

function curl($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);

    return $data;

}
change file_get_contents() to curl()

Warning: file_get_contents(https://www.youtube.com/yts/jsbin/player_ias/en_US/base.js): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in C:\xampp\htdocs\youtube.php on line 207

showing this error

I have fixed it,

yt.php?id=your-id

@arankaren
Copy link

arankaren commented Feb 23, 2021

Error decrypting url signatures: Decipher dictionary was not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment