YouTube is the most popular videos sharing platform where we watched videos online. However, we often need to get those YouTube videos for offline uses. If you’re a PHP developer then definitely you're looking for a PHP script to download YouTube videos on your local server. So here in this post, we will explain how easily you can create your own PHP script to download your favorite YouTube videos.
At the end of this post, you will also see live demo of YouTube video download script and also download live demo source code.

As we know that YouTube didn’t provide any ways to get the raw video but still we can download it. As the player will always issue a HTTP request to http://youtube.com/get_video_id?video_id=THE_VIDEO_ID to get information about a specific video. The result of the request contains a URL-encoded string that has the video’s location. So, we will need to get that part first of video.
Now we will get all the streams. As the stream is also different sets of URL-encoded data separated by a comma. In order to retrieve all the streams, we need to turn it into an array by using explode(',',$streams).
Finally, we will need to loop through all the streams and get its data.
The complete PHP script to download YouTube videos:
Now you can call it like this after you’ve put it to the server in a PHP file:
Here we have developed a complete Youtube video downloader script with the help of above. You can see this script in action from below live demo link.
Demo [sociallocker]Download[/sociallocker]
At the end of this post, you will also see live demo of YouTube video download script and also download live demo source code.

As we know that YouTube didn’t provide any ways to get the raw video but still we can download it. As the player will always issue a HTTP request to http://youtube.com/get_video_id?video_id=THE_VIDEO_ID to get information about a specific video. The result of the request contains a URL-encoded string that has the video’s location. So, we will need to get that part first of video.
$vid = $_GET['vid']; // Youtube video ID
$vformat = $_GET['vformat']; // The MIME type of the video. e.g. video/mp4, video/webm, etc.
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=".$vid),$info);
$streams = $info['url_encoded_fmt_stream_map'];
Now we will get all the streams. As the stream is also different sets of URL-encoded data separated by a comma. In order to retrieve all the streams, we need to turn it into an array by using explode(',',$streams).
$streams = explode(',',$streams);
Finally, we will need to loop through all the streams and get its data.
foreach($streams as $stream){
parse_str($stream,$data); //Now decode the stream
if(stripos($data['type'],$vformat) !== false){
$video = fopen($data['url'].'&signature='.$data['sig'],'r'); //the video
$file = fopen('video.'.str_replace($vformat,'video/',''),'w');
stream_copy_to_stream($video,$file);
fclose($video);
fclose($file);
echo 'Youtube Video Download finished! Now check downloaded file.';
break;
}
}
The complete PHP script to download YouTube videos:
<?php
$vid = $_GET['vid']; //the youtube video ID
$vformat = $_GET['vformat']; //the MIME type of the video. e.g. video/mp4, video/webm, etc.
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=".$vid),$info); //decode the data
$streams = $info['url_encoded_fmt_stream_map']; //the video's location info
$streams = explode(',',$streams);
foreach($streams as $stream){
parse_str($stream,$data); //decode the stream
if(stripos($data['type'],$vformat) !== false){ //We've found the right stream with the correct format
$video = fopen($data['url'].'&signature='.$data['sig'],'r'); //the video
$file = fopen('video.'.str_replace($vformat,'video/',''),'w');
stream_copy_to_stream($video,$file); //copy it to the file
fclose($video);
fclose($file);
echo 'Youtube Video Download finished! Now check the file.';
break;
}
}
?>
Now you can call it like this after you’ve put it to the server in a PHP file:
http://localhost/PHP SCRIPT NAME.php?vid=THE YOUTUBE VIDEO ID&vformat=THE MIME TYPE OF THE VIDEO
Here we have developed a complete Youtube video downloader script with the help of above. You can see this script in action from below live demo link.
Demo [sociallocker]Download[/sociallocker]
Hi there, nice script but it only download a very low video quality, what about 720p?
ردحذفThanks
nice script bro...
ردحذفbut can you plz tell how to download complete playlist
Now We have updated script with live demo and script source to download and try it. I will solve your problem.
ردحذفThanks, I have updated script. now try it.
ردحذفi get an error that the client does not have permission to get URL
ردحذفwhat does it mean
How to download full video in playlist or channels.. it script php
ردحذفThanks for comment, The script only download single videos, I will update script in future to download full list or channels.
ردحذفThanks for query, I think the error you facing due to some other issue not related YouTube video download.
ردحذفhello, nice tutorial! but may i know how can i download the download code? like the live demo? thank you.
ردحذفThis code doesn't work with copyrighted videos.
ردحذفYou can download from given download link.
ردحذفYes its not allowed copyright videos to download
ردحذفnice
ردحذفThe code works fine but the only problem am getting is it just downloads the video to the server not client machine
ردحذفits working fine in locally But in Live Website that code doesn't work (It is restricted from playback on certain sites.)
ردحذفthank you very much, nice
ردحذفThank you for nice share.
ردحذفwhere is the download option..
ردحذفDownload link is next to live demo and its locked, you just need to social share to unlock download link.
ردحذفthank bro you doing very great job script is 1000000000% working
ردحذفplease make a php live chat and live video chat script with online show option thanks for giving a php youtube download script
Thanks bro!, I will try to publish tutorial on these topics.
ردحذفBlown away with your script. Simple, short, effective, no OAuth, no python.
ردحذفThank you very much.
Hello together,
ردحذفsince today theres a new cipher ... ill try to get it ... if now maybe someone of you could help - would be nice!
greetings
Thanks for the script Working perfect & fast.
ردحذفThe functionality working fine in demo. Send more details, so that I can help you. Thanks!
ردحذفhow i get your script brother
ردحذفOnly low videos download
ردحذفYou can download script through download link next to live demo link. Thanks!
ردحذفإرسال تعليق