Wednesday, March 26, 2014

Code download Video from Youtube in PHP

@set_time_limit(0);
$id = '8ZF-MIV25Yg';//$_GET['id']; //The youtube video ID
$type = 'video/mp4';//$_GET['type']; //the MIME type of the video

parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id='.$id),$info);
//var_dump($info); die;
$streams = explode(',',$info['url_encoded_fmt_stream_map']);

foreach($streams as $stream){
    parse_str($stream,$real_stream);
    $stype = $real_stream['type'];

    if(strpos($real_stream['type'],';') !== false){
        $tmp = explode(';',$real_stream['type']);
        $stype = $tmp[0];
        unset($tmp);
    }

    if($stype == $type && ($real_stream['quality'] == 'large' || $real_stream['quality'] == 'medium' || $real_stream['quality'] == 'small')){
        $real_stream['quality'] = 'large';
//        var_dump($stype);
        header('Content-type: '.$stype);
//        header('Transfer-encoding: chunked');
//        echo $real_stream['url'].'&signature='.$real_stream['sig']; die;
        @readfile($real_stream['url'].'&signature='.$real_stream['sig']); //Change here to do other things such as save the file to the filesystem etc.
        ob_flush();
        flush();
        break;
    }
}
die;

No comments:

Post a Comment