views:

88

answers:

1

Hi guys,

I was wondering if there is a way, after all the changes in youtube in the last months, to write a script that enables downloading videos?

I'm aware of the fact that the methods that worked a year ago, are no longer relevant.

+1  A: 

Yes there is still a way. I'm not going to write the whole script for you, but I'll at least write you the function that provides the download link from YouTube.

Alright, you're going to need CURL installed to do this.

/* Developed by User - WebEntrepreneur @ StackOverlow.com */

function ___get_youtube_video($youtube_url)
{
    if(eregi('youtube.com', $youtube_url))
    {
        preg_match('/http:\/\/(.+)youtube.com\/watch(.+)?v=(.+)/', $youtube_url, $youtube_id_regex);
        $youtube_id = ($youtube_id_regex[3]) ? $youtube_id_regex[3] : '';

        if(!$youtube_id)
        {
            return INVALID_YOUTUBE_ID;
        }

        if(eregi('\&', $youtube_id))
        {
            $youtube_id_m = explode('&', $youtube_id);
            foreach($youtube_id_m as $slices)
            {
                $youtube_id = $slices;
                break;
            }
        }

    } else {
        $youtube_id = ($youtube_url);
    }

    $ping = ___get_curl("http://www.youtube.com/watch?v={$youtube_id}&feature=youtu.be");

    if(!$ping)
    {
        return YOUTUBE_UNAVAILABLE;
    }

    $ping_scan = nl2br($ping);
    $ping_scan = explode('<br />', $ping_scan);

    if(eregi('= null', $ping_scan[36]) or !$ping_scan[36])
    {
        return YOUTUBE_TOO_MANY_REQUESTS;
    }

    $ping_scan = str_replace("\n", "", $ping_scan[36]);
    $ping_scan = str_replace("              img.src = '", "", $ping_scan);
    $out[1] = str_replace("';", "", $ping_scan);

    $sub_ping = ___get_curl("http://gdata.youtube.com/feeds/api/videos/{$youtube_id}");

    preg_match('/<title type=\'text\'>(.+)<\/title>/', $sub_ping, $inout);

    $inout = $inout[1];

    if(!$out[1])
    {
        return VERSION_EXPIRED;
    }

    $out[1] = str_replace("generate_204", "videoplayback", $out[1]);
    $out[1] = str_replace("\\/", "/", $out[1]);
    $out[1] = rawurldecode($out[1]);

    if($inout)
    {
        $out[1] .= "&file=".urlencode($inout).".mp4";
        $filename = urlencode($inout).".mp4";
    }

    header("Content-Disposition: attachment; filename=\"".$filename."\"");
    flush();
    return ___get_curl($out[1]);
}

function ___get_curl($url)
{

    if(!function_exists("curl_setopt"))
    {
        return CURL_NOT_INSTALLED;
    }

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_USERAGENT, "YouTube Video Downloader");
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,100000);

    $curl_output=curl_exec($ch);
    $curlstatus=curl_getinfo($ch);
    curl_close($ch);

    return $curl_output;

}

Now, let me talk about the code. After around 30 minutes of research, I managed to hack their algorithm however it does have very strict boundaries which they've set in now.

As each request made for a video download is only tied to the IP, you would need to stream it out of your bandwidth which is what the script above does. However, sites like keepvid.com use Java to grab the download url and stream it to the user. I've also put in my own YouTube ID grabber which is very handy for these kind of tools.

Please acknowledge that as stated, YouTube does always change their algorithms and usage of this tool is at your own risk. I am not held liable for any damages made to YouTube.

Hope it sets a good boundary for you, spent a while making it.

WebEntrepreneur
Thanks man. That's just the thing I was looking for
Alex1987
Ok, I tried this code and it downloads html file. This is how I used your script echo ___get_youtube_video("http://www.youtube.com/watch?v=dDwKPGUIVME");and it downloads Alizee+-+Moi+Lolita.mp4.html
Alex1987
It could be your server, trying debugging it and see what comes up.
WebEntrepreneur