Java怎么实现抖音去水印
一、前言
抖音去水印方法很简单,以前一直没有去研究,以为搞个去水印还要用到算法去除,直到动手的时候才发现这么简单,不用编程基础都能做。
二、原理与步骤
其实抖音它是有一个隐藏无水印地址的,只要我们找到那个地址就可以了
1、我们在抖音找一个想要去水印的视频链接
注意:这里一定要是https开头的,不是口令
打开浏览器访问:
访问之后会重定向到这个地址,后面有一串数字,这个就是视频的id,他是根据这个唯一id来找到视频播放的
按F12查看网络请求,找到刚刚复制的那个请求地址,在响应头里有一个location链接,访问location的链接
www.iesdouyin./share/video/7064781119429807363/
在F12中有许多请求,查看众多的请求里有一个请求是:
请求太多没找到可以直接跳过,直接看:aweme.snssdk. 这个就行了,把id替换一下
www.iesdouyin./web/api/v2/aweme/iteminfo/?item_ids=7064781119429807363
把这个请求再次用浏览器访问,然后返回了一大串json数据,一直放下翻可以找到这个链接
aweme.snssdk./aweme/v1/playwm/?video_id=v0200fg10000c85i9ejc77ue0kb2vo80&ratio=720p&line=0
直接用那个链接访问,他其实是一个有水印的链接,仔细观察发现最后那里有一段/playwm,有两个字母wm其实就是watermark英语单词的缩写,去掉wm后就能得到一个无水印链接了
aweme.snssdk./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("")) { resultDto.setVideoUrl(resultDto.getVideoUrl().replace("", "")); } 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 = "www.iesdouyin./web/api/v2/aweme/iteminfo/?item_ids="; public static String HUO_SHAN_BASE_URL = " share.huoshan./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.pile("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.pile("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包括参数,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 (patible; 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.pile("http[:|/|\\w|\\.]+"); Matcher matcher = p.matcher(url); if (matcher.find()) {host = matcher.group(); } return host.trim(); } /** * 查找域名(以 https开头 结尾) * * @param url * @return */ public static String getDomainName(String url) { String host = ""; Pattern p = Pattern.pile(".*\\."); Matcher matcher = p.matcher(url); if (matcher.find()) {host = matcher.group(); } return host.trim(); }}
登录后复制
以上就是Java怎么实现抖音去水印的详细内容,更多请关注范的资源库其它相关文章!
<