[程式設計]Java實現解析抖音無水印影片

專注的阿熊發表於2021-07-12

package pers.LovelyBunny;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

//

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("));

}

/**

  *

  * 解析抖音無水印影片

  *

  * @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(" + 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;

}

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2780912/,如需轉載,請註明出處,否則將追究法律責任。

相關文章