Java實現(xiàn)超簡單抖音去水印的示例詳解
一、前言
抖音去水印方法很簡單,以前一直沒有去研究,以為搞個去水印還要用到算法去除,直到動手的時候才發(fā)現(xiàn)這么簡單,不用編程基礎(chǔ)都能做。
二、原理與步驟
其實抖音它是有一個隱藏?zé)o水印地址的,只要我們找到那個地址就可以了
1、我們在抖音找一個想要去水印的視頻鏈接
注意:這里一定要是https開頭的,不是口令
打開瀏覽器訪問:
訪問之后會重定向到這個地址,后面有一串?dāng)?shù)字,這個就是視頻的id,他是根據(jù)這個唯一id來找到視頻播放的
按F12查看網(wǎng)絡(luò)請求,找到剛剛復(fù)制的那個請求地址,在響應(yīng)頭里有一個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數(shù)據(jù),一直放下翻可以找到這個鏈接
https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200fg10000c85i9ejc77ue0kb2vo80&ratio=720p&line=0
直接用那個鏈接訪問,他其實是一個有水印的鏈接,仔細(xì)觀察發(fā)現(xiàn)最后那里有一段/playwm
,有兩個字母wm其實就是watermark英語單詞的縮寫,去掉wm后就能得到一個無水印鏈接了
https://aweme.snssdk.com/aweme/v1/play/?video_id=v0200fg10000c85i9ejc77ue0kb2vo80&ratio=720p&line=0
到這里無水印已經(jīng)完成了
三、代碼實現(xiàn)
這里我用的是Java去實現(xiàn),這個跟語言無關(guān),只要能發(fā)請求就行
/** * 下載抖音無水印視頻 * * @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 拿視頻的詳細(xì)信息,包括無水印視頻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");//模擬手機(jī)連接 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方式獲取數(shù)據(jù) * * @param url URL包括參數(shù),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(); // 設(shè)置通用的請求屬性 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的響應(yīng) in = new BufferedReader(new InputStreamReader( connection.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發(fā)送GET請求出現(xiàn)異常!" + e); e.printStackTrace(); } // 使用finally塊來關(guān)閉輸入流 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結(jié)尾) * * @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(); } }
四、總結(jié)
其實看那個第二部分原理就行了是不是很簡單?
到此這篇關(guān)于Java實現(xiàn)超簡單抖音去水印的示例詳解的文章就介紹到這了,更多相關(guān)Java抖音去水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合HikariCP數(shù)據(jù)庫連接池方式
這篇文章主要介紹了SpringBoot整合HikariCP數(shù)據(jù)庫連接池方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03SpringCloud Feign配置應(yīng)用詳細(xì)介紹
這篇文章主要介紹了SpringCloud Feign配置應(yīng)用,feign是netflix提供的服務(wù)間基于http的rpc調(diào)用框架,在spring cloud得到廣泛應(yīng)用2022-09-09Spring?BeanDefinition父子關(guān)系示例解析
這篇文章主要為大家介紹了Spring?BeanDefinition父子關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Java網(wǎng)絡(luò)編程之簡易聊天室的實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實現(xiàn)一個簡易聊天室功能,可以實現(xiàn)運行客戶端和連接服務(wù)器,文中的示例代碼講解詳細(xì),需要的可以了解一下2022-10-10