一、前言
抖音去水印方法很簡單,以前一直沒有去研究,以為搞個去水印還要用到演算法去除,直到動手的時候才發現這麼簡單,不用程式設計基礎都能做。
二、原理與步驟
其實抖音它是有一個隱藏無水印地址的,只要我們找到那個地址就可以了
1、我們在抖音找一個想要去水印的視訊連結
9.23 mQK:/ 這輩子總要和你最愛的人來看一次洱海吧%洱海 %治癒系風景 https://v.douyin.com/NAmJfAJ/ 複製此連結,開啟Dou音搜尋,直接觀看視訊!
注意:這裡一定要是https開頭的,不是口令
開啟瀏覽器訪問:
https://v.douyin.com/NAmJfAJ/
訪問之後會重定向到這個地址,後面有一串數字,這個就是視訊的id,他是根據這個唯一id來找到視訊播放的
按F12檢視網路請求,找到剛剛複製的那個請求地址,在響應頭裡有一個location連結,訪問location的連結
https://www.iesdouyin.com/share/video/7064781119429807363/
在F12中有許多請求,檢視眾多的請求裡有一個請求是:
請求太多沒找到可以直接跳過,直接看:https://aweme.snssdk.com 這個就行了,把id替換一下
https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=7064781119429807363
把這個請求再次用瀏覽器訪問,然後返回了一大串json資料,一直放下翻可以找到這個連結
https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200fg10000c85i9ejc77ue0kb2vo80&ratio=720p&line=0
直接用那個連結訪問,他其實是一個有水印的連結,仔細觀察發現最後那裡有一段/playwm
,有兩個字母wm其實就是watermark英語單詞的縮寫,去掉wm後就能得到一個無水印連結了
https://aweme.snssdk.com/aweme/v1/play/?video_id=v0200fg10000c85i9ejc77ue0kb2vo80&ratio=720p&line=0
到這裡無水印已經完成了
三、程式碼實現
這裡我用的是Java去實現,這個跟語言無關,只要能發請求就行
/**
* 下載抖音無水印視訊
*
* @throws IOException
*/
@GetMapping(value = "/downloadDy")
public void downloadDy(String dyUrl, HttpServletResponse response) throws IOException {
ResultDto resultDto = new ResultDto();
try {
dyUrl = URLDecoder.decode(dyUrl).replace("dyUrl=", "");
resultDto = dyParseUrl(dyUrl);
} catch (Exception e) {
e.printStackTrace();
}
if (resultDto.getVideoUrl().contains("http://")) {
resultDto.setVideoUrl(resultDto.getVideoUrl().replace("http://", "https://"));
}
String videoUrl = resultDto.getVideoUrl();
response.sendRedirect(videoUrl);
}
public ResultDto dyParseUrl(String redirectUrl) throws Exception {
redirectUrl = CommonUtils.getLocation(redirectUrl);
ResultDto dyDto = new ResultDto();
if (!StringUtils.isEmpty(redirectUrl)) {
/**
* 1、用 ItemId 拿視訊的詳細資訊,包括無水印視訊url
*/
String itemId = CommonUtils.matchNo(redirectUrl);
StringBuilder sb = new StringBuilder();
sb.append(CommonUtils.DOU_YIN_BASE_URL).append(itemId);
String videoResult = CommonUtils.httpGet(sb.toString());
DYResult dyResult = JSON.parseObject(videoResult, DYResult.class);
/**
* 2、無水印視訊 url
*/
String videoUrl = dyResult.getItem_list().get(0)
.getVideo().getPlay_addr().getUrl_list().get(0)
.replace("playwm", "play");
String videoRedirectUrl = CommonUtils.getLocation(videoUrl);
dyDto.setVideoUrl(videoRedirectUrl);
/**
* 3、音訊 url
*/
String musicUrl = dyResult.getItem_list().get(0).getMusic().getPlay_url().getUri();
dyDto.setMusicUrl(musicUrl);
/**
* 4、封面
*/
String videoPic = dyResult.getItem_list().get(0).getVideo().getDynamic_cover().getUrl_list().get(0);
dyDto.setVideoPic(videoPic);
/**
* 5、視訊文案
*/
String desc = dyResult.getItem_list().get(0).getDesc();
dyDto.setDesc(desc);
}
return dyDto;
}
ResultDto.java
public class ResultDto {
private String videoUrl; //視訊
private String musicUrl; //背景音樂
private String videoPic; //無聲視訊
private String desc;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
public String getMusicUrl() {
return musicUrl;
}
public void setMusicUrl(String musicUrl) {
this.musicUrl = musicUrl;
}
public String getVideoPic() {
return videoPic;
}
public void setVideoPic(String videoPic) {
this.videoPic = videoPic;
}
}
CommonUtils .java
public class CommonUtils {
public static String DOU_YIN_BASE_URL = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";
public static String HUO_SHAN_BASE_URL = " https://share.huoshan.com/api/item/info?item_id=";
public static String DOU_YIN_DOMAIN = "douyin";
public static String HUO_SHAN_DOMAIN = "huoshan";
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();
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();
}
}
四、總結
其實看那個第二部分原理就行了是不是很簡單?
我自己有在微信小程式整合了,大家可以體驗一下
開啟微信小程式搜尋:日常多功能工具箱
大家有遇到什麼問題可以在下面留言,或者私信都可