抖音视频去水印

发布时间:2020-03-12 19:35:07 作者:Mos 阅读量:14096

免费快捷地址:抖音视频去水印

Java代码:

package com.manage.util;

import org.jsoup.Jsoup;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * DouYinQushuiyinUtil 抖音去水印
 *
 * @author <a href="morse.jiang@foxmail.com">JiangWen</a>
 * @version 1.0.0, 2019/10/28 0028 13:56
 */
public class DouYinQushuiyinUtil {


  private static final Pattern patternCompile = Pattern.compile("(?<=playAddr: \")https?://.+(?=\",)");

  /**
   * 去水印
   *
   * @param url
   *
   * @return
   *
   * @throws Exception
   */
  public static byte[] qushuiyinToByte(String url) throws Exception {
    return toByteArray(qushuiyinToStream(url));
  }


  public static InputStream qushuiyinToStream(String url) throws Exception {
    // 过滤链接,获取http连接地址
    String finalUrl = decodeHttpUrl(url);

    // 利用Jsoup抓取抖音链接
    // 抓取抖音网页
    String htmls = Jsoup.connect(finalUrl).ignoreContentType(true).execute().body();
    Matcher m = patternCompile.matcher(htmls);

    // 利用Matcher中的group方法获取匹配的特定字符串
    // 利用String的replace方法替换特定字符,得到抖音的去水印链接
    String matchUrl = "";
    while (m.find()) {
      matchUrl = m.group(0).replaceAll("playwm", "play");
    }

    // 将链接封装成流
    // 注:由于抖音对请求头有限制,只能设置一个伪装手机浏览器请求头才可实现去水印下载
    Map<String, String> headers = new HashMap<>();
    headers.put("Connection", "keep-alive");
    headers.put("Host", "aweme.snssdk.com");
    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"
    );

    // 利用Joup获取视频对象,并作封装成一个输入流对象(最大50m)
    BufferedInputStream in = Jsoup.connect(matchUrl).headers(headers).timeout(10000).ignoreContentType(true).maxBodySize(50*1024*1024).execute()
                                  .bodyStream();
    return in;
  }

  private static String decodeHttpUrl(String url) {
    Matcher matcher = Patterns.WEB_URL.matcher(url);
    if (matcher.find()) {
      return matcher.group();
    }
    return "";
  }

  private static byte[] toByteArray(InputStream input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024 * 4];
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
      output.write(buffer, 0, n);
    }
    return output.toByteArray();
  }

}

转载请注明出处:文思齐远博客,如果对您有帮助,请帮忙点一下右侧的广告,谢谢您了!
支付宝打赏 微信打赏

我要评论

猜你想搜

实时天气

©2020 i847.cn
部分内容转自网络,如有损害您的权益,致邮联系:jiang2008wen#126.com,一经证实,立即删除!    我要留言
备案号:蜀ICP备18020563号-1