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!

56 Replies to “Create a YouTube video downloader in PHP”

      1. hi everson vansoski so many posible reason
        1.wmg video or like vevo video use cipher signature that change every day so your script must decipher this cipher first.
        2. this content is not available on your country (youtube check your ip to request to this video).

    1. hey am trying to create a downloader like yours but the problem i get is videos are being downloaded to the server instead of client machine

    2. Hi Suyash Kumar, great creation of the website. I want to create like this but I dont have knowledge in coding. Can you please send me the code so I can embed to my wordpress site. Or is it okay to just iFrame your website? Thats way more easier. Please let me know your opinion.

      Thank you

    3. Suyash(Nitin Kumar) your work is awesome.I have a simple technique that can benefit you much more.
      I watched your alexa ranking (34 lakhs 🙁 ) which is to slow to rank high
      Good Luck.

  1. Hi,
    It’s Work! Thanks!
    but, how can I get a list of new uploaded videos in YouTube? or search in YouTube videos!
    Are there any api for those?

  2. Hello Licson,
    is there a way to download the mp3 Version of a video?
    And how can I directly download the video to the users browser instead of saving it in the server ( I sometimes have over 1k videos. That would be way too much to save in the server )?


  3. How would I add an input box and a button to this? I would lile for my users to be able to imput the URL and click the button, starting the process.

  4. Hi,
    After this line: $file = fopen(‘video.’.str_replace($format,’video/’,”),’w’);
    I recive this error:
    PHP Warning: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
    in /home/public_html/index.php on line 23
    what is this?

  5. Does your site have a contact page? I’m having
    trouble locating it but, I’d like to shoot you an e-mail.
    I’ve got some recommendations for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it develop over time.

  6. This script is not working any more, but it did help. I guess YouTube updated their security measures. Now they seem to be checking your IP address, so a signature is valid only for one IP address, and you need to specify more parameters. Here is what I did :

    $streams = $info['url_encoded_fmt_stream_map']; //the video's location info
    $streams = explode(',',$streams);
    parse_str(urldecode($streams[0]),$data);  //In this example I am downloading only the first video
    	// Returns a 403 error
    	echo '';
    	$query = str_replace('%2C', ',' ,$url.'&'.http_build_query($data).'&signature='.$sig); //html_entity_decode should do the trick too (instead of str_replace)
    	file_get_contents($query);  //downloads the file

    I actually wanted to host the script on my server. The problem with YouTube checking the IP address is that in my case I must download the videos on my server, then download them from my server to my laptop. I can’t directly access the YouTube servers with the generated URL, but that’s not a big issue.

    1. Can you give me more information such as the youtube video ID and the contents of the result video file so that I can fix it? Also you need to wait for the script finished as that’s mean the video is fully downloaded.

  7. Something wrong: in the third pre tag

    “parse_str($stream,$data); //decode the stream”, not “prase_str($stream,$data); //decode the stream”

    1. According to what I’ve tested, it works pretty fine. If there’re any changes to the API, I’ll update my post as soon as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *