Java实现抖音短视频去水印窗口程序

说明

本人也是初学Java不久的小白,可能会有很多地方不规范,本程序仅供参考,功能已经实现。

程序反映有点慢,按钮点一下就行哈

抖音短视频去水印原理:https://blog.csdn.net/qq_29556507/article/details/106943093

大家在看程序前先用电脑手动解析一下,就会知道大体流程以及我们大致需要怎么操作。

在用电脑一步一步解析出真实无水印地址的时候,需要注意的是:找到以这个https://aweme.snssdk.com/aweme开头的网站后,别忘了把playwm改为play再去访问,去水印原理的那篇文章有写的。

但是如果用电脑去访问修改完的这个网址电脑无法加载,手机可以跳转到真实的无水印地址,然后用谷歌浏览器打开下载无水印视频即可
在这里插入图片描述
在这里插入图片描述
因此我们的Java程序在访问的时候需要修改user-agent来实现模拟手机访问
程序中的:
public void doGet()

private void parseResult(InputStream inStream, String charSet)
这两个方法就是去改发送的数据实现模拟手机访问的方法。

程序演示

在这里插入图片描述
在这里插入图片描述

通过谷歌浏览器下载即可

代码区

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.*;
import java.io.InputStreamReader;

public class dy {
    
    
    String str="";
    String num="";
    String wz="";
    String strend = "";
    public static void main(String [] args) {
    
    
        dy dy = new dy();
        //创建窗口并进行布局
        JFrame f = new JFrame("02抖音去水印1.0");
        f.setLayout(null);
        f.setBounds(0,0,410,300);
        JLabel jl1 = new JLabel("地址:");
        jl1.setBounds(10,10,30,30);
        f.add(jl1);
        JTextField tf = new JTextField();
        tf.setBounds(45,12,280,25);
        f.add(tf);
        JLabel jl2 = new JLabel("打开:");
        jl2.setBounds(10,40,30,30);
        f.add(jl2);
        JTextArea ta = new JTextArea();
        ta.setLineWrap(true);
        ta.setBounds(45,40,280,200);
        ta.setEditable(false);
        JScrollPane jsp = new JScrollPane(ta);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        jsp.setBounds(45,40,280,200);
        f.add(jsp);
        JButton b = new JButton("OK");
        b.setHorizontalTextPosition(SwingConstants.CENTER);
        b.setVerticalTextPosition(SwingConstants.BOTTOM);
        b.setBounds(328,60,60,130);
        f.add(b);
        //设置按钮点击事件
        b.addActionListener(new ActionListener() {
    
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
    
                //获得tf文本框输入的需要解析的地址
                String text="";
                text=tf.getText();
                dy.str=text;
                if (text.length()==0){
    
    
                    ta.setText("地址为空");
                    return;
                }else {
    
    
                    try {
    
    
                        //对地址进行解析
                        dy.getNum();
                        dy.getWZ();
                        dy.doGet();
                        ta.setText(dy.strend);
                        //将解析完的网址自动复制到粘贴板上
                        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
                        Transferable tText = new StringSelection(dy.strend);
                        clip.setContents(tText, null);
                        //在tf文本框中显示复制成功提示
                        tf.setText("解析成功已复制到粘贴板,请去浏览器打开下载");

                    } catch (IOException ioException) {
    
    
                        ta.setText("地址错误");
                    }catch (StringIndexOutOfBoundsException e1){
    
    
                        ta.setText("地址错误");
                    }
                }

            }
        });
        //当tf文本框获得焦点时,自动清空内容
        tf.addFocusListener(new FocusListener() {
    
    
            @Override
            //获得焦点
            public void focusGained(FocusEvent e) {
    
    
                tf.setText("");
            }
            //失去焦点
            @Override
            public void focusLost(FocusEvent e) {
    
    

            }
        });
        //使窗口能正常关闭
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //显示窗口
        f.setVisible(true);

    }
    //获得视频的item_ids,也就是视频网址上的那个19位的数字串
    public void getNum()throws IOException{
    
    
        URL url = null;
        url = new URL(str);
        HttpURLConnection conn= null;
        conn = (HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);

        try {
    
    
            conn.getResponseCode();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        String realUrl=conn.getURL().toString();
        num=realUrl.substring(38,57);
    }
    //获得视频的地址https://aweme.snssdk.com/aweme开头的那个网址,并将网址中的playwm改为play
    public void getWZ()throws IOException{
    
    
        URL url= null;
        url = new URL("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids="+num);
        try {
    
    
            BufferedReader reader=new BufferedReader(new InputStreamReader(url.openStream()));
            String line;
            String str="";
            while((line=reader.readLine())!=null){
    
    
                str+=line;
            }
            reader.close();
            wz="https://aweme.snssdk.com/aweme/v1/play"+str.substring(str.indexOf("/?video"),str.indexOf("&line=0"))+"&line=0";
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
    //解决https://aweme.snssdk.com/aweme开头的网址只能手机端访问才能跳转的问题
    //并获得跳转后的真正的无水印地址
    public void doGet()throws IOException{
    
    


        URL obj = new URL(wz);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("GET");
        // 设置请求头
        con.setRequestProperty("user-agent", "Mozilla/5.0 ");

        int responseCode = con.getResponseCode();

        parseResult(con.getInputStream(), "GB2312");
    }
    private void parseResult(InputStream inStream, String charSet) {
    
    
        try {
    
    
            InputStreamReader stream = new InputStreamReader(inStream, charSet);
            BufferedReader br = new BufferedReader(stream);
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = br.readLine()) != null) {
    
    
                response.append(inputLine);
            }
            String str = response.toString();
            strend=str.substring(str.indexOf("http"),str.indexOf("\">F"));
            System.out.println(strend);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/z2742431760/article/details/108678470