图片说明

抖音短视频无水印视频解析下载网页版java源码

最近自己需要下载抖音无水印视频,在论坛找了个遍,基本上都是电脑版或者APP。
感觉并不是很方便,不像网页版的,不局限于设备,直接可以下载。
因为现在作者分享页那个接口找不到,所以没办法通过作者分享页下载,但是可以输入很多个作品分享页,打包下载。

从输入框中找到所有的作品链接

public static List<String> getUrl(String data) {
    String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
    //list存放正则匹配的结果
    List<String> list = new ArrayList<>();
    try {
        //编译正则字符串
        Pattern p = Pattern.compile(regex);
        //利用正则去匹配
        Matcher matcher = p.matcher(data);
        //如果找到了我们正则里要的东西
        while (matcher.find()) {
            //保存到list中
            list.add(matcher.group());
        }
    }catch (Exception e){
        return list;
    }
    return list;
}

根据分享的作品短链接,获取第一次跳转后的长链接

public static List<String> getSecondUrl(List<String> urls){
    List<String> second_urls = new ArrayList<>();
    for(String url : urls){
        try {
            Connection.Response execute = Jsoup.connect(url).headers(headers).followRedirects(true).execute();
            second_urls.add(execute.url().toString());
        }catch (Exception e){
            continue;
        }
    }
    return second_urls;
}

从长链接获取ID

public static String getId(String url) {
        String regex = "[\\d]{19}";
        //编译正则字符串
        Pattern p = Pattern.compile(regex);
        //利用正则去匹配
        Matcher matcher = p.matcher(url);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

获取到所有的API链接

public static List<String> getApiUrl(List<String> second_urls){
        List<String> api_urls = new ArrayList<>();
        for(String second_url : second_urls){
            String id = getId(second_url);
            String new_url = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + id +"&dytk=";
            api_urls.add(new_url);
        }
        return api_urls;
    }

获取所有作品的标题,用户昵称,无水印下载地址

public static List<Map<String,String>> getListMap(List<String> api_urls){
        List<Map<String,String>> list_map = new ArrayList<>();
        for(String api_url : api_urls){
            Map<String,String> map = new HashMap<>();//存放下载信息
            try {
                String api_return_str = Jsoup.connect(api_url).headers(headers).ignoreContentType(true).followRedirects(true).get().text();//获取API返回值
                JSONObject api_return_json = JSONObject.parseObject(api_return_str);//接口放回字符串转JSON
                JSONArray item_list = api_return_json.getJSONArray("item_list");
                String nickname = item_list.getJSONObject(0).getJSONObject("author").getString("nickname");
                String share_title = item_list.getJSONObject(0).getJSONObject("share_info").getString("share_title");
                String shuiyin_url = item_list.getJSONObject(0).getJSONObject("video").getJSONObject("play_addr").getJSONArray("url_list").getString(0);
                String wu_shuiyin_url = shuiyin_url.replace("playwm","play");
                map.put("nickname",nickname);
                map.put("share_title",share_title);
                map.put("wu_shuiyin_url",wu_shuiyin_url);
                list_map.add(map);
            }catch (Exception e){
                e.printStackTrace();
                continue;
            }
        }
        return list_map;
    }

下载单个视频的接口

@RequestMapping("download")
public void download(String id, HttpServletResponse resp){
    String download_url = DouyinUtils.getDownloadUrlById(id);
    try {
        File file = new File(rarDir + File.separator + id +".mp4");
        if(!file.exists()){//如果文件不存在,则生成
            Connection.Response response = Jsoup.connect(download_url).headers(headers).ignoreContentType(true).execute();
            byte[] bytes = response.bodyAsBytes();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytes);
            fos.close();
        }
        resp.setContentType("application/octet-stream");
        resp.setHeader("Content-Disposition","attachment;filename=" + id + ".mp4");
        resp.setContentLength((int) file.length());
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] buffer = new byte[128];
            int count = 0;
            while ((count = fis.read(buffer)) > 0) {
                resp.getOutputStream().write(buffer, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            resp.getOutputStream().flush();
            resp.getOutputStream().close();
            fis.close();
        }
    }catch (Exception e){
 
    }
}

下载单个视频的接口

@RequestMapping("download")
public void download(String id, HttpServletResponse resp){
    String download_url = DouyinUtils.getDownloadUrlById(id);
    try {
        File file = new File(rarDir + File.separator + id +".mp4");
        if(!file.exists()){//如果文件不存在,则生成
            Connection.Response response = Jsoup.connect(download_url).headers(headers).ignoreContentType(true).execute();
            byte[] bytes = response.bodyAsBytes();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytes);
            fos.close();
        }
        resp.setContentType("application/octet-stream");
        resp.setHeader("Content-Disposition","attachment;filename=" + id + ".mp4");
        resp.setContentLength((int) file.length());
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] buffer = new byte[128];
            int count = 0;
            while ((count = fis.read(buffer)) > 0) {
                resp.getOutputStream().write(buffer, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            resp.getOutputStream().flush();
            resp.getOutputStream().close();
            fis.close();
        }
    }catch (Exception e){
 
    }
}

多个视频压缩未rar后的下载接口

@RequestMapping("batchdownload")
public void batchdownload(String rarFileName,HttpServletResponse resp) throws Exception {
    File rarFile = new File(rarDir + File.separator + rarFileName);
    resp.setContentType("application/octet-stream");
    resp.setHeader("Content-Disposition","attachment;filename=" + rarFileName);
    resp.setContentLength((int) rarFile.length());
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(rarFile);
        byte[] buffer = new byte[128];
        int count = 0;
        while ((count = fis.read(buffer)) > 0) {
            resp.getOutputStream().write(buffer, 0, count);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        resp.getOutputStream().flush();
        resp.getOutputStream().close();
        fis.close();
    }
}

 

评论0

请先

本站临时QQ客服236249898
没有账号? 忘记密码?