The most convenient and simplest watermarking solution to process video with java is attached with the java source code

This article is only for programming learning research, not for other purposes.   

I don't know what's going on, but
first, URL-decode the input short link, and store the decoded string in the "decodeUrl" variable.
Next, create a HashMap type data "data" to store the video information obtained from the Bilibili website.
Call the "getGetLocationtoutiao" method in the HttpUtil class to obtain the redirection link corresponding to the short link, and store it in the "location" variable.
Determine whether the "location" variable contains a key-value pair with the key name "location". If it exists, it means that the redirection link is obtained successfully, and the redirection link is stored in the "redirctUrl" variable.
Create a data "headers" of HashMap type to store the header information of the HTTP request, and set some common header information, such as User-Agent, Accept, etc.
Call the "doGetAndHead" method in the HttpUtil class, send a GET request and get the response content, and store the response content in the "body" variable.
Process the response content and replace ";(function())" with "@" to facilitate subsequent use of regular expression matching.
Use a regular expression to match the content between "window.INITIAL_STATE=" and ";(function())" in the response content and store it in the "initialStr" variable.
 

public Map bilibili(String shortUrl) throws Exception {
    byte[] decode = URLDecoder.decode(shortUrl.getBytes());
    String decodeUrl = new String(decode);
    HashMap<String, String> data = new HashMap<>();
    Map<String, String> location = HttpUtil.getGetLocationtoutiao(decodeUrl);
    if (location.containsKey("location")) {
        String redirctUrl = location.get("location");
        HashMap<String, String> headers = new HashMap<>();
        //headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.46");
        headers.put("user-agent", "Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.91 Mobile Safari/537.36");
        headers.put("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        headers.put("accept-encoding", "gzip, deflate, br");
        String body = HttpUtil.doGetAndHead(redirctUrl, headers);
        String s = body.replaceAll(";\\(function\\(\\)", "@");
        String initialReg = "window.__INITIAL_STATE__=([^@]+)";
        Pattern initialP = Pattern.compile(initialReg);
        Matcher initialM = initialP.matcher(s);
        if (initialM.find()) {
            String initialStr = initialM.group(1);
            JSONObject jo = JSONObject.parseObject(initialStr);
            if (jo.containsKey("video")) {
                jo = jo.getJSONObject("video");
                if(jo.containsKey("viewInfo")){
                    jo = jo.getJSONObject("viewInfo");
                    if (jo.containsKey("title")) {
                        String title = jo.getString("title");
                        data.put("desc", title);
                    }
                    if (jo.containsKey("pic")) {
                        String pic = jo.getString("pic");
                        String cover = URLDecoder.decodeForPath(pic, Charset.defaultCharset());
                        data.put("cover", toHttps(cover));
                    }
                }
            }
        }

        String playInfoReg = "var options = ([^;]+)";
        Pattern p = Pattern.compile(playInfoReg);
        Matcher m = p.matcher(s);
        if(m.find()){
            String group = m.group(1);
            JSONObject jo = JSONObject.parseObject(group);
            if(jo.containsKey("readyVideoUrl")){
                String videoUrl = jo.getString("readyVideoUrl");
                data.put("videoUrl",toHttps(videoUrl));
            }
        }



        String playInfoReg = "window.__playinfo__=([^<]+)";
        Pattern p = Pattern.compile(playInfoReg);
        Matcher m = p.matcher(s);
        if (m.find()) {
            String videoInfo = m.group(1);
            JSONObject jo = JSONObject.parseObject(videoInfo);
            if (jo.containsKey("data")) {
                jo = jo.getJSONObject("data");
                if (jo.containsKey("dash")) {
                    jo = jo.getJSONObject("dash");
                    if (jo.containsKey("video")) {
                        JSONArray videos = jo.getJSONArray("video");
                        if (videos.size() > 0) {
                            JSONObject videoJo = videos.getJSONObject(0);
                            String baseUrl = videoJo.getString("baseUrl");
                            data.put("videoUrl", baseUrl);
                            data.put("referer", redirctUrl);
                        }
                    }
                }
            }
        }
    }
    if (StringUtils.isNotBlank(data.get("videoUrl"))) {
        return data;
    }
    return null;
}

 Network disk address of more platform source code:

Link: https://pan.baidu.com/s/1S-I6Pvv31iwNgzVTZVSLwA 
Extraction code: rwtk Welcome everyone to communicate

https://minivideo.chaoniuck.com/erweima.jpg

Guess you like

Origin blog.csdn.net/javayxc/article/details/129825170
Recommended