序:优酷之前更新了次算法(很久之前了,呵呵。。。),故此很多博客的解析算法已经无法使用。很多大牛也已经更新了新的解析方法。我也在此写篇解析过程的文章。(本文使用语言为C#)
由于优酷视频地址时间限制,在你访问本篇文章时,下面所属链接有可能已经失效,望见谅。
例:http://v.youku.com/v_show/id_XNzk2NTI0MzMy.html
1:获取视频vid
在视频url中标红部分。一个正则表达式即可获取。
string getVid(string url)
{
string strRegex = "(?<=id_)(\\w+)";
Regex reg = new Regex(strRegex);
Match match = reg.Match(url);
return match.ToString();
}
</div>
2:获取视频元信息
http://v.youku.com/player/getPlayList/VideoIDS/XNzk2NTI0MzMy/Pf/4/ctype/12/ev/1
将前述vid嵌入到上面url中访问即可得到视频信息文件。由于视频信息过长不在此贴出全部内容。下面是部分重要内容的展示。(获取文件为json文件,可直接解析)
{ "data": [ {
"ip": 1991941296,
"ep": "MwXRTAsbJLnb0PbJ8uJxAdSivUU11wnKXxc=",
"segs": {
"hd2": [
{
"no": "0",
"size": "34602810",
"seconds": 205,
"k": "248fe14b4c1b37302411f67a",
"k2": "1c8e113cecad924c5"
},
{
"no": "1",
},] }, } ],}
</div>
上面显示的内容后面都会使用到。其中segs包含hd3,hd2,flv,mp4,3gp等各种格式,并且每种格式下均分为若干段。本次选用清晰度较高的hd2(视频格式为flv)
3:拼接m3u8地址
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比较简单,后面会说。
3.1计算ep,sid,token
计算方法单纯的为数学计算,下面给出计算的函数。三个参数可一次性计算得到。其中涉及到Base64编码解码知识,点击查看。
private static string myEncoder(string a, byte[] c, bool isToBase64)
{
string result = "";
List<Byte> bytesR = new List<byte>();
int f = 0, h = 0, q = 0;
int[] b = new int[256];
for (int i = 0; i < 256; i++)
b[i] = i;
while (h < 256)
{
f = (f + b[h] + a[h % a.Length]) % 256;
int temp = b[h];
b[h] = b[f];
b[f] = temp;
h++;
}
f = 0; h = 0; q = 0;
while (q < c.Length)
{
h = (h + 1) % 256;
f = (f + b[h]) % 256;
int temp = b[h];
b[h] = b[f];
b[f] = temp;
byte[] bytes = new byte[] { (byte)(c[q] ^ b[(b[h] + b[f]) % 256]) };
bytesR.Add(bytes[0]);
result += System.Text.ASCIIEncoding.ASCII.GetString(bytes);
q++;
}
if (isToBase64)
{
Byte[] byteR = bytesR.ToArray();
result = Convert.ToBase64String(byteR);
}
return result;