YouTube video downloader PHP Script Free

YouTube video downloader PHP Script

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 will look for a PHP script to download YouTube videos on your local server. So here in this post, I will explain how easily you can create your own script to download your favourite YouTube videos. At the end of this post, you will also see live demo of Youtube video downloader script and also link to download live demo script.

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;
}
}

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

GitHub Url https://github.com/jeckman/YouTube-Downloader

About The Author

Related Posts

Add Comment