Create a YouTube video downloader in PHP

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

$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("".$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
        echo 'Download finished! Check the file.';

The script is finished. Now you can call it like this after you’ve put it to the server:


It can be turned into a streaming script that streams YouTube videos to the client also. Happy downloading YouTube videos!

Do `Harlem Shake` on any websites!

These days you can see many dancing videos on YouTube and those dances are called Harlem Shake. I wonder if there’s others things that can dance the Harlem Shake.

Based on the idea of Vir Ascabano, He has created a little script that can do the Harlem Shake on websites! However, it’s not very perfect, so I create another one which is better in some┬ácircumstances.

Here’s the script:

(function(c){var b=c.getElementsByTagName("head")[0];a=c.createElement("script");a.type="text/javascript";a.src=unescape("");a.async=true;b.appendChild(a);})(document);

It’s been reported that this doesn’t work on Facebook. It’s because Facebook limits what scripts can be loaded by using the Content Security Policy headers. To make it work on Facebook, we need to use the long script below instead of the one above.

(function(){function l(a){var c=a.offsetHeight;a=a.offsetWidth;return c>q&&c<r&&a>s&&a<t}function m(){for(var a=document.getElementsByClassName(f),c=RegExp("\\b"+f+"\\b");0<a.length;)a[0].className=a[0].className.replace(c,"")}var q=10,s=10,r=1E3,t=1E3,f="mw-harlem_shake_me",n=["im_drunk","im_baked","im_trippin","im_blown","im_hanging"],p,b=document.documentElement;p=window.innerWidth?window.innerHeight:b&&!isNaN(b.clientHeight)?b.clientHeight:0;var g;g=window.pageYOffset?window.pageYOffset:Math.max(document.documentElement.scrollTop,document.body.scrollTop);for(var b=document.getElementsByTagName("*"),h=null,e=0;e<b.length;e++){var a=b[e];if(l(a)){var d;d=a;for(var j=0;d;)j+=d.offsetTop,d=d.offsetParent;d=j;if(d>=g&&d<=p+g){h=a;break}}}if(null===a)a=b;else{a=document.createElement("link");a.setAttribute("type","text/css");a.setAttribute("rel","stylesheet");a.setAttribute("href","//");a.setAttribute("class","mw_added_css");document.body.appendChild(a);a=document.createElement("audio");a.setAttribute("class","mw_added_css");a.src="//";a.loop=!1;a.addEventListener("canplay",function(){setTimeout(function(){h.className+=" "+f+" im_first"},500);setTimeout(function(){m();var a=document.createElement("div");a.setAttribute("class","mw-strobe_light");document.body.appendChild(a);setTimeout(function(){document.body.removeChild(a)},100);for(var c=0;c<k.length;c++){var b=k[c];b.className+=" "+f+" "+n[Math.floor(Math.random()*n.length)]}},15500)},!0);a.addEventListener("ended",function(){m();for(var a=document.getElementsByClassName("mw_added_css"),b=0;b<a.length;b++)document.body.removeChild(a[b])},!0);a.innerHTML=" <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";document.body.appendChild(a);;for(var k=[],e=0;e<b.length;e++)a=b[e],l(a)&&k.push(a)}})();

How to use?

  1. First, press F12 -> Console
  2. Then, copy-and-paste the code above and click enter.
  3. You should see the effect afterwards.

This effect works in Chrome, Firefox, IE10+, Safari and Opera.