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?

@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)

@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.

@ngtuan96lc

This comment has been minimized.

Copy link

ngtuan96lc commented Nov 24, 2019

Hi everyone.
I have an issue when get information of video.
With some videos, i received: "The uploader does not make this video available in your country."


How to resolve it ? Thanks everyone!

@Stephane961

This comment has been minimized.

Copy link

Stephane961 commented Jan 1, 2020

Fantastic tool!
I'm trying to load it using a developer tool that cannot run javascript..
Unfortunately I get errors..
How can I get the JSON output already being generated so I can GATHER it in a non javascript tool?

thank you!

Stephane

(I'm using http://youlink.epizy.com/?url=https://www.youtube.com/watch?v=[ID])

@quyleanh

This comment has been minimized.

Copy link

quyleanh commented Jan 9, 2020

It's seems that the script is broken. @el3zahaby

@inthinks

This comment has been minimized.

Copy link

inthinks commented Jan 15, 2020

i can not download anymore from youtube since today, there is an error "Undefined index: url_encoded_fmt_stream_map"

@GavHern

This comment has been minimized.

Copy link

GavHern commented Jan 18, 2020

i can not download anymore from youtube since today, there is an error "Undefined index: url_encoded_fmt_stream_map"

I think the way this goes is youtube keeps patching this api from working and then OP fixes it... Not a really reliable system but not much you can do when you're using an API that breaks terms of service...

@lhuxman-lhux

This comment has been minimized.

Copy link

lhuxman-lhux commented Jan 28, 2020

parse_str(file_get_contents("https://www.youtube.com/get_video_info?video_id=N-1IiYkrvgE&asv=3&el=detailpage&hl=en_US"),$info);
$streams = $info['player_response'];
$jsn_str=str_replace("\u0026","&",$streams);
$streamin_data_json=json_decode($jsn_str, true);
foreach ($streamin_data_json["streamingData"]["adaptiveFormats"] as $stream) {

parse_str($stream["cipher"],$dturl);
echo $dturl['url'].'
';
}

@GavHern

This comment has been minimized.

Copy link

GavHern commented Jan 28, 2020

parse_str(file_get_contents("https://www.youtube.com/get_video_info?video_id=N-1IiYkrvgE&asv=3&el=detailpage&hl=en_US"),$info,$info));
$streams = $info['player_response'];
$jsn_str=str_replace("\u0026","&",$streams);
$streamin_data_json=json_decode($jsn_str, true);
foreach ($streamin_data_json["streamingData"]["adaptiveFormats"] as $stream) {

parse_str($stream["cipher"],$dturl);
echo $dturl['url'].'
';
}

The link sends me here...
image

I think there's some param you need that can be found in the youtube video player. (Here's someone who completly reverse engineered this api (if you'd even call it that).)[https://tyrrrz.me/blog/reverse-engineering-youtube]. I think this is missing the sts param, as it is called in his blog.

@lhuxman-lhux

This comment has been minimized.

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jan 28, 2020

@lhuxman-lhux

my code

https://gist.github.com/lhuxman-lhux/bade6474d6b4616d29594541a9e11888

this is really good work ,, i will fork it and improve it if you don't mind

@lhuxman-lhux

This comment has been minimized.

Copy link

lhuxman-lhux commented Jan 28, 2020

oke, no problem

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jan 28, 2020

no audio i any video @lhuxman-lhux

@lhuxman-lhux

This comment has been minimized.

Copy link

lhuxman-lhux commented Jan 28, 2020

first url,

@el3zahaby

This comment has been minimized.

Copy link
Owner Author

el3zahaby commented Jan 28, 2020

first url,

i did not understand this

@GavHern

This comment has been minimized.

Copy link

GavHern commented Feb 2, 2020

first url,

i did not understand this

I'm playing around with lhuxman-lhux's script and starting to figure it out... Not every itag is working in his code (he says it's still in development). The ones that are working for me are 299, 251, 313, 137, 248. You can see what those itags correspond to within his switch statement at the top of the script. There are more of them that work, but the ones I listed are for 1080p60, 1080p, 360p, and 4k.

Also, @lhuxman-lhux do you mind if I also fork this? This is really well made.

@lhuxman-lhux

This comment has been minimized.

Copy link

lhuxman-lhux commented Feb 7, 2020

first url,

i did not understand this

I'm playing around with lhuxman-lhux's script and starting to figure it out... Not every itag is working in his code (he says it's still in development). The ones that are working for me are 299, 251, 313, 137, 248. You can see what those itags correspond to within his switch statement at the top of the script. There are more of them that work, but the ones I listed are for 1080p60, 1080p, 360p, and 4k.

Also, @lhuxman-lhux do you mind if I also fork this? This is really well made.

please contact me:
lhuxman19@gmail.com

@Maadhav

This comment has been minimized.

Copy link

Maadhav commented May 15, 2020

@GavHern

This comment has been minimized.

Copy link

GavHern commented May 15, 2020

image
Why I am getting no response.
Entered URL: http://youlink.epizy.com/?url=https://www.youtube.com/watch?v=QIKv7j0bTyM

Yeah the API doesn't seem to be working anymore... There arent any good alternatives I could find either...

@Maadhav

This comment has been minimized.

Copy link

Maadhav commented May 16, 2020

So after all the research, I have done, I found a better solution and implemented and hosted it on Heroku...
Link: https://maadhav-ytdl.herokuapp.com/
Please let me know if any more features are needed. Thanks.
I hope it helps all others...

@GavHern

This comment has been minimized.

Copy link

GavHern commented May 16, 2020

So after all the research, I have done, I found a better solution and implemented and hosted it on Heroku...
Link: https://maadhav-ytdl.herokuapp.com/
Please let me know if any more features are needed. Thanks.
I hope it helps all others...

This is really good, great work! Is it, by chance, open source? I may be able to add if the video has audio or not. I'd have to look at some old code of mine and see...

Edit: nevermind, seems like you got that already implemented. Might I suggest the "format" be an array just to make things easier?

@Maadhav

This comment has been minimized.

Copy link

Maadhav commented May 16, 2020

Thanks! I have given the source code just below the title.
Anyway: https://github.com/Maadhav/youtube-downloader-API

@GavHern

This comment has been minimized.

Copy link

GavHern commented May 16, 2020

Thanks! I have given the source code just below the title.
Anyway: https://github.com/Maadhav/youtube-downloader

Cool man! Big ups for making this.

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.