抖音视频去水印
程序员文章站
2022-04-11 15:49:14
...
免费快捷地址:抖音视频去水印
Java代码:
package com.manage.util;
import org.jsoup.Jsoup;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* DouYinQushuiyinUtil 抖音去水印
*
* @author <a href="[email protected]">JiangWen</a>
* @version 1.0.0, 2019/10/28 0028 13:56
*/
public class DouYinQushuiyinUtil {
private static final Pattern patternCompile = Pattern.compile("(?<=playAddr: \")https?://.+(?=\",)");
/**
* 去水印
*
* @param url
*
* @return
*
* @throws Exception
*/
public static byte[] qushuiyinToByte(String url) throws Exception {
return toByteArray(qushuiyinToStream(url));
}
public static InputStream qushuiyinToStream(String url) throws Exception {
// 过滤链接,获取http连接地址
String finalUrl = decodeHttpUrl(url);
// 利用Jsoup抓取抖音链接
// 抓取抖音网页
String htmls = Jsoup.connect(finalUrl).ignoreContentType(true).execute().body();
Matcher m = patternCompile.matcher(htmls);
// 利用Matcher中的group方法获取匹配的特定字符串
// 利用String的replace方法替换特定字符,得到抖音的去水印链接
String matchUrl = "";
while (m.find()) {
matchUrl = m.group(0).replaceAll("playwm", "play");
}
// 将链接封装成流
// 注:由于抖音对请求头有限制,只能设置一个伪装手机浏览器请求头才可实现去水印下载
Map<String, String> headers = new HashMap<>();
headers.put("Connection", "keep-alive");
headers.put("Host", "aweme.snssdk.com");
headers.put(
"User-Agent",
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1"
);
// 利用Joup获取视频对象,并作封装成一个输入流对象(最大50m)
BufferedInputStream in = Jsoup.connect(matchUrl).headers(headers).timeout(10000).ignoreContentType(true).maxBodySize(50*1024*1024).execute()
.bodyStream();
return in;
}
private static String decodeHttpUrl(String url) {
Matcher matcher = Patterns.WEB_URL.matcher(url);
if (matcher.find()) {
return matcher.group();
}
return "";
}
private static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
}
上一篇: app项目选php还是java
下一篇: java抖音视频解析(去水印)