|
|
|
@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 可灵AI文生视频工具类
|
|
|
|
@ -28,11 +27,10 @@ public class KlText2Video extends KlCommon {
|
|
|
|
|
*
|
|
|
|
|
* @param prompt 提示词
|
|
|
|
|
* @param modelName 模型名称,枚举值:kling-v1, kling-v1-6
|
|
|
|
|
* @param externalTaskId 外部任务ID,可为空
|
|
|
|
|
* @return 任务ID
|
|
|
|
|
* @throws Exception 异常信息
|
|
|
|
|
*/
|
|
|
|
|
public static String generateVideo(String prompt, String modelName, String externalTaskId) throws Exception {
|
|
|
|
|
public static String generateVideo(String prompt, String modelName) throws Exception {
|
|
|
|
|
// 获取JWT令牌
|
|
|
|
|
String jwt = getJwt();
|
|
|
|
|
|
|
|
|
@ -40,12 +38,7 @@ public class KlText2Video extends KlCommon {
|
|
|
|
|
Map<String, Object> requestBody = new HashMap<>();
|
|
|
|
|
requestBody.put("model_name", modelName);
|
|
|
|
|
requestBody.put("prompt", prompt);
|
|
|
|
|
|
|
|
|
|
// 如果提供了外部任务ID,则添加到请求体中
|
|
|
|
|
if (externalTaskId != null && !externalTaskId.isEmpty()) {
|
|
|
|
|
requestBody.put("external_task_id", externalTaskId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 使用Hutool发送POST请求
|
|
|
|
|
HttpResponse response = HttpRequest.post(BASE_URL + GENERATION_PATH)
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
@ -54,13 +47,10 @@ public class KlText2Video extends KlCommon {
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
log.info("生成视频请求体:{}", JSONUtil.toJsonStr(requestBody));
|
|
|
|
|
log.info("生成视频响应体:{}", response.body());
|
|
|
|
|
|
|
|
|
|
// 检查响应状态码
|
|
|
|
|
if (response.getStatus() != 200) {
|
|
|
|
|
throw new Exception("请求失败,状态码:" + response.getStatus());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析响应
|
|
|
|
|
String responseBody = response.body();
|
|
|
|
|
JSONObject responseJson = JSONUtil.parseObj(responseBody);
|
|
|
|
@ -128,16 +118,12 @@ public class KlText2Video extends KlCommon {
|
|
|
|
|
return responseJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下载函数已移至KlCommon类
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
// 提示词和模型名称
|
|
|
|
|
String prompt = "一只可爱的小猫咪在草地上奔跑,阳光明媚";
|
|
|
|
|
String modelName = "kling-v1"; // 可选:kling-v1, kling-v1-6
|
|
|
|
|
|
|
|
|
|
// 生成外部任务ID(可选)
|
|
|
|
|
String externalTaskId = "video_" + UUID.randomUUID().toString().replace("-", "");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加重试逻辑
|
|
|
|
|
int generateRetryCount = 0;
|
|
|
|
|
int maxGenerateRetries = 5; // 最大重试次数
|
|
|
|
@ -148,7 +134,7 @@ public class KlText2Video extends KlCommon {
|
|
|
|
|
|
|
|
|
|
while (!accountIssue) {
|
|
|
|
|
try {
|
|
|
|
|
taskId = generateVideo(prompt, modelName, externalTaskId);
|
|
|
|
|
taskId = generateVideo(prompt, modelName);
|
|
|
|
|
break;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("生成视频异常: {}", e.getMessage(), e);
|
|
|
|
|