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.

<?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!

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

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

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

  3. 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=&quot;.$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;
    }
    }
    *********************************************************

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

Leave a Reply to Vojta Cancel reply

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