话不多说,上代码,注意这是JAVA语言;
package com.prms.douyin; import java.io.IOException; 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.methods.HttpGet; import
org.apache.http.client.methods.HttpUriRequest; import
org.apache.http.impl.client.CloseableHttpClient; import
org.apache.http.impl.client.HttpClientBuilder; import
org.apache.http.protocol.BasicHttpContext; import
org.apache.http.protocol.HttpContext; import
org.apache.http.protocol.HttpCoreContext; import org.jsoup.Jsoup; import
org.jsoup.nodes.Document; import org.jsoup.select.Elements; import lombok.Data;
@SuppressWarnings("all") public class DYDecodeUtils { public static void
main(String[] args) { // 解析的网址,长短链接都可以 String url =
"#在抖音,记录美好生活#回家相亲喽,不出意外很快就结婚了,这就是所谓的搭伙过日子吗?呵呵呵呵…… http://v.douyin.com/NbTt5U/
复制此链接,打开【抖音短视频】,直接观看视频!"; try { DecodeVideo decodeVideo = decode(url);
System.out.println(decodeVideo); } catch (IOException e) { e.printStackTrace();
} } /** * * 获取抖音真实地址,无水印地址; * * @param url 抖音视频链接地址 * @return DecodeVideo */
public static DecodeVideo decode(String url) throws IOException { Matcher
matcher = Pattern.compile("[http|https]+[://]+[0-9A-Za-z:/[-]_#[?][=][.][&]]*",
Pattern.CASE_INSENSITIVE).matcher(url); if (matcher.find()){ url =
matcher.group(); } String playAddr =
getURI(NewUrlDecode(urlAnalysisMethod(url))).trim();// 有空白符 String cover =
getCover(urlAnalysisMethod(url)).trim();// 有空白符 DecodeVideo decodeVideo =
DecodeVideo.build(cover, playAddr); return decodeVideo; } 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("\"", ""); } //
获取真实地址 public static String getURI(String url) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
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(HttpCoreContext.HTTP_TARGET_HOST); //
获取实际的请求对象的URI,即重定向之后的地址 HttpUriRequest realRequest = (HttpUriRequest)
httpContext.getAttribute(HttpCoreContext.HTTP_REQUEST); return (HttpHost)
httpContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST) + ((HttpUriRequest)
httpContext.getAttribute(HttpCoreContext.HTTP_REQUEST)).getURI().toString(); }
catch (Exception e) { e.printStackTrace(); return null; } finally {
httpClient.close(); } } // 检查是否有中文 public static boolean
isContainChinese(String str) { Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str); if (m.find()) { return true; } return false; } //
截取到http开始的字段 public static String cuthttpschinese(String str) { int start =
str.indexOf("http"); return str.substring(start); } } @Data class DecodeVideo {
// 封面图 private String cover; // 播放地址 private String playAddr; public static
DecodeVideo build(String cover, String playAddr) { DecodeVideo decodeVideo =
new DecodeVideo(); decodeVideo.setPlayAddr(playAddr);
decodeVideo.setCover(cover); return decodeVideo; } }
 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信