原文章的代码是用C#写的。本文在作者代码的基础上将代码转换成Java代码,方便Java程序员使用。
例:http://v.youku.com/v_show/id_XNzk2NTI0MzMy.html
1:获取视频vid
在视频url中标红部分。一个正则表达式即可获取。
- public static String getVid(String url) {
- String s = "";
- String strRegex = "(?<=id_)(\\w+)";
- Pattern pattern = Pattern.compile(strRegex);
- Matcher matcher = pattern.matcher(url);
- while (matcher.find()) {
- s = matcher.group();
- }
- return s;
- }
2:获取视频元信息
http://v.youku.com/player/getPlayList/VideoIDS/XNzk2NTI0MzMy/Pf/4/ctype/12/ev/1
将前述vid嵌入到上面url中访问即可得到视频信息文件。由于视频信息过长不在此贴出全部内容。下面是部分重要内容的展示。(获取文件为json文件,可直接解析)
- { "data": [ {
- "ip": 996949050,
- "ep": "NQXRTAodIbrd1vnC8+JxB4emuRs41w7DWho=",
- "segs": {
- "hd2": [
- {
- "no": "0",
- "size": "34602810",
- "seconds": 205,
- "k": "248fe14b4c1b37302411f67a",
- "k2": "1c8e113cecad924c5"
- },
- {
- "no": "1",
- },]
- },
- }
- ],
- }
上面显示的内容后面都会使用到。其中segs包含hd3,hd2,flv,mp4,3gp等各种格式,并且每种格式下均分为若干段。本次选用清晰度较高的hd2(视频格式为flv)
3:拼接m3u8地址
以上共有6个参数,其中vid和oip已经得到,分别之前的vid和json文件中的ip字段,即(XNzk2NTI0MzMy和1991941296),但是ep,sid,token需要重新计算(json文件中的ep值不能直接使用)。type即为之前选择的segs。
3.1计算ep,sid,token
计算方法单纯的为数学计算,下面给出计算的函数。三个参数可一次性计算得到。其中涉及到Base64编码解码知识,点击查看。
- private static String myEncoder(String a, byte[] c, boolean isToBase64) {
- try {
- String result = "";
- ArrayList<Byte> bytesR = new ArrayList<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.charAt(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 += new String(bytes, "US-ASCII");
- q++;
- }
- if (isToBase64) {
- Byte[] byteR = bytesR.toArray(new Byte[bytesR.size()]);
- byte[] bs = new byte[byteR.length];
- for (int i = 0; i < byteR.length; i++) {
- bs[i] = byteR[i].byteValue();
- }
- result = Base64.encodeToString(bs, Base64.DEFAULT);
- }
- return result;
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- }
- /**
- * 获取到优酷链接的sid token 新的ep
- *
- * @param vid
- * @param ep
- * 旧的ep
- * @return rs[0] = sid; rs[1] = token; rs[2] = epNew;
- */
- public static String[] getValues(String vid, String ep) {
- try {
- String template1 = "becaf9be";
- String template2 = "bf7e5f01";
- byte[] bytes = Base64.decode(ep, Base64.DEFAULT);
- ep = new String(bytes, "US-ASCII");
- String temp = myEncoder(template1, bytes, false);
- String[] part = temp.split("_");
- String sid = part[0];
- String token = part[1];
- String whole = sid + "_" + vid + "_" + token;
- byte[] newbytes = whole.getBytes("US-ASCII");
- String epNew = myEncoder(template2, newbytes, true);
- epNew = URLEncoder.encode(epNew);
- String[] rs = new String[3];
- rs[0] = sid;
- rs[1] = token;
- rs[2] = epNew;
- return rs;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
计算得到ep,token,sid分别为cCaVGE6OUc8H4ircjj8bMiuwdH8KXJZ0vESH/7YbAMZuNaHQmjbTwg==, 3825, 241273717793612e7b085。注意,此时ep并不能直接拼接到url中,需要对此做一下url编码ToUrlEncode(ep)。最终ep为cCaVGE6OUc8H4ircjj8bMiuwdH8KXJZ0vESH%2f7YbAMZuNaHQmjbTwg%3d%3d
3.2视频格式及清晰度
视频格式和选择的segs有密切关系。如本文选择的hd2,格式即为flv,下面是segs,视频格式和清晰度的对照。之前对此部分理解有些偏差,多谢削着苹果走路提醒。
- “segs”,”视频格式”,”清晰度”
- "hd3", "flv", "1080P"
- "hd2", "flv", "超清"
- "mp4", "mp4", "高清"
- "flvhd", "flv", "高清"
- "flv", "flv", "标清"
- "3gphd", "3gp", "高清"
3.3拼接地址
最后的m3u8地址为:
http://pl.youku.com/playlist/m3u8?
ctype=12&ep=cCaVGE6OUc8H4ircjj8bMiuwdH8KXJZ0vESH%2f7YbAMZuNaHQmjbTwg%3d%3d&ev=1
&keyframe=1&oip=996949050&sid=241273717793612e7b085&token=3825&type=hd2&vid=XNzk2NTI0MzMy
4:获取视频地址
将上述m3u8文件下载后,其中内容即为真实地址,不过还需要稍微处理一下。部分内容如下:
- #EXTM3U
- #EXT-X-TARGETDURATION:12
- #EXT-X-VERSION:3
- #EXTINF:6.006,
- http://59.108.137.14/65666E0ED34581E6B96293A18/0300010F005430BCBA49631468DEFEC61C5678-3A78-37BA-1971-21A0D4EEA0E7.flv?ts_start=0&ts_end=5.906&ts_seg_no=0&ts_keyframe=1
- #EXTINF:5.464,
- http://59.108.137.14/65666E0ED34581E6B96293A18/0300010F005430BCBA49631468DEFEC61C5678-3A78-37BA-1971-21A0D4EEA0E7.flv?ts_start=5.906&ts_end=11.37&ts_seg_no=1&ts_keyframe=1
- #EXTINF:5.505,
- http://59.108.137.14/65666E0ED34581E6B96293A18/0300010F005430BCBA49631468DEFEC61C5678-3A78-37BA-1971-21A0D4EEA0E7.flv?ts_start=11.37&ts_end=16.875&ts_seg_no=2&ts_keyframe=1
- #EXTINF:9.26,
- http://59.108.137.14/65666E0ED34581E6B96293A18/0300010F005430BCBA49631468DEFEC61C5678-3A78-37BA-1971-21A0D4EEA0E7.flv?ts_start=16.875&ts_end=26.135&ts_seg_no=3&ts_keyframe=1
- #EXTINF:11.136,
- http://59.108.137.14/65666E0ED34581E6B96293A18/0300010F005430BCBA49631468DEFEC61C5678-3A78-37BA-1971-21A0D4EEA0E7.flv?ts_start=26.135&ts_end=37.271&ts_seg_no=4&ts_keyframe=1
- #EXTINF:8.258,
- http://59.108.137.14/65666E0ED34581E6B96293A18/0300010F005430BCBA49631468DEFEC61C5678-3A78-37BA-1971-21A0D4EEA0E7.flv?ts_start=37.271&ts_end=45.529&ts_seg_no=5&ts_keyframe=1
- #EXTINF:9.843,
- http://59.108.137.14/65666E0ED34581E6B96293A18/0300010F005430BCBA49631468DEFEC61C5678-3A78-37BA-1971-21A0D4EEA0E7.flv?ts_start=45.529&ts_end=55.372&ts_seg_no=6&ts_keyframe=1
- #EXTINF:10.26,
- http://59.108.137.14/65666E0ED34581E6B96293A18/0300010F005430BCBA49631468DEFEC61C5678-3A78-37BA-1971-21A0D4EEA0E7.flv?ts_start=55.372&ts_end=65.632&ts_seg_no=7&ts_keyframe=1
其中每条url只包含6s左右视频,但是可将url中参数部分去掉即可得到实际的长度。但是每条去掉后需合并一下相同的url,如上述列表可得到url片段
将m3u8中所有的url片段全部下载即可大功告成。
原文地址:http://www.cnblogs.com/zhaojunjie/p/4009192.html