Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP API To get Direct Link of YouTube videos
<?php
/*
Made by [egy.js](https://www.instagram.com/egy.js/);
v2.0
*/
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
function get_error($ErrorExaction){
$myObj = new stdClass();
$myObj->error = true;
$myObj->msg = $ErrorExaction;
$myObj->madeBy = "A.El-zahaby";
$myObj->instagram = "egy.js";
$myJSON = json_encode($myObj,JSON_PRETTY_PRINT);
echo $myJSON;
exit;
}
function get_log($dump){
if (isset($_GET['log'])) var_dump($dump).'\n\n\n';
}
if(isset($_GET['url']) && $_GET['url'] != ""){
parse_str( parse_url( $_GET['url'], PHP_URL_QUERY ), $vars );
$id=$vars['v'];
$dt=file_get_contents("https://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en");
if (strpos($dt, 'status=fail') !== false) {
$x=explode("&",$dt);
$t=array(); $g=array(); $h=array();
foreach($x as $r){
$c=explode("=",$r);
$n=$c[0]; $v=$c[1];
$y=urldecode($v);
$t[$n]=$v;
}
$x=explode("&",$dt);
foreach($x as $r){
$c=explode("=",$r);
$n=$c[0]; $v=$c[1];
$h[$n]=urldecode($v);
}
$g[]=$h;
$g[0]['error'] = true;
$g[0]['instagram'] = "egy.js";
$g[0]['apiMadeBy'] = 'El-zahaby';
echo json_encode($g,JSON_PRETTY_PRINT);
}else{
$x=explode("&",$dt);
$t=array(); $g=array(); $h=array();
foreach($x as $r){
$c=explode("=",$r);
$n=$c[0]; $v=$c[1];
$y=urldecode($v);
$t[$n]=$v;
}
$streams = explode(',',urldecode($t['url_encoded_fmt_stream_map']));
// if(empty($streams[0])){ get_error('ops! this video has something wrong! :( '); }
if(empty($streams[0])){
get_log($streams);
}
foreach($streams as $dt){
$x=explode("&",$dt);
foreach($x as $r){
$c=explode("=",$r);
if ($c[0] == 'itag'){ // reference: https://superuser.com/q/1386658
switch ($c[1]){
case '18':
$h['mimeType'] = "mp4";
$h['width'] = "640";
$h['height'] = "360";
$h['qualityLabel'] = '360p';
break;
case '22':
$h['mimeType'] = "mp4";
$h['width'] = "1280";
$h['height'] = "720";
$h['qualityLabel'] = '720p';
break;
case '43':
$h['mimeType'] = "webm";
$h['width'] = "640";
$h['height'] = "360";
$h['qualityLabel'] = '360p';
break;
default:
$h['mimeType'] = null;
$h['width'] = null;
$h['height'] = null;
$h['qualityLabel'] = '';
}
}
$n=$c[0]; /* => */ $v=$c[1];
$h[$n]=urldecode($v);
}
$g[]=$h;
}
echo json_encode($g,JSON_PRETTY_PRINT);
// var_dump( $g[1]["quality"],true);
}
}else{
get_error("Ops, there is no youtube link!");
}
@IamAKX

This comment has been minimized.

Copy link

IamAKX commented Nov 21, 2018

Hi, I tried using you API on youtube mobile link. http://youtu.be/30FTr6G53VU

I got response
[
{
"reason": "Invalid parameters.",
"status": "fail",
"errorcode": "2",
"error": true,
"instagram": "egy.js",
"apiMadeBy": "El-zahaby"
}
]

@JMV2112

This comment has been minimized.

Copy link

JMV2112 commented Mar 26, 2019

I created a Javascript variant for anyone not wanting to rely on calling an external API, https://gist.github.com/JMV2112/f131f6d7c7782eb6e54c72f4da735ec0

@SandyAra

This comment has been minimized.

Copy link

SandyAra commented Apr 12, 2019

Premium YouTube video not loading

@JMV2112

This comment has been minimized.

Copy link

JMV2112 commented Apr 19, 2019

I'm guessing that Youtube keeps changing code on their end to avoid people accessing the MP4 directly as this API isn't working again.

@mojohn888

This comment has been minimized.

Copy link

mojohn888 commented Apr 21, 2019

not working anymore??

@askdream

This comment has been minimized.

Copy link

askdream commented Apr 23, 2019

not working anymore??

@askdream

This comment has been minimized.

Copy link

askdream commented Apr 23, 2019

please !

@inthinks

This comment has been minimized.

Copy link

inthinks commented Apr 23, 2019

try to use this tutorial and still working. https://www.codexworld.com/download-youtube-video-using-php/

@JMV2112

This comment has been minimized.

Copy link

JMV2112 commented Apr 23, 2019

@inthinks the demo of that tutorial doesn't download the correct file, it gave my a download file of a couple hundred bytes in size.

@namcoder

This comment has been minimized.

Copy link

namcoder commented Jun 16, 2019

Your old code still work good ( the version used: "file_get_contents" )
Your old code:
$dt=file_get_contents("http://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en");

Just replace: http => https

Done

@mayeaux

This comment has been minimized.

Copy link

mayeaux commented Jun 17, 2019

Doesn't work, returns null

@kalimbig

This comment has been minimized.

Copy link

kalimbig commented Jun 23, 2019

hi anyone, can you tell me what am I going to do with this?

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jul 16, 2019

i will fix it guys , but we will return to the Previous version , with more improve 👍

thanks a lot

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jul 16, 2019

@DinhNC

This comment has been minimized.

Copy link

DinhNC commented Jul 24, 2019

Http or https all of the end by null? It's "Not working" :)

https://you-link.herokuapp.com/?url=https://www.youtube.com/watch?v=YGCLs9Bt_KY

@namcoder

This comment has been minimized.

Copy link

namcoder commented Jul 24, 2019

For all you guys

function getYoutubeDirectLinkMp4($id){

    parse_str(file_get_contents("https://www.youtube.com/get_video_info?video_id=$id"),$info);
    $streams = $info['adaptive_fmts']; // $info['url_encoded_fmt_stream_map'];
    $streams = explode(',',$streams);

    $listQuality = [];
    foreach($streams as $stream){
	    print_r($stream);
    }
}

Live demo: https://skytour.mtpentertainment.com . The video background is get from Youtube.

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jul 24, 2019

Guys .. Code is working well,,The issue is from HOST

Try the code on local

@DinhNC

This comment has been minimized.

Copy link

DinhNC commented Jul 24, 2019

function getYoutubeDirectLinkMp4($id){

parse_str(file_get_contents("https://www.youtube.com/get_video_info?video_id=$id"),$info);
$streams = $info['adaptive_fmts']; // $info['url_encoded_fmt_stream_map'];
$streams = explode(',',$streams);

$listQuality = [];
foreach($streams as $stream){
	print_r($stream)
}

}

Vip guys =)). Supperman appeared!

@DinhNC

This comment has been minimized.

Copy link

DinhNC commented Jul 24, 2019

Guys .. Code is working well,,The issue is from HOST

Try the code on local

Thank u!

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jul 24, 2019

Guys .. Code is working well,,The issue is from HOST

Try the code on local

If any one can offer a very small part (like 0.5giga) of his host we can upload the code on it and do more upgrading 👍😊
Because i don't find a good free host

@DinhNC

This comment has been minimized.

Copy link

DinhNC commented Jul 24, 2019

Why do you think about Google Firebase hosting? 😄

https://firebase.google.com/

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jul 24, 2019

Why do you think about Google Firebase hosting?

https://firebase.google.com/

https://stackoverflow.com/a/35524033 [html,css,js]

Firebase Hosting is for hosting static assets. Firebase currently doesn't offer any way to execute your code on Firebase's servers.

@DinhNC

This comment has been minimized.

Copy link

DinhNC commented Jul 25, 2019

Why do you think about Google Firebase hosting?
https://firebase.google.com/

https://stackoverflow.com/a/35524033 [html,css,js]

Firebase Hosting is for hosting static assets. Firebase currently doesn't offer any way to execute your code on Firebase's servers.

Oh, really 😢 . I'm using nodejs for this. Maybe, PHP don't support for this hosting.

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jul 25, 2019

Oh, really . I'm using nodejs for this. Maybe, PHP don't support for this hosting.

Yeah.. i will try to find another host

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jul 26, 2019

@DinhNC , @mojohn888 , @mayeaux
I changed the host .. please try it and tell me if anything goes wrong

@vishnupsatish

This comment has been minimized.

Copy link

vishnupsatish commented Aug 23, 2019

Hi. This doesn't work with JavaScript. It gives the following response:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("bbf486f74d02fd069ca9536beda40f84");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://youlink.epizy.com/?url=https://www.youtube.com/watch?v=7iBqEknWOiU&i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

The links when I go to the actual website work, but why is it giving this response, and how can I fix it?

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Aug 23, 2019

please

Hi. This doesn't work with JavaScript. It gives the following response:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("bbf486f74d02fd069ca9536beda40f84");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://youlink.epizy.com/?url=https://www.youtube.com/watch?v=7iBqEknWOiU&i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

The links when I go to the actual website work, but why is it giving this response, and how can I fix it?

please some body provide 1 mega from his host .. thanx

@vishnupsatish

This comment has been minimized.

Copy link

vishnupsatish commented Aug 23, 2019

What does that mean? Please explain in detail and also please tell me how I can fix it or what you intend to do/how I can help to fix it. Please explain the problem first though...

@TIRTAGT

This comment has been minimized.

Copy link

TIRTAGT commented Sep 27, 2019

I Got The youtube_signature_decipher.php from my friend, it return yhe videolink perfectly, but when i open the link

I got FORBIDDEN message from youtube.... I guess its because of my server ip and my computer ip was different..... (Youtube will only give access to the ip who requested the access)

@TIRTAGT

This comment has been minimized.

Copy link

TIRTAGT commented Sep 27, 2019

Can we solve this problem and share the code together?

I got the script that will output the [long youtube url] (work fine on localhost but not on hostings) , did anyone got how to bypass the forbidden problem? Or maybe something that can fix the forbidden message

@petrk94

This comment has been minimized.

Copy link

petrk94 commented Oct 23, 2019

The script is working, but Im facing a weird issue.
When I get the results and click on the URL, it immediately redirect me to another googlevideo URL. Not sure what this cause, but while the showed URL result has an IP in Germany, the redirected URL seems to be in USA. Thats very weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.