|
|
|
@ -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);
|
|
|
|
|