main
HuangHai 2 months ago
parent 0bae093cb3
commit 5b9e3121f2

@ -29,11 +29,10 @@ public class KlImg2Video extends KlCommon {
* *
* @param imageUrl URL * @param imageUrl URL
* @param modelName kling-v1, kling-v1-5, kling-v1-6 * @param modelName kling-v1, kling-v1-5, kling-v1-6
* @param externalTaskId ID
* @return ID * @return ID
* @throws Exception * @throws Exception
*/ */
public static String generateVideo(String imageUrl, String modelName, String externalTaskId) throws Exception { public static String generateVideo(String imageUrl, String modelName) throws Exception {
// 获取JWT令牌 // 获取JWT令牌
String jwt = getJwt(); String jwt = getJwt();
@ -41,12 +40,7 @@ public class KlImg2Video extends KlCommon {
Map<String, Object> requestBody = new HashMap<>(); Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model_name", modelName); requestBody.put("model_name", modelName);
requestBody.put("image", imageUrl); requestBody.put("image", imageUrl);
// 如果提供了外部任务ID则添加到请求体中
if (externalTaskId != null && !externalTaskId.isEmpty()) {
requestBody.put("external_task_id", externalTaskId);
}
// 使用Hutool发送POST请求 // 使用Hutool发送POST请求
HttpResponse response = HttpRequest.post(BASE_URL + GENERATION_PATH) HttpResponse response = HttpRequest.post(BASE_URL + GENERATION_PATH)
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
@ -55,13 +49,10 @@ public class KlImg2Video extends KlCommon {
.execute(); .execute();
log.info("生成视频请求体:{}", JSONUtil.toJsonStr(requestBody)); log.info("生成视频请求体:{}", JSONUtil.toJsonStr(requestBody));
log.info("生成视频响应体:{}", response.body());
// 检查响应状态码 // 检查响应状态码
if (response.getStatus() != 200) { if (response.getStatus() != 200) {
throw new Exception("请求失败,状态码:" + response.getStatus()); throw new Exception("请求失败,状态码:" + response.getStatus());
} }
// 解析响应 // 解析响应
String responseBody = response.body(); String responseBody = response.body();
JSONObject responseJson = JSONObject.parseObject(responseBody); JSONObject responseJson = JSONObject.parseObject(responseBody);
@ -102,28 +93,22 @@ public class KlImg2Video extends KlCommon {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// 确保目录存在
FileUtil.mkdir(basePath);
log.info("保存目录: {}", basePath);
// 图片URL和模型名称 // 图片URL和模型名称
String imageUrl = "https://example.com/image.jpg"; // 替换为实际可访问的图片URL String imageUrl = "https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/202505131058596.png"; // 替换为实际可访问的图片URL
String modelName = "kling-v1"; // 可选kling-v1, kling-v1-5, kling-v1-6 String modelName = "kling-v1"; // 可选kling-v1, kling-v1-5, kling-v1-6
// 生成外部任务ID可选
String externalTaskId = "image2video_" + UUID.randomUUID().toString().replace("-", "");
// 添加重试逻辑 // 添加重试逻辑
int generateRetryCount = 0; int generateRetryCount = 0;
int maxGenerateRetries = 5; // 最大重试次数 int maxGenerateRetries = 1000; // 最大重试次数
int generateRetryInterval = 5000; // 重试间隔(毫秒) int generateRetryInterval = 5000; // 重试间隔(毫秒)
String taskId = null; String taskId = null;
boolean accountIssue = false; boolean accountIssue = false;
while (generateRetryCount < maxGenerateRetries && !accountIssue) { while (!accountIssue) {
try { try {
taskId = generateVideo(imageUrl, modelName, externalTaskId); taskId = generateVideo(imageUrl, modelName);
break; break;
} catch (Exception e) { } catch (Exception e) {
log.error("生成视频异常: {}", e.getMessage(), e); log.error("生成视频异常: {}", e.getMessage(), e);
@ -157,7 +142,7 @@ public class KlImg2Video extends KlCommon {
// 查询任务状态 // 查询任务状态
int queryRetryCount = 0; int queryRetryCount = 0;
int maxQueryRetries = 60; // 最大查询次数 int maxQueryRetries = 1000; // 最大查询次数
int queryRetryInterval = 5000; // 查询间隔(毫秒) int queryRetryInterval = 5000; // 查询间隔(毫秒)
while (queryRetryCount < maxQueryRetries) { while (queryRetryCount < maxQueryRetries) {

Loading…
Cancel
Save