序:優酷之前更新了次算法(很久之前了,呵呵。。。),故此很多博客的解析算法已經無法使用。很多大牛也已經更新了新的解析方法。我也在此寫篇解析過程的文章。(本文使用語言為C#)
由於優酷視頻地址時間限制,在你訪問本篇文章時,下面所屬鏈接有可能已經失效,望見諒。
例:http://v.youku.com/v_show/id_XNzk2NTI0MzMy.html
在視頻url中標紅部分。一個正則表達式即可獲取。
1 string getVid(string url)
2 {
3 string strRegex = "(?<=id_)(\\w+)";
4 Regex reg = new Regex(strRegex);
5 Match match = reg.Match(url);
6 return match.ToString();
7 }
http://v.youku.com/player/getPlayList/VideoIDS/XNzk2NTI0MzMy/Pf/4/ctype/12/ev/1
將前述vid嵌入到上面url中訪問即可得到視頻信息文件。由於視頻信息過長不在此貼出全部內容。下面是部分重要內容的展示。(獲取文件為json文件,可直接解析)
1 { "data": [ {
2 "ip": 1991941296,
3 "ep": "MwXRTAsbJLnb0PbJ8uJxAdSivUU11wnKXxc=",
4 "segs": {
5 "hd2": [
6 {
7 "no": "0",
8 "size": "34602810",
9 "seconds": 205,
10 "k": "248fe14b4c1b37302411f67a",
11 "k2": "1c8e113cecad924c5"
12 },
13 {
14 "no": "1",
15 },] }, } ],}
上面顯示的內容後面都會使用到。其中segs包含hd3,hd2,flv,mp4,3gp等各種格式,並且每種格式下均分為若干段。本次選用清晰度較高的hd2(視頻格式為flv)
http://pl.youku.com/playlist/m3u8?ctype=12&ep={0}&ev=1&keyframe=1&oip={1}&sid={2}&token={3}&type={4}&vid={5}
以上共有6個參數,其中vid和oip已經得到,分別之前的vid和json文件中的ip字段,即(XNzk2NTI0MzMy和1991941296),但是ep,sid,token需要重新計算(json文件中的ep值不能直接使用)。type比較簡單,後面會說。
計算方法單純的為數學計算,下面給出計算的函數。三個參數可一次性計算得到。其中涉及到Base64編碼解碼知識,點擊查看。
1 private static string myEncoder(string a, byte[] c, bool isToBase64)
2 {
3 string result = "";
4 List<Byte> bytesR = new List<byte>();
5 int f = 0, h = 0, q = 0;
6 int[] b = new int[256];
7 for (int i = 0; i < 256; i++)
8 b[i] = i;
9 while (h < 256)
10 {
11 f = (f + b[h] + a[h % a.Length]) % 256;
12 int temp = b[h];
13 b[h] = b[f];
14 b[f] = temp;
15 h++;
16 }
17 f = 0; h = 0; q = 0;
18 while (q < c.Length)
19 {
20 h = (h + 1) % 256;
21 f = (f + b[h]) % 256;
22 int temp = b[h];
23 b[h] = b[f];
24 b[f] = temp;
25 byte[] bytes = new byte[] { (byte)(c[q] ^ b[(b[h] + b[f]) % 256]) };
26 bytesR.Add(bytes[0]);
27 result += System.Text.ASCIIEncoding.ASCII.GetString(bytes);
28 q++;
29 }
30 if (isToBase64)
31 {
32 Byte[] byteR = bytesR.ToArray();
33 result = Convert.ToBase64String(byteR);
34 }
35 return result;
36 }
37 public static void getEp(string vid, string ep, ref string pNew, ref string token, ref string sid)
38 {
39 string template1 = "becaf9be";
40 string template2 = "bf7e5f01";
41 byte[] bytes = Convert.FromBase64String(ep);
42 ep = ystem.Text.ASCIIEncoding.ASCII.GetString(bytes);
43 string temp = myEncoder(template1, bytes, false);
44 string[] part = temp.Split('_');
45 sid = part[0];
46 token = part[1];
47 string whole = string.Format("{0}_{1}_{2}", sid, vid, token);
48 byte[] newbytes = System.Text.ASCIIEncoding.ASCII.GetBytes(whole);
49 epNew = myEncoder(template2, newbytes, true);
50 }