好东西又来了,通过优酷链接获取优酷真实视频地址

jerry thinkphp 2015年11月18日 收藏
好东西又来了,通过优酷链接获取优酷真实视频地址,需要的朋友可以拿去研究下,已测试过
  1. <?php

    /*
    返回:
    array
      'mp4' => string 'http://f.youku.com/player/getFlvPath/sid/138041779515547_00/st/mp4/fileid/03000804005241236C2C5300422C39EBED4FE4-2CCC-71BB-6AC3-59B4332828D7?K=48daa69002023a2a2828f4f1' (length=167)
      'flv' => string 'http://f.youku.com/player/getFlvPath/sid/138041779514451_00/st/flv/fileid/030002040052410FA52C5300422C39EBED4FE4-2CCC-71BB-6AC3-59B4332828D7?K=b9c1dfedde4eb5052828f4f1' (length=167)
    */

    if(YoukuFlv::getYoukuFlv("http://v.youku.com/v_show/id_XNjEyOTE4NTEy_ev_1.html"))
    {
        var_dump( YoukuFlv::result() );
    }
    else
    {
        echo YoukuFlv::error();
    }
     
     
     
    /*
    获取优酷视频地址    
    */
    class YoukuFlv{
     
        static private $error   =   "";
        static private $result  =   array();
     
        static public function getYoukuFlv($url){
            //从url获取youkuid
            if(! $id    =   self::getYoukuId($url)){
                return false;
            }
            //获取youku视频详细信息
            $content    =   self::get_curl_contents( "http://v.youku.com/player/getPlayList/VideoIDS/".$id );
            $data   =   json_decode($content);
            if(!isset($data->data[0]->streamfileids)){
                self::$error    =   "Cannot find this video";
                return false;
            }
            foreach($data->data[0]->streamfileids AS $k=>$v){
                if($k == 'flv' || $k == 'mp4'){         
                    //sid
                    $sid=   self::getSid();
                    //fileid
                    $fileid =   self::getfileid($v,$data->data[0]->seed);
                    $one=($data->data[0]->segs->$k);
                    self::$result[$k]   = "http://f.youku.com/player/getFlvPath/sid/{$sid}_00/st/{$k}/fileid/{$fileid}?K={$one[0]->k}";
                }
            }
            if(empty(self::$result)){
                self::$error    =   "THIS VIOD IS NOT IN MP4 OR FLV FORMAT";
                return false;
            }else{
                return true;
            }
        } 
        static public function error(){
            return self::$error;
        }
     
        static public function result(){
            return self::$result;
        }
     
        static private function getYoukuId($url){       
            //url 不能为空
            if($url == "" || substr($url , 0 , 29) != "http://v.youku.com/v_show/id_"){
                self::$error    =   "URL IS ERROR";
                return false;
            }
            return substr($url , 29 , -5);      
        }
     
        static private function get_curl_contents($url, $second = 5){
            if(!function_exists('curl_init')) die('php.ini未开启php_curl.dll');
            $c = curl_init();
            curl_setopt($c,CURLOPT_URL,$url);
            $UserAgent=$_SERVER['HTTP_USER_AGENT'];
            curl_setopt($c,CURLOPT_USERAGENT,$UserAgent);
            curl_setopt($c,CURLOPT_HEADER,0);
            curl_setopt($c,CURLOPT_TIMEOUT,$second);
            curl_setopt($c,CURLOPT_RETURNTRANSFER, true);
            $cnt = curl_exec($c);
            curl_close($c);
            return $cnt;
        }
        static private function getSid() {
            $sid = time().(rand(0,9000)+10000);
            return $sid;
        }
        static private function getfileid($fileId,$seed) {
            $mixed = self::getMixString($seed);
            $ids = explode("*",$fileId);
            unset($ids[count($ids)-1]);
            $realId = "";
            for ($i=0;$i < count($ids);++$i) {
                $idx = $ids[$i];
                $realId .= substr($mixed,$idx,1);
            }
            return $realId;
        }
        static private function getMixString($seed) {
            $mixed = "";
            $source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
            $len = strlen($source);
            for($i=0;$i< $len;++$i){
                $seed = ($seed * 211 + 30031) % 65536;
                $index = ($seed / 65536 * strlen($source));
                $c = substr($source,$index,1);
                $mixed .= $c;
                $source = str_replace($c, "",$source);
            }
            return $mixed;
        }
    }
     
     
     

    ?>