Youku Video Absolute Address acquisition method detailed _php skill

Source: Internet
Author: User
For a while, a series of research on video sites was conducted to study knlivecommentary. Since knlivecommentary need to be able to get enough video sources to test, we chose Youku (Youku) a larger video site to test.
In fact, the beginning of the study to resolve the absolute address is also to study the Youku of the player, by the way to remove ads or something. Later, we put the Youku player ASV6 (ActionScript Viewer 6) "decompile" a bit, to achieve amazing results.

Youku video to take the encryption + dynamic access, video addresses need to access the Web site dynamic acquisition, and the results also need to be decrypted and other operations.
Copy Code code as follows:

$base _url = ' http://v.youku.com/player/getPlayList/VideoIDS/'; Access to the address site of the video information
$_video_id = $_get[' vid ']; Extract the video ID from the Get inside
if ($_video_id== ')
$_video_id = ' xmjy0ode1mda0 '; I'm lazy, I have a fixed one during the test.
$ch = Curl_init (); Open Curl Object
curl_setopt ($ch, Curlopt_url, $base _url. $_video_id); Get the address of the information for this video
curl_setopt ($ch, Curlopt_header, 1); To 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); Perform!!!
Curl_close ($ch); /* Below Analysis * *
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, $key 1);
Preg_match (' ~ "key2″\s*:\s*" (. *) "\s*,~ius", $content, $key 2);
Extracts the 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 returns the proportional position of a seed [0,1)
}
function Cg_hun () {///estimated this is called "CG Mix", anyway ASV solution is the name
$this->cg_str= "";
$sttext = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz/\:._-1234567890 '; Default string (max)
$len = strlen ($sttext); Get its length
for ($i =0; $i < $len; $i + +) {
$cuch = (int) ($this->ran () *strlen ($sttext)); Gets the character subscript for the string seed proportional position
$this->cg_str.= $sttext [$cuch]; Read the letters.
$sttext = Str_replace ($sttext [$cuch], $sttext); Remove the Read letter (stop at 0)
}
}
function decode ($string) {
$output = "";
$this->cg_hun ();
$EXPL = Explode (' * ', $string); Break this string of 1*23*34*45*56*
For ($i =0 $i <count ($EXPL)-1; $i + +) {
$output. = $this->cg_str[(int) $EXPL [$i]]; Gets the numeric bit represented by the Cg_hun to disrupt the string character, from which the decryption completes
}
return $output; OK pull
}
function Decode_key ($key 1, $key 2) {
$key = Hexdec ($key 1); Two key is hex
$key = $key ^-1520786011; This turned out to be a 8-bit hex, and then I used the calculator to calculate the value, because this convenient PHP bit operation
Return $key 2. Dechex ($key); Synthetic final Key
}
}//decryption class, with this very convenient $new = new Decoder ((int) $seed [1]);
$fileid = $new->decode ($encoded [2]);
$key = $new->decode_key ($key 1[1], $key 2[1]);
Feed the data, calculate//Address the composition
$s 7 = substr ($fileid, 10,strlen ($fileid));
$s 5 = substr ($fileid, 0,8);
$s 6 = substr ($fileid, 6,2);
Take apart $s4 = ' 00′;//Note that this is a HEX value, that is, 00 represents video first segment, 01 second 0f 15th ... And so on $sid = time (). Mt_rand (10,99). ' 1000′. Mt_rand (30,80). ' 00′;//gets a random SID to the server (in fact it won't be checked)
$d _addr = ' Http://f.youku.com/player/getFlvPath/sid/'. $sid. ‘_'. $s 4. '/st/'. $encoded [1]. '/fileid/'. $file _id;
Echo $d _addr. ‘? K= '. $key;
Finally, the address output

Please note that since the Youku replacement algorithm/format has not been able to handle all cases, let me describe the current process:
1. Visit Http://v.youku.com/player/getplaylist/videoids/[id]
2. Get the file and resolve "Streamfileids": {"flv": "Encrypted Address", "Mp4″:" Encrypted address, "etc.": "Encrypted Address"
3. To crack the encrypted address according to the above method
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. Synthetic address, but each section K is the new K obtained above

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.