通义万相-文生视频实践
通义万相-文生视频实践
简介
本文主要介绍关于阿里的文生视频的Api的注册和如何使用,详细简介在官网:https://help.aliyun.com/zh/model-studio/text-to-video-api-reference#ecd1180f3c026
注册APIkey
前往阿里的个人中心开通通义灵码服务,没有APi的同学可以创建相应key,并且保存好
pom项目中添加依赖
环境说明:
<java.version>17</java.version>
<spring-boot.version>3.4.7</spring-boot.version>
<spring-cloud.version>2024.0.0</spring-cloud.version>
我这里由于本地有SLF4j的依赖冲突,所以移除了slf4j-simple的依赖,如果不冲突可以不移除。
<dependency><groupId>com.alibaba</groupId><artifactId>dashscope-sdk-java</artifactId><version>2.20.7</version><exclusions><!-- 排除 slf4j-simple --><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId></exclusion></exclusions>
</dependency>
配置文件
/*** @Description: 阿里的apiKey配置中心* @author: zh* @Create : 2025/7/10* @Project_name : RuoYi-Cloud-Plus* @Version :**/
@Component
public class AiGeneralProperties {@Value("${dashscope.apiKey}")public String apiKey;@Value("${dashscope.model}")public String model;
}
配置资源
dashscope:apiKey: sk-xxxxx##对应模型的版本-这个版本可以支持首图和尾图model: wanx2.1-kf2v-plus
对应的Dto类
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;/*** @Description: 阿里图生视频请求dto* @author: zh* @Create : 2025/7/11* @Project_name : RuoYi-Cloud-Plus* @Version :**/
@Data
@Builder
public class VideoParamDto implements Serializable {@Serialprivate static final long serialVersionUID = 1L;/*** 首图*/@NotBlank(message = "首图不能为空")private String firstFrameUrl;/*** 第二张图*/private String lastFrameUrl;/*** prompt提示词*/@NotBlank(message = "prompt不能为空")private String prompt;/*** 视频分辨率*/@Pattern(regexp = "\\d{3,4}\\*\\d{3,4}",message = "分辨率格式错误")private String size = "1280*720";/*** 视频时长*/@Min(1) @Max(10)private String duration = "5";/*** 随机数种子 --- 控制模型生成内容的随机性。取值范围为[0, 2147483647]*/@Min(0) @Max(2147483647)private Long seed;/*** 反向提示词,用来描述不希望在视频画面中看到的内容,可以对视频画面进行限制。*/private String negativePrompt;
}
实现类
/*** @Description: 图片转视频* @author: zh* @Create : 2025/7/10* @Project_name : RuoYi-Cloud-Plus* @Version :**/
@Component
public class PictureToVideoComponent {@Autowiredprivate AiGeneralProperties aiGeneralProperties;private static final VideoSynthesis videoSynthesis = new VideoSynthesis();private static final Map<String, Object> parameters = new HashMap<>();static {parameters.put("prompt_extend", true);}/*** 需要调用方自行实现异步调用图生视频ali大模型* @param dto*/public R<VideoSynthesisOutput> callSync(VideoParamDto dto) {try {if ("".equals(dto.getFirstFrameUrl()) && "".equals(dto.getPrompt())) {return R.fail();}VideoSynthesisParam.VideoSynthesisParamBuilder<?, ?> builder = VideoSynthesisParam.builder().apiKey(aiGeneralProperties.apiKey).model(aiGeneralProperties.model).prompt(dto.getPrompt()).size(dto.getSize()).parameters(parameters);if (dto.getLastFrameUrl() != null && !"".equals(dto.getLastFrameUrl())) {builder.firstFrameUrl(dto.getFirstFrameUrl()).lastFrameUrl(dto.getLastFrameUrl());}else{builder.imgUrl(dto.getFirstFrameUrl());}VideoSynthesisParam param = builder.build();VideoSynthesisResult result = null;result = videoSynthesis.call(param);return R.ok(result.getOutput());} catch (Exception e) {e.printStackTrace();}return R.fail();}
}
测试类
@SpringBootTest(classes = RuoyiAiApplication.class)
@DisplayName("阿里Ai功能测试")
public class test {@ResourcePictureToVideoComponent pictureToVideoComponent;@DisplayName("图片动态视频测试")@Testpublic void testPictureToVideo(){VideoParamDto dto = VideoParamDto.builder().firstFrameUrl("https://typo-img.oss-cn-chengdu.aliyuncs.com/img-localhost/202507162159709.png").lastFrameUrl("https://typo-img.oss-cn-chengdu.aliyuncs.com/img-localhost/202507162200488.jpg").prompt("让男孩跑向女孩").build();System.out.println(pictureToVideoComponent.callSync(dto));}}
测试结果
对应的图片
首图:
尾图:
生成的视频链接放这里了:
https://dental-zh.oss-cn-chengdu.aliyuncs.com/dev/upload/2025-07-17/6878ef1653f666ad84863693