1 @PostMapping("geturl") 2 public DataResponse decodeDouiyin(@RequestBody DouyinRequest req ) throws IOException{ 3 DataResponse dataResponse=new DataResponse(); 4 //String url1 ="#在抖音,记录美好生活#是你,是你,还是你! http://v.douyin.com/xj4Agk/ 复制此链接,打开【抖音短视频】,直接观看视频!"; 5 6 //过滤链接,获取http连接地址 7 String finalUrl = decodeHttpUrl(req.getData().getLinkUrl()); 8 //1.利用Jsoup抓取抖音链接 9 //抓取抖音网页 10 String htmls = Jsoup.connect(finalUrl).ignoreContentType(true).execute().body(); 11 12 //2.利用正则匹配可以抖音下载链接 13 //playAddr: "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200ffc0000bfil6o4mavffbmroeo80&line=0", 14 //具体匹配内容格式:「https://aweme.snssdk.com/aweme/...line=0」 15 Pattern patternCompile = Pattern.compile("(?<=playAddr: \")https?://.+(?=\",)"); 16 //利用Pattern.compile("正则条件").matcher("匹配的字符串对象")方法可以将需要匹配的字段进行匹配封装 返回一个封装了匹配的字符串Matcher对象 17 18 //3.匹配后封装成Matcher对象 19 Matcher m = patternCompile.matcher(htmls); 20 21 //4.①利用Matcher中的group方法获取匹配的特定字符串 ②利用String的replace方法替换特定字符,得到抖音的去水印链接 22 String matchUrl =""; 23 while(m.find()) { 24 matchUrl = m.group(0).replaceAll("playwm", "play"); 25 } 26 27 //5.将链接封装成流 28 //注:由于抖音对请求头有限制,只能设置一个伪装手机浏览器请求头才可实现去水印下载 29 Map<String, String> headers = new HashMap<>(); 30 headers.put("Connection", "keep-alive"); 31 headers.put("Host", "aweme.snssdk.com"); 32 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"); 33 headers.put("Content-Encoding","gzip"); 34 //7.利用Joup获取视频对象,并封装成一个输入流对象 35 //BufferedInputStream in = Jsoup.connect(matchUrl).headers(headers).timeout(10000).ignoreContentType(true).execute().bodyStream(); 36 byte[] ins = Jsoup.connect(matchUrl).maxBodySize(6000000).headers(headers).timeout(15000).ignoreContentType(true).execute().bodyAsBytes(); 37 String filename=req.getSign()+".mp4"; 38 //UploadUtil.uploadFile(filename,in); 39 40 //8.保存文件到指定位置 41 File fileParent= new File("/home/ftpimages/images/"); 42 fileParent.setWritable(true, false); 43 if(!fileParent.exists()){ 44 fileParent.mkdirs(); 45 } 46 47 File file= new File("/home/ftpimages/images/"+filename); 48 file.createNewFile(); 49 50 BufferedOutputStream out = 51 new BufferedOutputStream( 52 new FileOutputStream(file)); 53 //copyFile(in,out); 54 out.write(ins,0,ins.length); 55 out.flush(); 56 out.close(); 57 //in.close(); 58 59 dataResponse.setVideoUrl("/images/"+filename); 60 61 return dataResponse; 62 }
Github地址:https://github.com/zwl568633995/douyindecode.git (麻烦star一下,谢谢)
代码如上,效果如图
需要上述微信小程序前端源码的朋友加我微信:zwl568633995