main
HuangHai 2 months ago
parent ecfd53f160
commit 4b52e82fb4

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

Loading…
Cancel
Save