抖音去水印超簡單

一、前言

抖音去水印方法很簡單,以前一直沒有去研究,以爲搞個去水印還要用到算法去除,直到動手的時候才發現這麼簡單,不用編程基礎都能做。

二、原理與步驟

其實抖音它是有一個隱藏無水印地址的,只要我們找到那個地址就可以了
1、我們在抖音找一個想要去水印的視頻鏈接

9.23 mQK:/ 這輩子總要和你最愛的人來看一次洱海吧%洱海 %治癒系風景 https://v.douyin.com/NAmJfAJ/ 複製此鏈接,打開Dou音搜索,直接觀看視頻!

注意:這裏一定要是https開頭的,不是口令
打開瀏覽器訪問:

https://v.douyin.com/NAmJfAJ/

訪問之後會重定向到這個地址,後面有一串數字,這個就是視頻的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去實現,這個跟語言無關,只要能發請求就行

/**
 * 下載抖音無水印視頻
 *
 * @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();
    }
}

四、總結

其實看那個第二部分原理就行了是不是很簡單?
我自己有在微信小程序集成了,大家可以體驗一下
打開微信小程序搜索:日常多功能工具箱
大家有遇到什麼問題可以在下面留言,或者私信都可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章