YouTube is the most popular video sharing platform and videos is watched online. However, sometimes we have a need to get the video for offline uses or you don’t want the watermark of YouTube on the video or even you want to use YouTube as a free video host and YouTube didn’t provide any ways to get the raw video. So in this post, I’m teach you how to do it in PHP (so you can embed the video easily in your website).
By reverse engineering the YouTube embed player, we know that the player will 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 need to get that part first.
<?php $id = $_GET['id']; //the youtube video ID $format = $_GET['fmt']; //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=".$id),$info); //decode the data $streams = $info['url_encoded_fmt_stream_map']; //the video's location info
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);
After that, we need to loop through all the streams. Now it goes to the main part: select an appropriate stream and get its data.
foreach($streams as $stream){ parse_str($stream,$data); //decode the stream if(stripos($data['type'],$format) !== 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($format,'video/',''),'w'); stream_copy_to_stream($video,$file); //copy it to the file fclose($video); fclose($file); echo 'Download finished! Check the file.'; break; } }
The script is finished. Now you can call it like this after you’ve put it to the server:
http://localhost/THE SCRIPT NAME.php?id=THE YOUTUBE VIDEO ID&fmt=THE MIME TYPE OF THE VIDEO
It can be turned into a streaming script that streams YouTube videos to the client also. Happy downloading YouTube videos!
i am having error online 5 and 9 why,
Can you please help me to create a Facebook video downloader in PHP or any other language.
There is clearly a lot to identify about this. I think you made various nice points in features also.
Hello friends. I have created the same tool and using it in my official websites.
http://www.webnius.com/tools/youtube-video-downloader-direct-download-any-video/
My script is different but works like a charm and separate audio and video formats of video are shown with my tool. please check once.
I made an example follow your guide. I got 403 responsed at line
`$video = fopen($data[‘url’].’&signature=’.$data[‘s’], ‘r’);`
I replace $data[‘sig’] with $data[‘s’] when I don’t find the attribute.
I think Youtube has disabled this function and make another.
Or am I missing any?
hey am Interested in this see i have a website that I want visitors to be able to download video directly from the site by adding php upload URL or create YouTube Downloader please help
how to video download using youtube video url
some video size 0k display in
Hi,
Can you please help, its not working . Getting error as
Notice: Undefined index: sig in C:\xampp\htdocs\youtube.php on line 10
Warning: fopen(http://r1—sn-hpjgvnj5o-cvhe.googlevideo.com/videoplayback?expire=1473021577&upn=Rxo5nXRyx_g&lmt=1469698296478872&key=yt6&sparams=dur%2Cgcr%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Csource%2Cupn%2Cexpire&sver=3&ratebypass=yes&gcr=in&mt=1472999304&ipbits=0&source=youtube&mv=m&mime=video%2Fmp4&ms=au&dur=173.499&initcwndbps=1737500&id=o-AMZScTAI82JOdERk3WSIvYS7Vo0NuutN-6mWN61hAk0P&pl=24&itag=22&mm=31&ip=111.125.233.229&mn=sn-hpjgvnj5o-cvhe&signature=): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in C:\xampp\htdocs\youtube.php on line 10
Warning: stream_copy_to_stream() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\youtube.php on line 12
Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\youtube.php on line 13
Download finished! Check the file.
**********Script ************************************
<?php
$id = $_GET['id']; //the youtube video ID
$format = $_GET['fmt']; //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=".$id),$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'],$format) !== 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($format,'video/',''),'w');
stream_copy_to_stream($video,$file); //copy it to the file
fclose($video);
fclose($file);
echo 'Download finished! Check the file.';
break;
}
}
*********************************************************
I need help please
the script copies the file to the server instead of the client machine.
Please I want to implement this script on my website http://www.ghdownloads.com but when I try it on localhost, I get this error:
Notice: Undefined index: 144p-3gp in C:\xampp\htdocs\dy\index.php on line 7
tahnk your article 🙂
how to get id and video format from youtube url.I actually want to take the youtube url form user(from a textbox).So how to get the file format from it.I’ve googled and found the way to get id of a youtube url.
obviously like your website however you have to check the spelling on quite a few of
your posts. A number of them are rife with spelling problems and I to find
it very troublesome to inform the reality on the
other hand I will definitely come again again.