main
HuangHai 2 months ago
parent ecfd53f160
commit 4b52e82fb4

@ -25,7 +25,7 @@ public class JmImage2Video extends JmCommon {
* 使URL
*
* @param imageUrls URL
* @param prompt
* @param prompt
* @return
* @throws Exception
*/
@ -46,8 +46,8 @@ public class JmImage2Video extends JmCommon {
/**
* URL
*
* @param fileUrl URL
*
* @param fileUrl URL
* @param saveFilePath
* @throws Exception
*/
@ -57,7 +57,7 @@ public class JmImage2Video extends JmCommon {
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(60000);
// 确保目录存在
File file = new File(saveFilePath);
File parentDir = file.getParentFile();
@ -65,19 +65,19 @@ public class JmImage2Video extends JmCommon {
parentDir.mkdirs();
log.info("创建目录: {}", parentDir.getAbsolutePath());
}
// 获取输入流
try (InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(saveFilePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
// 读取数据并写入文件
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
log.info("文件下载成功,保存路径: {}", saveFilePath);
} catch (Exception e) {
log.error("文件下载失败: {}", e.getMessage(), e);
@ -88,23 +88,25 @@ public class JmImage2Video extends JmCommon {
}
public static void main(String[] args) throws Exception {
//玩法:
//https://www.volcengine.com/docs/85621/1544774
List<String> imageUrls = new ArrayList<>();
imageUrls.add("https://dsideal.obs.myhuaweicloud.com/HuangHai/%E5%A4%87%E4%BB%BD/%E5%B0%8F%E4%B9%94%E5%A4%B4%E5%83%8F.jpg");
String prompt = "";
String mp4FileName = "image2video.mp4";
// 添加重试逻辑处理API并发限制错误
JSONObject submitResult = null;
int submitRetryCount = 0;
int maxSubmitRetries = 1000; // 最大重试次数
int submitRetryInterval = 3000; // 重试间隔(毫秒)
while (submitRetryCount < maxSubmitRetries) {
try {
submitResult = submitImageToVideoTaskWithUrls(imageUrls, prompt);
log.info("提交结果: {}", submitResult);
int code = submitResult.getInteger("code");
if (code == JmErrorCode.API_CONCURRENT_LIMIT.getCode()) {
log.warn("API并发限制等待{}毫秒后重试...", submitRetryInterval);
@ -115,7 +117,7 @@ public class JmImage2Video extends JmCommon {
log.error("提交任务失败: 错误码={}, 错误信息={}", code, JmErrorCode.getMessageByCode(code));
return;
}
// 成功获取结果,跳出循环
break;
} catch (Exception e) {
@ -129,36 +131,36 @@ public class JmImage2Video extends JmCommon {
}
}
}
if (submitRetryCount >= maxSubmitRetries) {
log.error("提交任务失败,已达到最大重试次数: {}", maxSubmitRetries);
return;
}
// 获取任务ID
String taskId = submitResult.getJSONObject("data").getString("task_id");
log.info("任务ID: {}", taskId);
// 检查任务是不是已经结束
int queryRetryCount = 0;
int maxQueryRetries = 10000; // 最大查询次数
int queryRetryInterval = 3000; // 查询间隔(毫秒)
while (queryRetryCount < maxQueryRetries) {
JSONObject result = queryTaskResult(taskId);
log.info("查询结果: {}", result);
int code = result.getInteger("code");
if (!JmErrorCode.isSuccess(code)) {
log.error("查询失败: 错误码={}, 错误信息={}", code, JmErrorCode.getMessageByCode(code));
break;
}
JSONObject data = result.getJSONObject("data");
if (data != null && data.getString("video_url") != null && !data.getString("video_url").isEmpty()) {
String videoUrl = data.getString("video_url");
log.info("视频地址: {}", videoUrl);
// 下载视频
String saveVideoPath = basePath + mp4FileName;
log.info("开始下载视频...");
@ -171,7 +173,7 @@ public class JmImage2Video extends JmCommon {
queryRetryCount++;
}
}
if (queryRetryCount >= maxQueryRetries) {
log.error("任务查询超时,已达到最大查询次数: {}", maxQueryRetries);
}

Loading…
Cancel
Save