Java实现超简单抖音去水印的示例详解

2023-01-21 0 4,749

目录

一、前言

抖音去水印方法很简单,以前一直没有去研究,以为搞个去水印还要用到算法去除,直到动手的时候才发现这么简单,不用编程基础都能做。

二、原理步骤

其实抖音它是有一个隐藏无水印地址的,只要我们找到那个地址就可以了

1、我们在抖音找一个想要去水印的视频链接

注意:这里一定要是https开头的,不是口令

打开浏览器访问:

Java实现超简单抖音去水印的示例详解

访问之后会重定向到这个地址,后面有一串数字,这个就是视频的id,他是根据这个唯一id来找到视频播放的

Java实现超简单抖音去水印的示例详解

按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

Java实现超简单抖音去水印的示例详解

直接用那个链接访问,他其实是一个有水印的链接,仔细观察发现最后那里有一段/playwm,有两个字母wm其实就是watermark英语单词的缩写,去掉wm后就能得到一个无水印链接了

https://aweme.snssdk.com/aweme/v1/play/?video_id=v0200fg10000c85i9ejc77ue0kb2vo80&ratio=720p&line=0

Java实现超简单抖音去水印的示例详解

到这里无水印已经完成了

三、代码实现

这里我用的是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(\"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 拿视频的详细信息,包括无水印视频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\");//模拟手机连接
            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方式获取数据
     *
     * @param url URL包括参数,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();
            // 设置通用的请求属性
            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的响应
            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.compile(\"http[:|/|\\\\w|\\\\.]+\");
        Matcher matcher = p.matcher(url);
        if (matcher.find()) {
            host = matcher.group();
        }

        return host.trim();
    }

    /**
     * 查找域名(以 https开头 com结尾)
     *
     * @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();
    }
}

四、总结

其实看那个第二部分原理就行了是不是很简单?

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 Java实现超简单抖音去水印的示例详解 http://www.20zxx.cn/463083/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务