优酷视频解析正则算法[m3u8]

  • 2013-09-12
  • 0
  • 0

1.class youku_m3u8
2.{
3. public static function curl($url, $carry_header = true, $REFERER_ = 0, $add_arry_header = 0)
4. {
5. $ch = curl_init($url);
6. if ($carry_header)
7. {
8. curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36’));
9. }
10. if ($add_arry_header)
11. {
12. curl_setopt($ch, CURLOPT_HTTPHEADER, $add_arry_header);
13. }
14. if ($REFERER_)
15. {
16. curl_setopt($ch, CURLOPT_REFERER, $REFERER_);
17. }
18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
19. $get_url = curl_exec($ch);
20. curl_close($ch);
21. return $get_url;
22. }
23.
24. public static function charCodeAt($str, $index)
25. {
26. $charCode = array();
27. $key = md5($str);
28. $index = $index + 1;
29. if (isset($charCode[$key]))
30. {
31. return $charCode[$key][$index];
32. }
33. $charCode[$key] = unpack(‘C*’, $str);
34. return $charCode[$key][$index];
35. }
36. public static function charAt($str, $index = 0)
37. {
38. return substr($str, $index, 1);
39. }
40. public static function fromCharCode($codes)
41. {
42. if (is_scalar($codes))
43. {
44. $codes = func_get_args();
45. }
46. $str = ”;
47. foreach ($codes as $code)
48. {
49. $str .= chr($code);
50. }
51. return $str;
52. }
53. public static function yk_e($a, $c)
54. {
55. for ($f = 0, $i, $e = ”, $h = 0; 256 > $h; $h++)
56. {
57. $b[$h] = $h;
58. }
59. for ($h = 0; 256 > $h; $h++)
60. {
61. $f = (($f + $b[$h]) + self::charCodeAt($a, $h % strlen($a))) % 256;
62. $i = $b[$h];
63. $b[$h] = $b[$f];
64. $b[$f] = $i;
65. }
66. for ($q = ($f = ($h = 0)); $q < strlen($c); $q++)
67. {
68. $h = ($h + 1) % 256;
69. $f = ($f + $b[$h]) % 256;
70. $i = $b[$h];
71. $b[$h] = $b[$f];
72. $b[$f] = $i;
73. $e .= self::fromCharCode(self::charCodeAt($c, $q) ^ $b[($b[$h] + $b[$f]) % 256]);
74. }
75. return $e;
76. }
77. public static function yk_d($a)
78. {
79. $str = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/’;
80. if (!$a)
81. {
82. return ”;
83. }
84. $f = strlen($a);
85. $b = 0;
86. for ($c = ”; $b < $f ; ) 87. { 88. $e = self::charCodeAt($a, $b++) &255; 89. if ($b == $f) 90. { 91. $c .= self::charAt($str, $e >> 2);
92. $c .= self::charAt($str, ($e &3) << 4); 93. $c .= ‘==’; 94. break; 95. } 96. $g = self::charCodeAt($a, $b++); 97. if ($b == $f) 98. { 99. $c .= self::charAt($str, $e >> 2);
100. $c .= self::charAt($str, ($e &3) << 4 | ($g &240) >> 4);
101. $c .= self::charAt($str, ($g &15) << 2); 102. $c .= ‘=’; 103. break; 104. } 105. $h = self::charCodeAt($a, $b++); 106. $c .= self::charAt($str, $e >> 2);
107. $c .= self::charAt($str, ($e &3) << 4 | ($g &240) >> 4);
108. $c .= self::charAt($str, ($g &15) << 2 | ($h &192) >> 6);
109. $c .= self::charAt($str, $h &63);
110. }
111. return $c;
112. }
113.
114. public static function get_m3u8_url($youku_ID, $type = ‘flv’)
115. {
116. $video_info = self::curl(‘http://v.youku.com/player/getPlayList/VideoIDS/’ . $youku_ID . ‘/ctype/12/ev/1’);
117. $obj = json_decode($video_info);
118. $vid = $obj->data[0]->videoid;
119. $oip = $obj->data[0]->ip;
120. $epdata = $obj->data[0]->ep;
121. $youku_m3u8 = self::_calc_ep2($vid, $epdata);
122. $m3u8_url = ‘http://pl.youku.com/playlist/m3u8?vid=’ . $vid . ‘&type=’ . $type . ‘&ep=’ . urlencode($youku_m3u8[‘ep’]) . ‘&token=’ . $youku_m3u8[‘token’] . ‘&ctype=12&ev=1&oip=’ . $oip . ‘&sid=’ . $youku_m3u8[‘sid’];
123. return $m3u8_url;
124. }
125. public static function _calc_ep2($vid, $ep)
126. {
127. $e_code = self::yk_e(‘becaf9be’, base64_decode($ep));
128. $s_t = explode(‘_’, $e_code);
129. $sid = $s_t[0];
130. $token = $s_t[1];
131. // $new_ep = trans_e(‘bf7e5f01’, ‘%s_%s_%s’ % ($sid.$vid.$token));
132. $new_ep = self::yk_e(‘bf7e5f01’, $sid . ‘_’ . $vid . ‘_’ . $token);
133.
134. $new_ep = base64_encode($new_ep); //可以换作下面代码
135. // $new_ep = iconv(‘gbk’, ‘UTF-8’, self::yk_d($new_ep));
136. return array(‘ep’ => $new_ep,
137. ‘token’ => $token,
138. ‘sid’ => $sid,
139. );
140. }
141.}

评论

还没有任何评论,你来说两句吧

CC共享许可协议:BY-NC-SA 4.0 - 友情链接 - Theme by Qzhai