JAVA版解析抖音原视频By南京陈冠希师承剑总

小生我怕怕 2022-07-07 21:45:37 11217

package org.jeecg.modules.wechat.api;
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;

/**
* JAVA抖音解析
* @Auth 南京陈冠希
*/
public class DouYinParse {
public static void main(String[] args) {
System.out.println(parseDouYinVideo("https://v.douyin.com/efKYWbU/"));
}

/**
*
* 解析抖音无水印视频
* @param httpURL
* 抖音视频分享链接
* @return 抖音无水印视频链接
*/
public static String parseDouYinVideo(String httpURL) {
String result = null;
// 获取重定向后的URL地址,并从中截取item_ids
String itemIDs = getRedirectsURL(httpURL).split("/")[5];
// 使用item_ids作为参数,请求携带有带水印的视频URLJSON数据
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地址
* @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数据
* @param httpURL
* @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.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Accept-Encoding", "utf-8");
connection.setRequestProperty("Host", "api-hl.amemv.com");
connection.setRequestProperty("user-agent","Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
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;
}
}

想看隐藏的秘密吗?

最后于 2022-7-14 被admin编辑 ,原因:
这家伙太懒了,什么也没留下。

社区声明 1、本站提供的一切软件、教程和内容信息仅限用于学习和研究目的
2、本站资源为用户分享,如有侵权请邮件与我们联系处理敬请谅解!
3、本站信息来自网络,版权争议与本站无关。您必须在下载后的24小时之内,从您的电脑或手机中彻底删除上述内容
最新回复 (1)

您可以在 登录 or 注册 后,对此帖发表评论!

返回