如题,具体分析如何通过视频分享的短链接获取无水印视频链接网上有太多的方法了,本文就不过多的叙述【可私聊交流】,直接上干货。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class douyinUtils {
public static String DOU_YIN_BASE_URL = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";
public static String getLocation(String url) {
try {
URL serverUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();
conn.setRequestMethod("GET");
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("User-agent", "ua");//模拟手机连接
conn.connect();
conn.setConnectTimeout(60000);
String location = conn.getHeaderField("Location");
return location;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String matchNo(String redirectUrl) {
List<String> results = new ArrayList<>();
Pattern p = Pattern.compile("video/([\\w/\\.]*)/");
Matcher m = p.matcher(redirectUrl);
while (!m.hitEnd() && m.find()) {
results.add(m.group(1));
}
return results.get(0);
}
public static String hSMatchNo(String redirectUrl) {
List<String> results = new ArrayList<>();
Pattern p = Pattern.compile("item_id=([\\w/\\.]*)&");
Matcher m = p.matcher(redirectUrl);
while (!m.hitEnd() && m.find()) {
results.add(m.group(1));
}
return results.get(0);
}
public static String httpGet2(String urlStr) throws Exception {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "text/json;charset=utf-8");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuffer buf = new StringBuffer();
String inputLine = in.readLine();
while (inputLine != null) {
buf.append(inputLine).append("\r\n");
inputLine = in.readLine();
}
in.close();
return buf.toString();
}
/**
* 使用Get方式获取数据
*
* @param url URL包括参数,http://HOST/XX?XX=XX&XXX=XXX
* @return
*/
public static String httpGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
public static String parseUrl(String url) {
String host = "";
Pattern p = Pattern.compile("http[:|/|\\w|\\.]+");
Matcher matcher = p.matcher(url);
if (matcher.find()) {
host = matcher.group();
}
return host.trim();
}
/**
* 查找域名(以 https开头 com结尾)
*
* @param url
* @return
*/
public static String getDomainName(String url) {
String host = "";
Pattern p = Pattern.compile("https://.*\\.com");
Matcher matcher = p.matcher(url);
if (matcher.find()) {
host = matcher.group();
}
return host.trim();
}
public static void main(String[] args) {
String redirectUrl = getLocation("https://v.douyin.com/2Yq8gaL/");
System.out.println(redirectUrl);
String itemId = matchNo(redirectUrl);
System.out.println(itemId);
StringBuilder sb = new StringBuilder();
sb.append(DOU_YIN_BASE_URL).append(itemId);
String videoResult = httpGet(sb.toString());
System.out.println(videoResult);
//无水印视频 url
String videoUrl = dyResult.getItem_list().get(0)
.getVideo().getPlay_addr().getUrl_list().get(0)
.replace("playwm", "play");
String videoRedirectUrl = getLocation(videoUrl);
// 循环加入无水印图片
if (dyResult.getItem_list().get(0).getImages() != null) {
List<String> imgList = new ArrayList<>();
for (com.media.domain.dyResult.Images img : dyResult.getItem_list().get(0).getImages()) {
imgList.add(img.getUrl_list().get(0));
}
resData.setImgList(imgList);
}
}
}
此外,还找到一款去免费水印小程序【光头去水印工具】,无需积分、注册、费用、无广告,完全免费,需要白嫖的老铁赶快白嫖,全面支持绝大多数视频app平台,如斗音、西关、霍山、筷手、皮皮瞎等
本文只作为学习使用。