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

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

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.

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!

Published by

Licson Lee

Hi! I’m Licson. I’m a student who loves programming.

51 thoughts on “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 )?

    Greetings,
    Cr4fTeXe

  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 :

    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 *