提交修改
This commit is contained in:
parent
87a8c9bb2a
commit
d648093ab5
127
api-third/src/main/java/com/heyu/api/alibaba/Image2Video.java
Normal file
127
api-third/src/main/java/com/heyu/api/alibaba/Image2Video.java
Normal file
@ -0,0 +1,127 @@
|
||||
|
||||
package com.heyu.api.alibaba;// Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
|
||||
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesis;
|
||||
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisParam;
|
||||
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisResult;
|
||||
import com.alibaba.dashscope.exception.ApiException;
|
||||
import com.alibaba.dashscope.exception.InputRequiredException;
|
||||
import com.alibaba.dashscope.exception.NoApiKeyException;
|
||||
import com.alibaba.dashscope.utils.JsonUtils;
|
||||
import com.alibaba.dashscope.utils.Constants;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 环境要求
|
||||
* dashscope java SDK >= 2.20.9
|
||||
* 更新maven依赖:
|
||||
* https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java
|
||||
*/
|
||||
|
||||
public class Image2Video {
|
||||
|
||||
static {
|
||||
// 以下为北京地域url,若使用新加坡地域的模型,需将url替换为:https://dashscope-intl.aliyuncs.com/api/v1
|
||||
Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";
|
||||
}
|
||||
|
||||
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey="sk-xxx"
|
||||
// 新加坡和北京地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key
|
||||
//static String apiKey = System.getenv("DASHSCOPE_API_KEY");
|
||||
|
||||
/**
|
||||
* 图像输入方式说明:三选一即可
|
||||
*
|
||||
* 1. 使用公网URL - 适合已有公开可访问的图片
|
||||
* 2. 使用本地文件 - 适合本地开发测试
|
||||
* 3. 使用Base64编码 - 适合私有图片或需要加密传输的场景
|
||||
*/
|
||||
|
||||
//【方式一】公网URL
|
||||
static String imgUrl = "https://gw.alicdn.com/bao/uploaded/i4/2219725998116/O1CN01jsogzI29pBWXEdU7M_!!2219725998116.jpg_.webp";
|
||||
|
||||
//【方式二】本地文件路径(file://+绝对路径)
|
||||
// static String imgUrl = "file://" + "/your/path/to/img.png"; // Linux/macOS
|
||||
// static String imgUrl = "file://" + "C:/your/path/to/img.png"; // Windows
|
||||
|
||||
//【方式三】Base64编码
|
||||
// static String imgUrl = Image2Video.encodeFile("/your/path/to/img.png");
|
||||
|
||||
public static void image2video() throws ApiException, NoApiKeyException, InputRequiredException {
|
||||
// 设置parameters参数
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
/***
|
||||
* 是否开启prompt智能改写。开启后使用大模型对输入prompt进行智能改写。对于较短的prompt生成效果提升明显,但会增加耗时。
|
||||
*
|
||||
* true:默认值,开启智能改写。
|
||||
*
|
||||
* false:不开启智能改写。
|
||||
*
|
||||
* 示例值:true。
|
||||
*/
|
||||
parameters.put("prompt_extend", true);
|
||||
|
||||
VideoSynthesis vs = new VideoSynthesis();
|
||||
VideoSynthesisParam param =
|
||||
VideoSynthesisParam.builder()
|
||||
.apiKey(LLMUtils.apiKey)
|
||||
.model("wan2.2-i2v-plus")
|
||||
.prompt("基于项链生成一个商品宣传视频")
|
||||
.imgUrl(imgUrl)
|
||||
.parameters(parameters)
|
||||
.resolution("1080P")
|
||||
.build();
|
||||
System.out.println("please wait...");
|
||||
VideoSynthesisResult result = vs.call(param);
|
||||
System.out.println(JsonUtils.toJson(result));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件编码为Base64字符串
|
||||
* @param filePath 文件路径
|
||||
* @return Base64字符串,格式为 data:{MIME_type};base64,{base64_data}
|
||||
*/
|
||||
public static String encodeFile(String filePath) {
|
||||
Path path = Paths.get(filePath);
|
||||
if (!Files.exists(path)) {
|
||||
throw new IllegalArgumentException("文件不存在: " + filePath);
|
||||
}
|
||||
// 检测MIME类型
|
||||
String mimeType = null;
|
||||
try {
|
||||
mimeType = Files.probeContentType(path);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("无法检测文件类型: " + filePath);
|
||||
}
|
||||
if (mimeType == null || !mimeType.startsWith("image/")) {
|
||||
throw new IllegalArgumentException("不支持或无法识别的图像格式");
|
||||
}
|
||||
// 读取文件内容并编码
|
||||
byte[] fileBytes = null;
|
||||
try{
|
||||
fileBytes = Files.readAllBytes(path);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("无法读取文件内容: " + filePath);
|
||||
}
|
||||
|
||||
String encodedString = Base64.getEncoder().encodeToString(fileBytes);
|
||||
return "data:" + mimeType + ";base64," + encodedString;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
image2video();
|
||||
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user