272 lines
12 KiB
Java
272 lines
12 KiB
Java
package com.tuoheng;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public class ZlmClient {
|
|
|
|
private static final String ZLM_BASE_URL = "http://114.67.89.4:8778";
|
|
private static final String SECRET = "su6TiedN2rVAmBbIDX0aa0QTiBJLBdcf";
|
|
|
|
public static void main(String[] args) {
|
|
try {
|
|
// 调用 startRecord 方法
|
|
// String result = startRecord(1, "__defaultVhost__", "live", "prod", 86400);
|
|
// System.out.println("startRecord 响应结果:");
|
|
// System.out.println(result);
|
|
//
|
|
// System.out.println("\n" + "=".repeat(50) + "\n");
|
|
|
|
// 调用 isRecording 方法
|
|
// String recordingStatus = isRecording(1, "__defaultVhost__", "live", "prod");
|
|
// System.out.println("isRecording 响应结果:");
|
|
// System.out.println(recordingStatus);
|
|
//
|
|
// System.out.println("\n" + "=".repeat(50) + "\n");
|
|
|
|
// 调用 getMp4RecordFile 方法
|
|
String mp4Files = getMp4RecordFile("__defaultVhost__", "live", "prod", "2025-12-10");
|
|
System.out.println("getMp4RecordFile 响应结果:");
|
|
System.out.println(mp4Files);
|
|
|
|
System.out.println("\n" + "=".repeat(50) + "\n");
|
|
|
|
// 调用 deleteRecordDirectory 方法
|
|
String deleteResult = deleteRecordDirectory("__defaultVhost__", "live", "prod", "2025-12-10");
|
|
System.out.println("deleteRecordDirectory 响应结果:");
|
|
System.out.println(deleteResult);
|
|
|
|
System.out.println("\n" + "=".repeat(50) + "\n");
|
|
|
|
// 调用 stopRecord 方法
|
|
// String stopResult = stopRecord(1, "__defaultVhost__", "live", "prod");
|
|
// System.out.println("stopRecord 响应结果:");
|
|
// System.out.println(stopResult);
|
|
} catch (Exception e) {
|
|
System.err.println("调用失败: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 开始录制
|
|
*
|
|
* @param type 类型 (0 或 1)
|
|
* @param vhost 虚拟主机
|
|
* @param app 应用名
|
|
* @param stream 流ID
|
|
* @param maxSecond 最大录制时长(秒)
|
|
* @return 响应结果 JSON 字符串
|
|
* @throws Exception 如果请求失败
|
|
*/
|
|
public static String startRecord(int type, String vhost, String app, String stream, int maxSecond) throws Exception {
|
|
// 构建 URL 参数
|
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
|
urlBuilder.append("/index/api/startRecord?");
|
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&type=").append(type);
|
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&max_second=").append(maxSecond);
|
|
|
|
// 创建 HTTP 连接
|
|
URL url = new URL(urlBuilder.toString());
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setConnectTimeout(10000);
|
|
connection.setReadTimeout(10000);
|
|
|
|
// 获取响应
|
|
int responseCode = connection.getResponseCode();
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
StringBuilder response = new StringBuilder();
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
response.append(line);
|
|
}
|
|
reader.close();
|
|
return response.toString();
|
|
} else {
|
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查录制状态
|
|
*
|
|
* @param type 类型 (0 或 1)
|
|
* @param vhost 虚拟主机
|
|
* @param app 应用名
|
|
* @param stream 流ID
|
|
* @return 响应结果 JSON 字符串
|
|
* @throws Exception 如果请求失败
|
|
*/
|
|
public static String isRecording(int type, String vhost, String app, String stream) throws Exception {
|
|
// 构建 URL 参数
|
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
|
urlBuilder.append("/index/api/isRecording?");
|
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&type=").append(type);
|
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
|
|
|
// 创建 HTTP 连接
|
|
URL url = new URL(urlBuilder.toString());
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setConnectTimeout(10000);
|
|
connection.setReadTimeout(10000);
|
|
|
|
// 获取响应
|
|
int responseCode = connection.getResponseCode();
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
StringBuilder response = new StringBuilder();
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
response.append(line);
|
|
}
|
|
reader.close();
|
|
return response.toString();
|
|
} else {
|
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 停止录制
|
|
*
|
|
* @param type 类型 (0 或 1)
|
|
* @param vhost 虚拟主机
|
|
* @param app 应用名
|
|
* @param stream 流ID
|
|
* @return 响应结果 JSON 字符串
|
|
* @throws Exception 如果请求失败
|
|
*/
|
|
public static String stopRecord(int type, String vhost, String app, String stream) throws Exception {
|
|
// 构建 URL 参数
|
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
|
urlBuilder.append("/index/api/stopRecord?");
|
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&type=").append(type);
|
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
|
|
|
// 创建 HTTP 连接
|
|
URL url = new URL(urlBuilder.toString());
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setConnectTimeout(10000);
|
|
connection.setReadTimeout(10000);
|
|
|
|
// 获取响应
|
|
int responseCode = connection.getResponseCode();
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
StringBuilder response = new StringBuilder();
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
response.append(line);
|
|
}
|
|
reader.close();
|
|
return response.toString();
|
|
} else {
|
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取MP4录制文件列表
|
|
*
|
|
* @param vhost 虚拟主机
|
|
* @param app 应用名
|
|
* @param stream 流ID
|
|
* @param period 日期 (格式: YYYY-MM-DD)
|
|
* @return 响应结果 JSON 字符串
|
|
* @throws Exception 如果请求失败
|
|
*/
|
|
public static String getMp4RecordFile(String vhost, String app, String stream, String period) throws Exception {
|
|
// 构建 URL 参数
|
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
|
urlBuilder.append("/index/api/getMp4RecordFile?");
|
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&period=").append(URLEncoder.encode(period, StandardCharsets.UTF_8.name()));
|
|
|
|
// 创建 HTTP 连接
|
|
URL url = new URL(urlBuilder.toString());
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setConnectTimeout(10000);
|
|
connection.setReadTimeout(10000);
|
|
|
|
// 获取响应
|
|
int responseCode = connection.getResponseCode();
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
StringBuilder response = new StringBuilder();
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
response.append(line);
|
|
}
|
|
reader.close();
|
|
return response.toString();
|
|
} else {
|
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除录制目录
|
|
*
|
|
* @param vhost 虚拟主机
|
|
* @param app 应用名
|
|
* @param stream 流ID
|
|
* @param period 日期 (格式: YYYY-MM-DD)
|
|
* @return 响应结果 JSON 字符串
|
|
* @throws Exception 如果请求失败
|
|
*/
|
|
public static String deleteRecordDirectory(String vhost, String app, String stream, String period) throws Exception {
|
|
// 构建 URL 参数
|
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
|
urlBuilder.append("/index/api/deleteRecordDirectory?");
|
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
|
urlBuilder.append("&period=").append(URLEncoder.encode(period, StandardCharsets.UTF_8.name()));
|
|
|
|
// 创建 HTTP 连接
|
|
URL url = new URL(urlBuilder.toString());
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setConnectTimeout(10000);
|
|
connection.setReadTimeout(10000);
|
|
|
|
// 获取响应
|
|
int responseCode = connection.getResponseCode();
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
StringBuilder response = new StringBuilder();
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
response.append(line);
|
|
}
|
|
reader.close();
|
|
return response.toString();
|
|
} else {
|
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
|
}
|
|
}
|
|
}
|