[程序设计]Java实现解析抖音无水印视频

package pers.LovelyBunny;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

// https://maven.aliyun.com/repository/public/com/alibaba/fastjson/1.2.76/fastjson-1.2.76.jar

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

/**

  * 解析抖音无水印视频

  *

  * @author 张泽楠

  * @since 2021-06-15

  *

  */

public class DouYinParse {

public static void main(String[] args) {

System.out.println(parseDouYinVideo("https://v.douyin.com/ex3VFQs/"));

}

/**

  *

  * 解析抖音无水印视频

  *

  * @author 张泽楠

  * @param httpURL

  *             抖音视频分享链接

  * @return 抖音无水印视频链接

  */

public static String parseDouYinVideo(String httpURL) {

String result = null;

// 获取重定向后的 URL 地址,并从中截取 item_ids

String itemIDs = getRedirectsURL(httpURL).split("/")[5];

// 使用 item_ids 作为参数,外汇跟单gendan5.com请求携带有带水印的视频 URL JSON 数据

String jsonString = getJSON("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + itemIDs);

// 使用 FastJSON 解析为 JSONObject 对象

JSONObject json = JSON.parseObject(jsonString);

// 解析 JSON 数据,获取带水印的视频 URL

Object videoURL = json.getJSONArray("item_list").getJSONObject(0).getJSONObject("video")

.getJSONObject("play_addr").getJSONArray("url_list").get(0);

// 替换 URL 关键位置,得到无水印视频 URL

result = videoURL.toString().replace("/playwm/", "/play/");

return result;

}

/**

  *

  * 获取重定向后的 URL 地址

  *

  * @author 张泽楠

  * @param httpURL

  *             URL

  * @return 重定向后的 URL

  */

public static String getRedirectsURL(String httpURL) {

String result = null;

HttpURLConnection conn = null;

try {

// 配置请求头

conn = (HttpURLConnection) new URL(httpURL).openConnection();

// 禁止重定向

conn.setInstanceFollowRedirects(false);

// 获取重定向后的 URL

result = conn.getHeaderField("Location");

} catch (Exception e) {

e.printStackTrace();

} finally {

// 清理资源

conn.disconnect();

}

return result;

}

/**

  *

  * GET 的方式请求 JSON 数据

  *

  * @author 张泽楠

  * @param httpURL

  *             请求的 URL

  * @return 请求到的 JSON 数据

  */

public static String getJSON(String httpURL) {

HttpURLConnection connection = null;

InputStream is = null;

InputStreamReader isr = null;

BufferedReader br = null;

String result = null;

try {

URL url = new URL(httpURL);

// 配置请求头

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

// 发送 HTTP 连接请求

connection.connect();

// 如果请求连接成功则接收数据

if (connection.getResponseCode() == 200) {

// 读取并保存 JSON 数据

is = connection.getInputStream();

isr = new InputStreamReader(is, "UTF8");

br = new BufferedReader(isr);

StringBuffer sbf = new StringBuffer();

String temp = null;

while ((temp = br.readLine()) != null) {

sbf.append(temp);

}

result = sbf.toString();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

// 关闭资源

try {

if (null != br) {

br.close();

}

if (null != isr) {

isr.close();

}

if (null != is) {

is.close();

}

} catch (Exception e) {

e.printStackTrace();

}

// 清理资源

connection.disconnect();

}

return result;

}

}


请使用浏览器的分享功能分享到微信等