Home  >  Article  >  Java  >  Java programmers hand-write a Douyin video watermark removal tool

Java programmers hand-write a Douyin video watermark removal tool

coldplay.xixi
coldplay.xixiforward
2020-11-27 17:02:3011928browse

java Basics introduces the method of removing watermark tools.

Java programmers hand-write a Douyin video watermark removal tool

Related learning recommendations: java basics

百cause有愿

Let me tell you why I want to make a Douyin video watermark removal tool. It’s actually because of my silly girlfriend. She actually fucked me~

One night she was in Douyin saw a very educational video, "If a man loves his wife, he should do all the housework", and then he wanted to download the video and share it with her sisters group to communicate 元夫 Thoughts.

But everyone knows that the videos downloaded from Douyin are watermarked. As a player with severe obsessive-compulsive disorder, this is not allowed. If there is no way, then look for a watermark removal tool. I searched around and found one. Either it is charging or it cannot be downloaded, and the smile on the master's face is gradually disappearing.

I joked on the side: It’s not that difficult, how about I make one for you! "Are you okay?" Then he cast a disdainful look.

Java programmers hand-write a Douyin video watermark removal tool

oops! It was just a joke, but you actually said that I am not good. This is unbearable. I have to prove it to you! Men, I just can’t stand this.

Let’s take a look at the online preview of the watermark removal tool I made: 47.93.6.5:8888/index

Java programmers hand-write a Douyin video watermark removal tool

Let’s analyze the idea of ​​​​making this watermark removal tool with everyone. When many people hear watermark removal at first glance, they subconsciously think that it is an awesome algorithm. In fact, it is an algorithm. It’s an illusion~

Go to the bottom of things

Although I have to argue, I was really confused when I first started doing it because I didn’t know where to start. What is the principle behind watermark removal? Is it possible that I still need to write an algorithm?

I found a sharing link for a Douyin video. After a little analysis, it was not difficult to find that this is a processed short link. Then this short link will definitely redirect to the real video addressURL .

https://v.douyin.com/JSkuhE4/

Enter the short link in the browser and get the following URL. Based on my experience, I judge that the 6820792802394262795 in URL is very likely to be The unique ID of the video, and the unique ID is usually used as an input parameter for the interface to obtain details. Hehe~ I seem to have a clue.

https://www.iesdouyin.com/share/video/6820792802394262795/

Java programmers hand-write a Douyin video watermark removal tool

hurry up F12 Dafa opened the console and found such an interface among many requests. It actually used the unique ID above.

https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=6820792802394262795


What’s even more surprising is that the data returned by the interface is so detailed, including author information, audio address, video address, and floor plan. But there is no video URL without watermark.

Only found one video with watermark URL, I was a little disappointed. I looked at the address again and found that wm was a bit similar to the name of my project. Ah, isn't it the abbreviation of watermark?

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


As if I saw a glimmer of hope again, I quickly modified URL and tried it again in the browser, and sure enough, there was no watermark.

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

Java programmers hand-write a Douyin video watermark removal tool
Only then did I discover DouyinRemove the watermark It’s so simple that it’s touching, hahaha~

## Practice it personally

Now that the principles are clear, all that's left is to implement the function step by step. The principle seems quite simple, but there are still some pitfalls in the implementation, which wastes a lot of time.

The implementation process has only three simple steps:

    1. Filter out the short video link from the input box
  • 2. Pass the short link to the backend and parse it out Video without watermark
  • URL
  • 3. Video
  • URL is passed to the front-end for preview and download
. It is not difficult for the back-end in one step. Just follow the analysis process above to parse the real video

URL in one step.

注意 :我们想得到的地址URL,都是当前短连接URL 经过重定向后的URL。而抖音有些链接是不支持浏览器访问的,所以要手动修改 User-agent 属性模拟移动端访问才可以。

/**
* @param url
* @author xiaofu
* @description 获取当前链接重定向后的url
* @date 2020/9/15 12:43
*/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 "";
    }

下边是完整的后端实现,可以看到代码量非常的少。

/**
 * @author xiaofu-公众号:程序员内点事
 * @description 抖音无水印视频下载
 * @date 2020/9/15 18:44
 */@Slf4j
@Controllerpublic class DYController {
    public static String DOU_YIN_BASE_URL = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";
    /**
     * @param url
     * @author xiaofu
     * @description 解析抖音无水印视频
     * @date 2020/9/15 12:43
     */
    @RequestMapping("/parseVideoUrl")
    @ResponseBody    public String parseVideoUrl(@RequestBody String url) throws Exception {
        DYDto dyDto = new DYDto();
        try {
            url = URLDecoder.decode(url).replace("url=", "");
            /**
             * 1、短连接重定向后的 URL
             */
            String redirectUrl = CommonUtils.getLocation(url);

            /**
             * 2、拿到视频对应的 ItemId
             */
            String videoUrl = "";
            String musicUrl = "";
            String videoPic = "";
            String desc = "";
            if (!StringUtils.isEmpty(redirectUrl)) {
                /**
                 * 3、用 ItemId 拿视频的详细信息,包括无水印视频url
                 */
                String itemId = CommonUtils.matchNo(redirectUrl);
                StringBuilder sb = new StringBuilder();
                sb.append(DOU_YIN_BASE_URL).append(itemId);
                String videoResult = CommonUtils.httpGet(sb.toString());
                DYResult dyResult = JSON.parseObject(videoResult, DYResult.class);
                /**
                 * 4、无水印视频 url
                 */
                videoUrl = dyResult.getItem_list().get(0)
                        .getVideo().getPlay_addr().getUrl_list().get(0)
                        .replace("playwm", "play");
                String videoRedirectUrl = CommonUtils.getLocation(videoUrl);
                dyDto.setVideoUrl(videoRedirectUrl);
                /**
                 * 5、音频 url
                 */
                musicUrl = dyResult.getItem_list().get(0).getMusic().getPlay_url().getUri();
                dyDto.setMusicUrl(musicUrl);
                /**
                 * 6、封面
                 */
                videoPic = dyResult.getItem_list().get(0).getVideo().getDynamic_cover().getUrl_list().get(0);
                dyDto.setVideoPic(videoPic);
                /**
                 * 7、视频文案
                 */
                desc = dyResult.getItem_list().get(0).getDesc();
                dyDto.setDesc(desc);
            }
        } catch (Exception e) {
            log.error("去水印异常 {}", e);
        }
        return JSON.toJSONString(dyDto);
    }}

前端实现也比较简单,拿到后端解析出来的视频URL 预览播放、下载就OK了。

Java programmers hand-write a Douyin video watermark removal tool

为快速实现我用了老古董JQuery,我这个年纪的人对它感情还是很深厚的,UI 框架用的 layer.js。源码后边会分享给大家,就不全贴出来了。

$.ajax({
    url: '/parseVideoUrl',
    type: 'POST',
    data: {"url": link},
    success: function (data) {
        $('.qsy-submit').attr('disabled', false);
        try {
            var rows = JSON.parse(data);
            layer.close(index);
            layer.open({
                type: 1,
                title: false,
                closeBtn: 1,
                shadeClose: true,
                skin: 'yourclass',
                content: `

`                 //content: ``             });         } catch (error) {             layer.alert('错误信息:' + error, {                 title: '异常',                 skin: 'layui-layer-lan',                 closeBtn: 0,                 anim: 4 //动画类型             });             return false;         }     },     error: function (err) {         console.log(err);         layer.close(index);         $('.qsy-submit').attr('disabled', false);     },     done: function () {         layer.close(index);     }})})

注意:我们在自己的网站中引用其它网站的资源URL,由于不在同一个域名下referrer 不同,通常会遇到三方网站的防盗链拦截,所以要想正常访问三方资源,必须要隐藏请求的referrer,页面中设置如下参数。

 
 

还简单做了下移动端适配,样式看着还可以,但是功能使用起来有点差强人意,后边在做优化了。

Java programmers hand-write a Douyin video watermark removal tool

总结

很多东西就是这样,没认真研究之前总感觉深不可测,可一旦接触到技术的本质,又开始笑自己之前好蠢,懂与不懂有时就查那么一层窗户纸。

The above is the detailed content of Java programmers hand-write a Douyin video watermark removal tool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete