声明:代码仅供学习交流 不做其他用途
一般python和Js的破解比较常见 Java版本由于需要 自己做了下 ,本篇优酷破解参考github上优酷python版本破解
常用的获取网页源码,及网络请求部分代码略过 本文主要写视频解析的基本流程 如果后续需要 会补充上来
常用的视频解析 一般分为三个步骤 抓包-----逻辑分析-----代码编写,由于优酷参照优酷视频js源码,所以抓包过程暂且省略 (注:如果有需要抓包的过程 小编个人建议,移动端和H5页面的包比较好抓(方法为给手机端添加代理),网页端的对比起来比较麻烦,抓包工具的话 推荐使用 fiddler)
好了 废话到此结束 以下是代码内容:
public class CrackYouKu {
//此数组用作视频类型 常见视频格式有MP4 flv hd等 本文只获取了这三种格式
ArrayList
public CrackYouKu(String url) {
super(url);
}
//A站视频解析将会用到的,优酷开放视频平台
boolean isOpen;
public CrackYouKu(String url, String vid, boolean isOpen) {
super(url);
this.vid = vid;
this.isOpen = isOpen;
}
boolean flag = false;
String vid;
String ep;
// 模拟请求
private HashMap
String cookieUrl = "http://p.l.youku.com/ypvlog";
String referHeader = "http://c-h5.youku.com/";
Map
Pattern pattern = Pattern.compile("(.*?;)\\s*?");
StringBuilder builder = new StringBuilder();
java.util.List
for (String value : cookies) {
Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
builder.append(matcher.group());
}
}
HashMap
requestHeaders.put("Referer", referHeader);
requestHeaders.put("Cookie", builder.toString());
return requestHeaders;
}
public String parseVideoUrl() {
String ret = "";
if (StringUtils.isNotEmpty(vid)) {
return getPlayUrlByVid();
}
if (StringUtils.isEmpty(url))
return ret;
try {
String html = Utils.GetSource(url);
Matcher mat = Pattern.compile("videoId = '.*?';").matcher(html);
Matcher mat1 = Pattern.compile("(?<=id_).*?==").matcher(url);
if (mat.find()) {
String id = mat.group(0);
vid = id.substring(id.indexOf("'") + 1, id.lastIndexOf("'"));
String result = getPlayUrlByVid();
return result;
}
} catch (Exception ex) {
logger.error(ex.toString(), ex);
}
return FAIL;
}
private String getPlayUrlByVid() {
String url = getPlayInfoUrlByVid();
if (!url.equals(FAIL)) {
for (int m = 0; m < this.type.size(); m++) {
String ty = this.type.get(m);
if (ty.equals("flvhd")) {
stand = url + "&type=flvhd";
} else if (ty.equals("mp4hd")) {
high = url + "&type=mp4";
} else if (ty.contains("hd")) {
sup = url + "&type=hd2";
}
}
System.out.println(stand);
System.out.println(high);
System.out.println(sup);
return SUCCESS;
}
return FAIL;
}
private String getPlayInfoUrlByVid() {
String html = getParamsByVid();
if (!html.equals(FAIL)) {
String f_code_1;
String f_code_2;
String url;
if (isOpen) {
f_code_1 = "10ehfkbv";
f_code_2 = "msjv7h2b";
url = "http://pl.youku.com/partner/m3u8?ctype=85&ev=1&keyframe=1";
} else {
f_code_1 = "becaf9be";
f_code_2 = "bf7e5f01";
url = "http://pl.youku.com/playlist/m3u8?ctype=12&ev=1&keyframe=1";
}
try {
JSONObject info = new JSONObject(html).optJSONObject("data");
for (int i = 0; i < info.optJSONArray("stream").length(); i++) {
this.type.add(info.optJSONArray("stream").getJSONObject(i).getString("stream_type"));
}
String oip = info.optJSONObject("security").getString("ip");
String vid = info.optJSONObject("video").getString("encodeid");
ep = info.optJSONObject("security").getString("encrypt_string");
String temp = key(f_code_1, ep);
flag = true;
String[] temp_slip = temp.split("_");
String sid = temp_slip[0];
String token = temp_slip[1];
ep = URLEncoder.encode(com.crack.Base64.encodeToString(key(f_code_2, sid + "_" + vid + "_" + token).getBytes("ISO-8859-1"), 0));
ep = ep.substring(0, ep.length() - 3);
url = url + "&ep=" + ep + "&oip=" + oip + "&sid=" + sid + "&token=" + token + "&vid=" + vid;
return url;
} catch (Exception ex) {
logger.error(ex.toString());
}
}
return FAIL;
}
private String getParamsByVid() {
String html;
try {
if (isOpen) {
String api = "https://api.youku.com/players/custom.json?type=h5&client_id=908a519d032263f8&video_id="
+ vid + "&embsig=null&refer=";
html = Utils.GetSource(api);
String sign = new JSONObject(html).getString("playsign");
String newUrl = "http://play.youku.com/partner/get.json?vid=" + vid
+ "&ct=86&cid=908a519d032263f8&sign=" + sign + "&callback=ykv.callbacks.cb_JhaGv4G";
html = Utils.GetSource(newUrl);
html = html.substring(html.indexOf("{"));
} else {
String newUrl = "http://play.youku.com/play/get.json?ct=12&vid=" + vid;
html = Utils.GetSource(newUrl, cookie());
}
return html;
} catch (Exception ex) {
logger.error(ex.toString(), ex);
}
return FAIL;
}
private String key(String a, String c) {
char[] chr = new char[a.length()];
chr = a.toCharArray();
int f, h, temp, c_length;
temp = f = h = 0;
int[] d = new int[100];
String e = "";
int b[] = new int[256];
for (int i = 0; i < 256; i++) {
b[i] = i;
}
for (int i = 0; i < 256; i++) {
f = ((f + b[i]) + (int) chr[i % a.length()]) % 256;
temp = b[i];
b[i] = b[f];
b[f] = temp;
}
f = h = 0;
if (!flag) {
byte[] ep_decode = Base64.decode(ep);
c_length = ep_decode.length;
for (int i = 0; i < c_length; i++) {
String hex = Integer.toHexString(ep_decode[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
d[i] = Integer.parseInt(hex, 16);
}
} else {
c_length = c.length();
char[] ch = new char[c_length];
ch = c.toCharArray();
for (int i = 0; i < c_length; i++) {
d[i] = ch[i];
}
}
for (int j = 0; j < c_length; j++) {
h = (h + 1) % 256;
f = (f + b[h]) % 256;
temp = b[h];
b[h] = b[f];
b[f] = temp;
e = e + (char) (d[j] ^ (b[(b[h] + b[f]) % 256]));
}
return e;
}
public static void main(String[] args) {
CrackYouKu crack = new CrackYouKu("http://v.youku.com/v_show/id_XMTU1NjkxMjA2NA==.html?from=y1.3-tv-grid-1007-9910.86869-86852.1-1");
crack.crackVideo();
System.out.println("result:" + crack.getResult());
}
}
注: 时间问题 未添加太多注释 如有不懂之处 可以留言询问
7-10天后会整理相关代码 到github上,届时 会有相关链接