main
HuangHai 2 months ago
parent a825369b3c
commit 2abf536103

@ -44,7 +44,10 @@ public class JmCommon {
protected static final String ak; // Access Key
protected static final String sk; // Secret Key
public static Prop PropKit; // 配置文件工具
// 获取项目根目录路径
protected static String projectRoot = System.getProperty("user.dir").replace("\\","/")+"/dsAiSupport";
// 拼接相对路径
protected static String basePath = projectRoot + "/src/main/java/com/dsideal/aiSupport/Util/JiMeng/Example/";
/**
*
* URL

@ -46,10 +46,6 @@ public class JmText2Image extends JmCommon {
//String prompt = "制作一张vlog视频封面。马卡龙配色美女旅游照片+色块的拼贴画风格主文案是“威海旅游vlog”副文案是“特种兵一日游 被低估的旅游城市”,海报主体是一个穿着短裙、梳双马尾的少女,人物白色描边";
//String prompt = "过曝强对比夜晚雪地里巨大的黄色浴缸小狗泡澡带墨镜在喝红酒胶片摄影毛刺质感复古滤镜夜晚过度曝光古早70年代摄影复古老照片闪光灯拍摄闪光灯效果过曝过度曝光闪光灯过曝极简高饱和复古色70s vintage photography, vintage, retro style";
String prompt="南瓜羹->画面展现一碗百合南瓜羹的一半,米黄色的糯米粉勾芡,块块橙色南瓜在橙色粥中,南瓜丝丝沙沙质感,紫白色百合点缀";
// 获取项目根目录路径
String projectRoot = System.getProperty("user.dir").replace("\\","/")+"/dsAiSupport";
// 拼接相对路径
String basePath = projectRoot + "/src/main/java/com/dsideal/aiSupport/Util/JiMeng/Example/";
String saveImagePath = basePath + "3.jpg";
log.info("保存图片路径:" + saveImagePath);
JmText2Image.generateImage(prompt, saveImagePath);

@ -6,6 +6,10 @@ import com.jfinal.kit.StrKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
public class JmText2Video extends JmCommon {
@ -28,25 +32,66 @@ public class JmText2Video extends JmCommon {
return JSON.parseObject(responseBody);
}
/**
* URL
*
* @param fileUrl URL
* @param saveFilePath
* @throws Exception
*/
public static void downloadFile(String fileUrl, String saveFilePath) throws Exception {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(60000);
// 获取输入流
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);
throw e;
} finally {
connection.disconnect();
}
}
public static void main(String[] args) throws Exception {
String prompt = "蓝色毛绒玩具在超市里拖地,结果拖把洒出好多五颜六色的粉末,接着把粉末洒向镜头前,镜头随之穿过粉末";
//保存的文件名称
String mp4FileName = "1.mp4";
JSONObject jo = JmText2Video.generateVideo(prompt);
log.info("结果:{}", jo);
String taskId = jo.getJSONObject("data").getString("task_id");
//检查任务是不是已经结束
while (true) {
JSONObject result = JmCommon.queryTaskResult(taskId);
JSONObject result = queryTaskResult(taskId);
log.info("查询结果:{}", result);
if (result.getInteger("code") != 10000) {
log.error("查询失败,错误代码=" + result.getInteger("code"));
break;
}
JSONObject data = result.getJSONObject("data");
if (!StrKit.isBlank(data.getString("video_url"))) {
String video_url = data.getString("video_url");
log.info("视频地址:{}", video_url);
//下载mp4
String saveVideoPath = basePath + mp4FileName;
downloadFile(video_url, saveVideoPath);
log.info("视频已下载到: {}", saveVideoPath);
break;
} else {
Thread.sleep(3000);

Loading…
Cancel
Save