Java怎么实现抖音去水印
Java怎么实现抖音去水印
本篇内容主要讲解“Java怎么实现抖音去水印”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java怎么实现抖音去水印”吧!
一、前言
抖音去水印方法很简单,以前一直没有去研究,以为搞个去水印还要用到算法去除,直到动手的时候才发现这么简单,不用编程基础都能做。
二、原理与步骤
其实抖音它是有一个隐藏无水印地址的,只要我们找到那个地址就可以了
1、我们在抖音找一个想要去水印的视频链接
注意:这里一定要是https开头的,不是口令
打开浏览器访问:
访问之后会重定向到这个地址,后面有一串数字,这个就是视频的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去实现,这个跟语言无关,只要能发请求就行
/***下载抖音无水印视频**@throwsIOException*/@GetMapping(value="/downloadDy")publicvoiddownloadDy(StringdyUrl,HttpServletResponseresponse)throwsIOException{ResultDtoresultDto=newResultDto();try{dyUrl=URLDecoder.decode(dyUrl).replace("dyUrl=","");resultDto=dyParseUrl(dyUrl);}catch(Exceptione){e.printStackTrace();}if(resultDto.getVideoUrl().contains("http://")){resultDto.setVideoUrl(resultDto.getVideoUrl().replace("http://","https://"));}StringvideoUrl=resultDto.getVideoUrl();response.sendRedirect(videoUrl);}publicResultDtodyParseUrl(StringredirectUrl)throwsException{redirectUrl=CommonUtils.getLocation(redirectUrl);ResultDtodyDto=newResultDto();if(!StringUtils.isEmpty(redirectUrl)){/***1、用ItemId拿视频的详细信息,包括无水印视频url*/StringitemId=CommonUtils.matchNo(redirectUrl);StringBuildersb=newStringBuilder();sb.append(CommonUtils.DOU_YIN_BASE_URL).append(itemId);StringvideoResult=CommonUtils.httpGet(sb.toString());DYResultdyResult=JSON.parseObject(videoResult,DYResult.class);/***2、无水印视频url*/StringvideoUrl=dyResult.getItem_list().get(0).getVideo().getPlay_addr().getUrl_list().get(0).replace("playwm","play");StringvideoRedirectUrl=CommonUtils.getLocation(videoUrl);dyDto.setVideoUrl(videoRedirectUrl);/***3、音频url*/StringmusicUrl=dyResult.getItem_list().get(0).getMusic().getPlay_url().getUri();dyDto.setMusicUrl(musicUrl);/***4、封面*/StringvideoPic=dyResult.getItem_list().get(0).getVideo().getDynamic_cover().getUrl_list().get(0);dyDto.setVideoPic(videoPic);/***5、视频文案*/Stringdesc=dyResult.getItem_list().get(0).getDesc();dyDto.setDesc(desc);}returndyDto;}
ResultDto.java
publicclassResultDto{privateStringvideoUrl;//视频privateStringmusicUrl;//背景音乐privateStringvideoPic;//无声视频privateStringdesc;publicStringgetDesc(){returndesc;}publicvoidsetDesc(Stringdesc){this.desc=desc;}publicStringgetVideoUrl(){returnvideoUrl;}publicvoidsetVideoUrl(StringvideoUrl){this.videoUrl=videoUrl;}publicStringgetMusicUrl(){returnmusicUrl;}publicvoidsetMusicUrl(StringmusicUrl){this.musicUrl=musicUrl;}publicStringgetVideoPic(){returnvideoPic;}publicvoidsetVideoPic(StringvideoPic){this.videoPic=videoPic;}}
CommonUtils .java
publicclassCommonUtils{publicstaticStringDOU_YIN_BASE_URL="https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";publicstaticStringHUO_SHAN_BASE_URL="https://share.huoshan.com/api/item/info?item_id=";publicstaticStringDOU_YIN_DOMAIN="douyin";publicstaticStringHUO_SHAN_DOMAIN="huoshan";publicstaticStringgetLocation(Stringurl){try{URLserverUrl=newURL(url);HttpURLConnectionconn=(HttpURLConnection)serverUrl.openConnection();conn.setRequestMethod("GET");conn.setInstanceFollowRedirects(false);conn.setRequestProperty("User-agent","ua");//模拟手机连接conn.connect();Stringlocation=conn.getHeaderField("Location");returnlocation;}catch(Exceptione){e.printStackTrace();}return"";}publicstaticStringmatchNo(StringredirectUrl){List<String>results=newArrayList<>();Patternp=Pattern.compile("video/([\\w/\\.]*)/");Matcherm=p.matcher(redirectUrl);while(!m.hitEnd()&&m.find()){results.add(m.group(1));}returnresults.get(0);}publicstaticStringhSMatchNo(StringredirectUrl){List<String>results=newArrayList<>();Patternp=Pattern.compile("item_id=([\\w/\\.]*)&");Matcherm=p.matcher(redirectUrl);while(!m.hitEnd()&&m.find()){results.add(m.group(1));}returnresults.get(0);}publicstaticStringhttpGet2(StringurlStr)throwsException{URLurl=newURL(urlStr);HttpURLConnectionconn=(HttpURLConnection)url.openConnection();conn.setRequestMethod("GET");conn.setRequestProperty("Content-Type","text/json;charset=utf-8");BufferedReaderin=newBufferedReader(newInputStreamReader(conn.getInputStream(),"UTF-8"));StringBufferbuf=newStringBuffer();StringinputLine=in.readLine();while(inputLine!=null){buf.append(inputLine).append("\r\n");inputLine=in.readLine();}in.close();returnbuf.toString();}/***使用Get方式获取数据**@paramurlURL包括参数,http://HOST/XX?XX=XX&XXX=XXX*@return*/publicstaticStringhttpGet(Stringurl){Stringresult="";BufferedReaderin=null;try{URLrealUrl=newURL(url);//打开和URL之间的连接URLConnectionconnection=realUrl.openConnection();//设置通用的请求属性connection.setRequestProperty("accept","*/*");connection.setRequestProperty("connection","Keep-Alive");connection.setRequestProperty("user-agent","Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1)");//建立实际的连接connection.connect();//定义BufferedReader输入流来读取URL的响应in=newBufferedReader(newInputStreamReader(connection.getInputStream(),"UTF-8"));Stringline;while((line=in.readLine())!=null){result+=line;}}catch(Exceptione){System.out.println("发送GET请求出现异常!"+e);e.printStackTrace();}//使用finally块来关闭输入流finally{try{if(in!=null){in.close();}}catch(Exceptione2){e2.printStackTrace();}}returnresult;}publicstaticStringparseUrl(Stringurl){Stringhost="";Patternp=Pattern.compile("http[:|/|\\w|\\.]+");Matchermatcher=p.matcher(url);if(matcher.find()){host=matcher.group();}returnhost.trim();}/***查找域名(以https开头com结尾)**@paramurl*@return*/publicstaticStringgetDomainName(Stringurl){Stringhost="";Patternp=Pattern.compile("https://.*\\.com");Matchermatcher=p.matcher(url);if(matcher.find()){host=matcher.group();}returnhost.trim();}}
到此,相信大家对“Java怎么实现抖音去水印”有了更深的了解,不妨来实际操作一番吧!这里是恰卡编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!