欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

抖音视频去水印

程序员文章站 2022-04-11 16:05:39
...

基于httpclient实现在线下载抖音去水印后的视频

DeleteWatermarkController.java
@Controller
@Slf4j
public class DeleteWatermarkController {

    private final static String ERROR = "error";

    private final static String LINK_PREFIX = "https://v.douyin.com/";

    private final static String SHARE_PREFIX = "https://www.iesdouyin.com/share/video/";

    private final static String UPLOAD_PREFIX = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";

    @GetMapping("/deleteWatermark")
    @ResponseBody
    public String deleteWatermark(@RequestParam("link") String link) {
        Matcher linkMatcher = Pattern.compile(LINK_PREFIX + "[a-zA-Z0-9]*/").matcher(link);
        if (!linkMatcher.find()) {
            return ERROR;
        }
        String share = getShare(linkMatcher.group());
        if (null == share) {
            return ERROR;
        }
        Matcher shareMatcher = Pattern.compile(SHARE_PREFIX + "[0-9]*/").matcher(share);
        if (!shareMatcher.find()) {
            return ERROR;
        }
        String uploadUrl = getUploadUrl(shareMatcher.group().substring(SHARE_PREFIX.length(), shareMatcher.group().length() - 1));
        return uploadUrl == null ? ERROR : uploadUrl;
    }

    public String getShare(String url) {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36");
        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();
        httpGet.setConfig(config);
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity entity = response.getEntity();
            if (null != entity) {
                Header[] headers = response.getHeaders("Location");
                if (headers != null && headers.length > 0) {
                    return headers[0].getValue();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public String getUploadUrl(String videoId) {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        HttpGet httpGet = new HttpGet(UPLOAD_PREFIX + videoId);
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36");
        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();
        httpGet.setConfig(config);
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 配置信息
            RequestConfig requestConfig = RequestConfig.custom()
                    // 设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    // 设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    // socket读写超时时间(单位毫秒)
                    .setSocketTimeout(5000)
                    // 设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();
            // 将上面的配置信息 运用到这个Get请求里
            httpGet.setConfig(requestConfig);
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                DeleteWatermarkDTO douVideoRes = JSONUtil.toBean(EntityUtils.toString(responseEntity), DeleteWatermarkDTO.class);
                return douVideoRes.getItemList().get(0).getVideo().getPlayAddr().getUrlList().get(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
DeleteWatermarkDTO.java
@NoArgsConstructor
@Data
public class DeleteWatermarkDTO {

    @JsonProperty("status_code")
    private Integer statusCode;
    @JsonProperty("item_list")
    private List<ItemListDTO> itemList;
    @JsonProperty("extra")
    private ExtraDTO extra;

    @NoArgsConstructor
    @Data
    public static class ExtraDTO {
        @JsonProperty("logid")
        private String logid;
        @JsonProperty("now")
        private Long now;
    }

    @NoArgsConstructor
    @Data
    public static class ItemListDTO {
        @JsonProperty("aweme_id")
        private String awemeId;
        @JsonProperty("duration")
        private Integer duration;
        @JsonProperty("forward_id")
        private String forwardId;
        @JsonProperty("video")
        private VideoDTO video;
        @JsonProperty("comment_list")
        private Object commentList;
        @JsonProperty("images")
        private Object images;
        @JsonProperty("desc")
        private String desc;
        @JsonProperty("author")
        private AuthorDTO author;
        @JsonProperty("music")
        private MusicDTO music;
        @JsonProperty("aweme_type")
        private Integer awemeType;
        @JsonProperty("long_video")
        private Object longVideo;
        @JsonProperty("cha_list")
        private List<ChaListDTO> chaList;
        @JsonProperty("label_top_text")
        private Object labelTopText;
        @JsonProperty("statistics")
        private StatisticsDTO statistics;
        @JsonProperty("video_labels")
        private Object videoLabels;
        @JsonProperty("is_preview")
        private Integer isPreview;
        @JsonProperty("is_live_replay")
        private Boolean isLiveReplay;
        @JsonProperty("create_time")
        private Integer createTime;
        @JsonProperty("text_extra")
        private List<TextExtraDTO> textExtra;
        @JsonProperty("author_user_id")
        private Long authorUserId;
        @JsonProperty("group_id")
        private Long groupId;
        @JsonProperty("share_url")
        private String shareUrl;
        @JsonProperty("share_info")
        private ShareInfoDTO shareInfo;
        @JsonProperty("geofencing")
        private Object geofencing;
        @JsonProperty("video_text")
        private Object videoText;
        @JsonProperty("promotions")
        private Object promotions;
        @JsonProperty("image_infos")
        private Object imageInfos;
        @JsonProperty("risk_infos")
        private RiskInfosDTO riskInfos;
        @JsonProperty("group_id_str")
        private String groupIdStr;

        @NoArgsConstructor
        @Data
        public static class VideoDTO {
            @JsonProperty("play_addr")
            private PlayAddrDTO playAddr;
            @JsonProperty("height")
            private Integer height;
            @JsonProperty("origin_cover")
            private OriginCoverDTO originCover;
            @JsonProperty("ratio")
            private String ratio;
            @JsonProperty("duration")
            private Integer duration;
            @JsonProperty("cover")
            private CoverDTO cover;
            @JsonProperty("width")
            private Integer width;
            @JsonProperty("dynamic_cover")
            private DynamicCoverDTO dynamicCover;
            @JsonProperty("has_watermark")
            private Boolean hasWatermark;
            @JsonProperty("bit_rate")
            private Object bitRate;
            @JsonProperty("vid")
            private String vid;

            @NoArgsConstructor
            @Data
            public static class PlayAddrDTO {
                @JsonProperty("url_list")
                private List<String> urlList;
                @JsonProperty("uri")
                private String uri;
            }

            @NoArgsConstructor
            @Data
            public static class OriginCoverDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }

            @NoArgsConstructor
            @Data
            public static class CoverDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }

            @NoArgsConstructor
            @Data
            public static class DynamicCoverDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }
        }

        @NoArgsConstructor
        @Data
        public static class AuthorDTO {
            @JsonProperty("type_label")
            private Object typeLabel;
            @JsonProperty("short_id")
            private String shortId;
            @JsonProperty("nickname")
            private String nickname;
            @JsonProperty("signature")
            private String signature;
            @JsonProperty("avatar_thumb")
            private AvatarThumbDTO avatarThumb;
            @JsonProperty("followers_detail")
            private Object followersDetail;
            @JsonProperty("geofencing")
            private Object geofencing;
            @JsonProperty("policy_version")
            private Object policyVersion;
            @JsonProperty("uid")
            private String uid;
            @JsonProperty("avatar_larger")
            private AvatarLargerDTO avatarLarger;
            @JsonProperty("avatar_medium")
            private AvatarMediumDTO avatarMedium;
            @JsonProperty("unique_id")
            private String uniqueId;
            @JsonProperty("platform_sync_info")
            private Object platformSyncInfo;

            @NoArgsConstructor
            @Data
            public static class AvatarThumbDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }

            @NoArgsConstructor
            @Data
            public static class AvatarLargerDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }

            @NoArgsConstructor
            @Data
            public static class AvatarMediumDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }
        }

        @NoArgsConstructor
        @Data
        public static class MusicDTO {
            @JsonProperty("id")
            private Long id;
            @JsonProperty("cover_large")
            private CoverLargeDTO coverLarge;
            @JsonProperty("cover_medium")
            private CoverMediumDTO coverMedium;
            @JsonProperty("cover_thumb")
            private CoverThumbDTO coverThumb;
            @JsonProperty("position")
            private Object position;
            @JsonProperty("status")
            private Integer status;
            @JsonProperty("mid")
            private String mid;
            @JsonProperty("title")
            private String title;
            @JsonProperty("author")
            private String author;
            @JsonProperty("cover_hd")
            private CoverHdDTO coverHd;
            @JsonProperty("play_url")
            private PlayUrlDTO playUrl;
            @JsonProperty("duration")
            private Integer duration;

            @NoArgsConstructor
            @Data
            public static class CoverLargeDTO {
                @JsonProperty("url_list")
                private List<String> urlList;
                @JsonProperty("uri")
                private String uri;
            }

            @NoArgsConstructor
            @Data
            public static class CoverMediumDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }

            @NoArgsConstructor
            @Data
            public static class CoverThumbDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }

            @NoArgsConstructor
            @Data
            public static class CoverHdDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }

            @NoArgsConstructor
            @Data
            public static class PlayUrlDTO {
                @JsonProperty("uri")
                private String uri;
                @JsonProperty("url_list")
                private List<String> urlList;
            }
        }

        @NoArgsConstructor
        @Data
        public static class StatisticsDTO {
            @JsonProperty("aweme_id")
            private String awemeId;
            @JsonProperty("comment_count")
            private Integer commentCount;
            @JsonProperty("digg_count")
            private Integer diggCount;
            @JsonProperty("play_count")
            private Integer playCount;
            @JsonProperty("share_count")
            private Integer shareCount;
        }

        @NoArgsConstructor
        @Data
        public static class ShareInfoDTO {
            @JsonProperty("share_weibo_desc")
            private String shareWeiboDesc;
            @JsonProperty("share_desc")
            private String shareDesc;
            @JsonProperty("share_title")
            private String shareTitle;
        }

        @NoArgsConstructor
        @Data
        public static class RiskInfosDTO {
            @JsonProperty("warn")
            private Boolean warn;
            @JsonProperty("type")
            private Integer type;
            @JsonProperty("content")
            private String content;
        }

        @NoArgsConstructor
        @Data
        public static class ChaListDTO {
            @JsonProperty("view_count")
            private Integer viewCount;
            @JsonProperty("hash_tag_profile")
            private String hashTagProfile;
            @JsonProperty("cid")
            private String cid;
            @JsonProperty("cha_name")
            private String chaName;
            @JsonProperty("desc")
            private String desc;
            @JsonProperty("user_count")
            private Integer userCount;
            @JsonProperty("connect_music")
            private Object connectMusic;
            @JsonProperty("type")
            private Integer type;
            @JsonProperty("is_commerce")
            private Boolean isCommerce;
        }

        @NoArgsConstructor
        @Data
        public static class TextExtraDTO {
            @JsonProperty("start")
            private Integer start;
            @JsonProperty("end")
            private Integer end;
            @JsonProperty("type")
            private Integer type;
            @JsonProperty("hashtag_name")
            private String hashtagName;
            @JsonProperty("hashtag_id")
            private Long hashtagId;
        }
    }
}

传入抖音分享的链接就行啦

相关标签: 音视频 java