欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php获取youku视频最终地址的方法

程序员文章站 2022-04-27 13:58:44
...
  1. /**

  2. * 取得youku视频的最终地址
  3. * edit by bbs.it-home.org
  4. * at: 2013-10-2
  5. */
  6. class YoukuFlv{
  7. static private $error = "";
  8. static private $result = array();
  9. static public function getYoukuFlv($url){
  10. //从url获取youkuid
  11. if(! $id = self::getYoukuId($url)){
  12. return false;
  13. }
  14. //获取youku视频详细信息
  15. $content = self::get_curl_contents( "http://v.youku.com/player/getPlayList/VideoIDS/".$id );
  16. $data = json_decode($content);
  17. if(!isset($data->data[0]->streamfileids)){
  18. self::$error = "Cannot find this video";
  19. return false;
  20. }
  21. foreach($data->data[0]->streamfileids AS $k=>$v){
  22. if($k == 'flv' || $k == 'mp4'){
  23. //sid
  24. $sid= self::getSid();
  25. //fileid
  26. $fileid = self::getfileid($v,$data->data[0]->seed);
  27. $one=($data->data[0]->segs->$k);
  28. self::$result[$k] = "http://f.youku.com/player/getFlvPath/sid/{$sid}_00/st/{$k}/fileid/{$fileid}?K={$one[0]->k}";
  29. }
  30. }
  31. if(empty(self::$result)){
  32. self::$error = "THIS VIOD IS NOT IN MP4 OR FLV FORMAT";
  33. return false;
  34. }else{
  35. return true;
  36. }
  37. }
  38. static public function error(){
  39. return self::$error;
  40. }
  41. static public function result(){
  42. return self::$result;
  43. }
  44. static private function getYoukuId($url){
  45. //url 不能为空
  46. if($url == "" || substr($url , 0 , 29) != "http://v.youku.com/v_show/id_"){
  47. self::$error = "URL IS ERROR";
  48. return false;
  49. }
  50. return substr($url , 29 , -5);
  51. }
  52. static private function get_curl_contents($url, $second = 5){
  53. if(!function_exists('curl_init')) die('php.ini未开启php_curl.dll');
  54. $c = curl_init();
  55. curl_setopt($c,CURLOPT_URL,$url);
  56. $UserAgent=$_SERVER['HTTP_USER_AGENT'];
  57. curl_setopt($c,CURLOPT_USERAGENT,$UserAgent);
  58. curl_setopt($c,CURLOPT_HEADER,0);
  59. curl_setopt($c,CURLOPT_TIMEOUT,$second);
  60. curl_setopt($c,CURLOPT_RETURNTRANSFER, true);
  61. $cnt = curl_exec($c);
  62. curl_close($c);
  63. return $cnt;
  64. }
  65. static private function getSid() {
  66. $sid = time().(rand(0,9000)+10000);
  67. return $sid;
  68. }
  69. static private function getfileid($fileId,$seed) {
  70. $mixed = self::getMixString($seed);
  71. $ids = explode("*",$fileId);
  72. unset($ids[count($ids)-1]);
  73. $realId = "";
  74. for ($i=0;$i $idx = $ids[$i];
  75. $realId .= substr($mixed,$idx,1);
  76. }
  77. return $realId;
  78. }
  79. static private function getMixString($seed) {
  80. $mixed = "";
  81. $source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
  82. $len = strlen($source);
  83. for($i=0;$i $seed = ($seed * 211 + 30031) % 65536;
  84. $index = ($seed / 65536 * strlen($source));
  85. $c = substr($source,$index,1);
  86. $mixed .= $c;
  87. $source = str_replace($c, "",$source);
  88. }
  89. return $mixed;
  90. }
  91. }
  92. if(YoukuFlv::getYoukuFlv("http://v.youku.com/v_show/id_XNjEyOTE4NTEy_ev_1.html")){
  93. print_r( YoukuFlv::result() );
  94. }else{
  95. echo YoukuFlv::error();
  96. }
  97. ?>

复制代码

另外一种调用方法: http://player.youku.com/player.php/sid/XMjI0MDIwNDc2/v.swf http://player.youku.com/player.php/sid/{$id}/v.swf