|
|
|
@ -26,18 +26,6 @@ public class KlText2Video extends KlCommon {
|
|
|
|
|
private static final String GENERATION_PATH = "/v1/videos/text2video";
|
|
|
|
|
private static final String QUERY_PATH = "/v1/videos/text2video/";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成视频
|
|
|
|
|
*
|
|
|
|
|
* @param prompt 提示词
|
|
|
|
|
* @param modelName 模型名称,枚举值:kling-v1, kling-v1-6
|
|
|
|
|
* @return 任务ID
|
|
|
|
|
* @throws Exception 异常信息
|
|
|
|
|
*/
|
|
|
|
|
public static String generateVideo(String prompt, String modelName) throws Exception {
|
|
|
|
|
return generateVideo(prompt, modelName, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成视频
|
|
|
|
|
*
|
|
|
|
@ -142,54 +130,14 @@ public class KlText2Video extends KlCommon {
|
|
|
|
|
|
|
|
|
|
return responseJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询任务状态(通过外部任务ID)
|
|
|
|
|
*
|
|
|
|
|
* @param externalTaskId 外部任务ID
|
|
|
|
|
* @return 任务结果
|
|
|
|
|
* @throws Exception 异常信息
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject queryTaskStatusByExternalId(String externalTaskId) throws Exception {
|
|
|
|
|
// 获取JWT令牌
|
|
|
|
|
String jwt = KeLingJwtUtil.getJwt();
|
|
|
|
|
|
|
|
|
|
// 使用Hutool发送GET请求
|
|
|
|
|
HttpResponse response = HttpRequest.get(BASE_URL + GENERATION_PATH + "?external_task_id=" + externalTaskId)
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
|
.header("Authorization", "Bearer " + jwt)
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
// 检查响应状态码
|
|
|
|
|
if (response.getStatus() != 200) {
|
|
|
|
|
throw new Exception("请求失败,状态码:" + response.getStatus());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析响应
|
|
|
|
|
String responseBody = response.body();
|
|
|
|
|
JSONObject responseJson = JSONUtil.parseObj(responseBody);
|
|
|
|
|
log.info("查询任务状态响应:{}", responseBody);
|
|
|
|
|
|
|
|
|
|
// 检查响应状态
|
|
|
|
|
int code = responseJson.getInt("code");
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
String message = responseJson.getStr("message");
|
|
|
|
|
String solution = KlErrorCode.getSolutionByCode(code);
|
|
|
|
|
String errorMsg = String.format("查询任务状态失败:[%d] %s - %s", code, message, solution);
|
|
|
|
|
throw new Exception(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从URL下载文件到指定路径
|
|
|
|
|
*
|
|
|
|
|
* @param fileUrl 文件URL
|
|
|
|
|
* @param saveFilePath 保存路径
|
|
|
|
|
* @throws Exception 下载过程中的异常
|
|
|
|
|
*/
|
|
|
|
|
public static void downloadFile(String fileUrl, String saveFilePath) throws Exception {
|
|
|
|
|
public static void downloadFile(String fileUrl, String saveFilePath) {
|
|
|
|
|
try {
|
|
|
|
|
// 使用Hutool下载文件
|
|
|
|
|
long fileSize = HttpUtil.downloadFile(fileUrl, FileUtil.file(saveFilePath));
|
|
|
|
@ -216,7 +164,7 @@ public class KlText2Video extends KlCommon {
|
|
|
|
|
String taskId = null;
|
|
|
|
|
boolean accountIssue = false;
|
|
|
|
|
|
|
|
|
|
while (generateRetryCount < maxGenerateRetries && !accountIssue) {
|
|
|
|
|
while (!accountIssue) {
|
|
|
|
|
try {
|
|
|
|
|
taskId = generateVideo(prompt, modelName, externalTaskId);
|
|
|
|
|
break;
|
|
|
|
|