|
|
|
@ -67,37 +67,79 @@ public class JmText2Video extends JmCommon {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询任务结果
|
|
|
|
|
*
|
|
|
|
|
* @param taskId 任务ID
|
|
|
|
|
* @return 任务结果
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject queryTaskResult(String taskId) {
|
|
|
|
|
return queryTaskResult(taskId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
String prompt = "蓝色毛绒玩具在超市里拖地,结果拖把洒出好多五颜六色的粉末,接着把粉末洒向镜头前,镜头随之穿过粉末";
|
|
|
|
|
//保存的文件名称
|
|
|
|
|
String mp4FileName = "1.mp4";
|
|
|
|
|
|
|
|
|
|
// 获取项目根目录路径
|
|
|
|
|
String projectRoot = System.getProperty("user.dir");
|
|
|
|
|
// 拼接相对路径
|
|
|
|
|
String basePath = projectRoot + "/src/main/java/com/dsideal/aiSupport/Util/JiMeng/Example/";
|
|
|
|
|
|
|
|
|
|
// 确保目录存在
|
|
|
|
|
java.io.File dir = new java.io.File(basePath);
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
dir.mkdirs();
|
|
|
|
|
log.info("创建目录: {}", basePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSONObject jo = JmText2Video.generateVideo(prompt);
|
|
|
|
|
log.info("结果:{}", jo);
|
|
|
|
|
|
|
|
|
|
int code = jo.getInteger("code");
|
|
|
|
|
if (!JmErrorCode.isSuccess(code)) {
|
|
|
|
|
log.error("生成视频失败: 错误码={}, 错误信息={}", code, JmErrorCode.getMessageByCode(code));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String taskId = jo.getJSONObject("data").getString("task_id");
|
|
|
|
|
log.info("任务ID: {}", taskId);
|
|
|
|
|
|
|
|
|
|
//检查任务是不是已经结束
|
|
|
|
|
while (true) {
|
|
|
|
|
int retryCount = 0;
|
|
|
|
|
int maxRetries = 30; // 最大重试次数
|
|
|
|
|
int retryInterval = 3000; // 重试间隔(毫秒)
|
|
|
|
|
|
|
|
|
|
while (retryCount < maxRetries) {
|
|
|
|
|
JSONObject result = queryTaskResult(taskId);
|
|
|
|
|
log.info("查询结果:{}", result);
|
|
|
|
|
if (result.getInteger("code") != 10000) {
|
|
|
|
|
log.error("查询失败,错误代码=" + result.getInteger("code"));
|
|
|
|
|
|
|
|
|
|
code = result.getInteger("code");
|
|
|
|
|
if (!JmErrorCode.isSuccess(code)) {
|
|
|
|
|
log.error("查询失败: 错误码={}, 错误信息={}", code, JmErrorCode.getMessageByCode(code));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSONObject data = result.getJSONObject("data");
|
|
|
|
|
if (!StrKit.isBlank(data.getString("video_url"))) {
|
|
|
|
|
if (data != null && !StrKit.isBlank(data.getString("video_url"))) {
|
|
|
|
|
String video_url = data.getString("video_url");
|
|
|
|
|
log.info("视频地址:{}", video_url);
|
|
|
|
|
//下载mp4
|
|
|
|
|
String saveVideoPath = basePath + mp4FileName;
|
|
|
|
|
log.info("开始下载视频...");
|
|
|
|
|
log.info("basePath: {}", basePath);
|
|
|
|
|
downloadFile(video_url, saveVideoPath);
|
|
|
|
|
log.info("视频已下载到: {}", saveVideoPath);
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
Thread.sleep(3000);
|
|
|
|
|
log.info("任务处理中,等待{}毫秒后重试...", retryInterval);
|
|
|
|
|
Thread.sleep(retryInterval);
|
|
|
|
|
retryCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (retryCount >= maxRetries) {
|
|
|
|
|
log.error("任务查询超时,已达到最大重试次数: {}", maxRetries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|