java抖音视频解析(去水印)
程序员文章站
2022-04-11 15:49:08
...
博主第二篇文章,转载请说明出处,有问题请提出来,和博主一起讨论(抖音分享地址更改)
1.真正解析水印并不是解析(理解误区)
2.拿到视频地址后解析出原始地址(有软件可以做到模糊水印,但是只能模糊,真真去掉是
有些许困难的,需要对视频一秒一秒取帧处理,博主实力有限,做不到)
博主喜欢用代码来表示自己所说的,废话不多说,附代码,大家观摩
package com.test.douyin;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
/**
* tips:导入jsoup包
*
* @author hedong
*/
@SuppressWarnings("all")
public class DYDecode {
public static void main(String[] args) {
String url = "#在抖音,记录美好生活#这可能是@小甜芯最想让你看到的一条抖音了!http://v.douyin.com/7N577/";// 解析的网址,长短链接都可以
String playAddr = getURI(NewUrlDecode(urlAnalysisMethod(url))).trim();// 有空白符
String cover = getCover(urlAnalysisMethod(url)).trim();// 有空白符
DecodeVideo dv = new DecodeVideo();
dv.setPlayAddr(playAddr);
dv.setCover(cover);
System.out.println(dv);
/*
* tips:最后得到的结果 DecodeVideo
* [cover=https://p3.pstatp.com/large/86920013ac9cb67fe09a.jpg,
* playAddr=http://v3-dy-z.ixigua.com/d153fa2e0d25730c9a6ffa949e228598/5b2b4b7e/
* video/m/2203826b87d19034f608c53f014a2f5ece01157576f0000077573318095/]
*/ }
public static String urlAnalysisMethod(String url) {
try {
if (isContainChinese(url)) {
url = cuthttpschinese(url);
}
if (url.length() < 40) {
url = getURI(url);
}
return url;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String NewUrlDecode(String url) {
Document doc = null;
try {
doc = Jsoup.connect(url).timeout(5000).get();
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println(doc.data());
Elements elem = doc.getElementsByTag("script");
String url1 = elem.toString();
int start = url1.indexOf("playAddr");
url1 = url1.substring(start);
int end = url1.indexOf("\",");
return url1.substring(11, end).replaceAll("playwm", "play");
}
// 获取封面,项目要用到
public static String getCover(String url) {
Document doc = null;
try {
doc = Jsoup.connect(url).timeout(5000).get();
} catch (Exception e) {
e.printStackTrace();
}
Elements elem = doc.getElementsByTag("script");
String url1 = elem.toString();
int start = url1.indexOf("cover");
String str = url1.substring(start);
int end = str.indexOf("}");
return str.substring(8, end).replaceAll("\"", "");
}
/**
* @param url
* @return
*/
// 获取真实地址
public static String getURI(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
try {
// 将HttpContext对象作为参数传给execute()方法,则HttpClient会把请求响应交互过程中的状态信息存储在HttpContext中
HttpResponse response = httpClient.execute(httpGet, httpContext);
// 获取重定向之后的主机地址信息
HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
// 获取实际的请求对象的URI,即重定向之后的地址
HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
return (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST)
+ ((HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST)).getURI().toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
httpClient.getConnectionManager().shutdown();
}
}
/**
* @param str
* @return
*/
// 检查是否有中文
public static boolean isContainChinese(String str) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}
/**
*
* @param str
* @return
*/
// 截取到http开始的字段
public static String cuthttpschinese(String str) {
int start = str.indexOf("http");
return str.substring(start);
}
}
package com.test.viodedecode;
/**
* @author hedong
*/
public class DecodeVideo {
private String cover;
private String playAddr;
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getPlayAddr() {
return playAddr;
}
public void setPlayAddr(String playAddr) {
this.playAddr = playAddr;
}
@Override
public String toString() {
return "DecodeVideo [cover=" + cover + ", playAddr=" + playAddr + "]";
}
}
谢谢观看,如有问题,联系博主哦
上一篇: 抖音视频去水印
下一篇: IOS中plist的读写操作