Home  >  Article  >  Backend Development  >  Detailed explanation of how to obtain the absolute address of Youku videos_PHP tutorial

Detailed explanation of how to obtain the absolute address of Youku videos_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:431316browse

A while ago, I conducted a series of research on video sites in order to study KnLiveCommentary. Since KnLiveCommentary needs to be able to obtain sufficient video sources for testing, we selected Youku, a relatively large video website, for testing.
In fact, I started studying the resolution of absolute addresses to study Youku’s built-in player and remove advertisements. Later, we "decompiled" Youku's player using ASV6 (ActionScript Viewer 6), and achieved amazing results.

Youku videos adopt an encryption + dynamic acquisition method. The video address needs to be dynamically obtained by visiting the website, and the result needs to be decrypted and other operations.

Copy code The code is as follows:

$base_url = 'http://v.youku.com/player/getPlayList/ VideoIDS/'; //Get the base address of the video information
$_VIDEO_ID = $_GET['vid']; //Extract the Video Id from GET
if($_VIDEO_ID=='')
$_VIDEO_ID = 'XMjY0ODE1MDA0'; //I'm lazy, so I fixed one during testing
$ch = curl_init(); //Open the cURL object
curl_setopt($ch, CURLOPT_URL, $base_url . $ _VIDEO_ID); //Get the address of this video information
curl_setopt($ch, CURLOPT_HEADER, 1); //Require HEADER
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://v.youku.com/v_show/id_' . $_VIDEO_ID); //Give a fake "REFERER"
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //Pass the current browser User Agent to the server
curl_setopt($ch, CURLOPT_NOBODY, 0);
$content = curl_exec($ch); //Execute! ! !
curl_close($ch); /*Analysis below*/
preg_match('~"seed"s*:s*(d+)s*,~iUs',$content,$seed);
preg_match('~{s*”(flv|mp4)”s*:s*”(.*)”s*}~iUs',$content,$encoded);
preg_match('~”key1″s *:s*”(.*)”s*,~iUs',$content,$key1);
preg_match('~”key2″s*:s*”(.*)”s*,~iUs ',$content,$key2);
//Extract necessary information from the returned JSON string seed, encoded_url, key1, key2
class decoder{
var $randomSeed = 0;
var $ cg_str="";
function __construct($seed){
$this->randomSeed = $seed;
}
function ran(){
$this->randomSeed = (($this->randomSeed * 211)+30031)%65536;
return ($this->randomSeed / 65536);// Calculate the new Seed based on the old Seed and return the proportional position of the Seed [0,1)
}
function cg_hun(){ //I guess this is called "CG mixing", anyway, the function of ASV solution is this name
$this->cg_str="";
$sttext = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/:._-1234567890'; //Default string (maximum)
$len = strlen($sttext); //Get its length
for($i=0;$ i<$len;$i++){
$cuch = (int)($this->ran()*strlen($sttext)); //Get the character subscript of the string Seed proportional position
$this->cg_str.=$sttext[$cuch]; //Read out the letters
$sttext = str_replace($sttext[$cuch],”,$sttext); //Delete this read out Letters (stop at 0)
}
}
function decode($string){
$output=””;
$this->cg_hun();
$ expl = explode('*',$string); //Break up the string 1*23*34*45*56*
for($i=0;$i$output.=$this->cg_str[(int)$expl[$i]]; //Get the cg_hun represented by the digits to scramble the string characters, and then the decryption is completed
}
return $output; //OK pull
}
function decode_key($key1,$key2){
$key = hexdec($key1); //Both Keys are HEX
$key = $key ^ -1520786011; //This turned out to be an 8-bit HEX, but I later used a calculator to calculate the value because it is convenient for PhP bit operations
return $key2 . dechex($key) ; //Synthesize the final Key
}
}//Decryption class, it is very convenient to use this $new = new decoder((int)$seed[1]);
$fileid = $new-> decode($encoded[2]);
$key = $new->decode_key($key1[1],$key2[1]);
//Feed the data and calculate//Address load Composition
$s7 = substr($fileid,10,strlen($fileid));
$s5 = substr($fileid,0,8);
$s6 = substr($fileid,6, 2);
//Open $s4 = '00';//Note that this is a HEX value, that is, 00 represents the first segment of the video, 01 the second 0f the fifteenth... and so on $ sid = time() . mt_rand(10,99) . '1000' . mt_rand(30,80) . '00';//Get a random SID and give it to the server (it will not be checked in fact)
$d_ADDR = 'http://f.youku.com/player/getFlvPath/sid/' . $sid . '_' . $s4 . '/st/' . $encoded[1] . '/fileid/' . $file_id ;
echo $d_ADDR . '?K=' . $key;
//Finally output the address

Please note that due to Youku’s replacement algorithm/format, the above method can no longer handle all situations. Let me describe the current process:
1. Visit http:/ /v.youku.com/player/getPlayList/VideoIDS/[ID]
2. Get the file and parse "streamfileids": {"flv":"encrypted address","mp4":"encrypted address"," Etc.":"Encrypted address"
3. Follow the above method to crack the encrypted address
4. Get the number of segments and K
{"mp4":[{"no":"0", "size":"18367795","seconds":"421","k":"281ff2875db680bb261c02ce"},{"no":"1","size":"19045091","seconds":"421", "k":"45398cdd4aa44968261c02ce"},
......
5. Synthesize the address, but the K of each segment uses the new K
obtained above

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327891.htmlTechArticleA while ago, we conducted a series of research on video sites in order to study KnLiveCommentary. Since KnLiveCommentary needs to be able to obtain sufficient video sources for testing, we choose...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn