提交数字人接口
parent
d05bbb996c
commit
82b91c6889
@ -0,0 +1,20 @@
|
||||
package com.supervision.manage.controller.human;
|
||||
|
||||
import com.supervision.manage.service.HumanManageService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "数字人管理")
|
||||
public class HumanManageController {
|
||||
|
||||
private final HumanManageService humanManageService;
|
||||
|
||||
@GetMapping("generateHuman")
|
||||
public void generateHuman(String humanId) throws Exception {
|
||||
humanManageService.generateHuman(humanId);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.supervision.manage.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class HumanGenerateDTO {
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
private String uid;
|
||||
|
||||
private String video;
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.supervision.manage.service;
|
||||
|
||||
public interface HumanManageService {
|
||||
|
||||
void generateHuman(String humanId) throws Exception;
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
package com.supervision.manage.service.impl;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.exception.BusinessException;
|
||||
import com.supervision.manage.dto.HumanGenerateDTO;
|
||||
import com.supervision.manage.service.FileManageService;
|
||||
import com.supervision.manage.service.HumanManageService;
|
||||
import com.supervision.model.FileResource;
|
||||
import com.supervision.model.Human;
|
||||
import com.supervision.service.HumanService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class HumanManageServiceImpl implements HumanManageService {
|
||||
|
||||
private final HumanService humanService;
|
||||
private final FileManageService fileManageService;
|
||||
@Value("${humanGenerate.baseUrl}")
|
||||
private String humanBaseUrl;
|
||||
@Value("${humanGenerate.silent}")
|
||||
private String silent;
|
||||
@Value("${humanGenerate.dynamic}")
|
||||
private String dynamic;
|
||||
@Value("${humanGenerate.status}")
|
||||
private String status;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void generateHuman(String humanId) throws Exception {
|
||||
Human human = humanService.getOptById(humanId).orElseThrow(() -> new BusinessException("数字人形象不存在"));
|
||||
|
||||
File imageFile = fileManageService.downloadFile(human.getImageFileId());
|
||||
// 创建一个图片临时文件
|
||||
log.info("提交文件生成,文件名:{}", FileUtil.getName(imageFile));
|
||||
HumanGenerateDTO silentDTO;
|
||||
try {
|
||||
String post = HttpUtil.post(humanBaseUrl + silent, Map.of("image", imageFile));
|
||||
silentDTO = JSONUtil.toBean(post, HumanGenerateDTO.class);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException("生成数字人静态视频异常");
|
||||
}
|
||||
|
||||
HumanGenerateDTO dynamicDTO;
|
||||
try {
|
||||
String post = HttpUtil.post(humanBaseUrl + dynamic, Map.of("image", imageFile));
|
||||
dynamicDTO = JSONUtil.toBean(post, HumanGenerateDTO.class);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException("生成数字人动态视频异常");
|
||||
}
|
||||
// 保存静态视频
|
||||
if (silentDTO.getCode() == 200) {
|
||||
if (StrUtil.isNotBlank(silentDTO.getVideo())) {
|
||||
// 如果已经生成了视频,那么直接拿到这个视频
|
||||
FileResource fileResource = convertBase64ToFile("silent", FileUtil.getName(imageFile), silentDTO.getVideo());
|
||||
human.setSilentVideoFileId(fileResource.getId());
|
||||
|
||||
}
|
||||
human.setSilentTaskUid(silentDTO.getUid());
|
||||
}
|
||||
|
||||
if (dynamicDTO.getCode() == 200) {
|
||||
if (StrUtil.isNotBlank(dynamicDTO.getVideo())) {
|
||||
// 如果已经生成了视频,那么直接拿到这个视频
|
||||
FileResource fileResource = convertBase64ToFile("dynamic", FileUtil.getName(imageFile), dynamicDTO.getVideo());
|
||||
human.setDynamicVideoFileId(fileResource.getId());
|
||||
|
||||
}
|
||||
human.setDynamicTaskUid(silentDTO.getUid());
|
||||
}
|
||||
humanService.updateById(human);
|
||||
}
|
||||
|
||||
private FileResource convertBase64ToFile(String prefixName, String imageFileName, String base64) throws Exception {
|
||||
String prefix = FileUtil.getPrefix(imageFileName);
|
||||
|
||||
// 创建一个图片临时文件
|
||||
File tempFile = FileUtil.createTempFile(prefixName + "_" + prefix, ".mp4", true);
|
||||
|
||||
Base64.decodeToFile(base64, tempFile);
|
||||
return fileManageService.uploadFile(tempFile);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
package com.supervision.manage.task;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.manage.dto.HumanStatusDTO;
|
||||
import com.supervision.manage.service.FileManageService;
|
||||
import com.supervision.model.FileResource;
|
||||
import com.supervision.model.Human;
|
||||
import com.supervision.service.HumanService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class HumanManageTask {
|
||||
|
||||
private final HumanService humanService;
|
||||
private final FileManageService fileManageService;
|
||||
@Value("${humanGenerate.baseUrl}")
|
||||
private String humanBaseUrl;
|
||||
@Value("${humanGenerate.status}")
|
||||
private String status;
|
||||
|
||||
@Scheduled(fixedDelay = 1000 * 60 * 2)
|
||||
public void refreshHumanUid() {
|
||||
log.info("定时任务检查数字人形象");
|
||||
List<Human> humanList = humanService.lambdaQuery().eq(Human::getStatus, 2).list();
|
||||
for (Human human : humanList) {
|
||||
silent:
|
||||
if (StrUtil.isBlank(human.getSilentVideoFileId())) {
|
||||
if (StrUtil.isNotBlank(human.getSilentTaskUid())) {
|
||||
// 如果任务不为空,就去更新任务的状态
|
||||
HumanStatusDTO silentDTO;
|
||||
try {
|
||||
String s = HttpUtil.get(humanBaseUrl + StrUtil.format(status, Map.of("uid", human.getSilentTaskUid())));
|
||||
silentDTO = JSONUtil.toBean(s, HumanStatusDTO.class);
|
||||
log.info("获取静态视频:{}", silentDTO.toString());
|
||||
} catch (Exception e) {
|
||||
log.error("数字人获取静态视频状态失败", e);
|
||||
break silent;
|
||||
}
|
||||
// 任务完成,则进行保存
|
||||
try {
|
||||
if (200 == silentDTO.getCode() && StrUtil.equals("completed", silentDTO.getStatus())
|
||||
&& StrUtil.isNotBlank(silentDTO.getVideo())) {
|
||||
FileResource fileResource = convertBase64ToFile("silent", human.getImageName(), silentDTO.getVideo());
|
||||
// 为最后更新状态准备
|
||||
human.setSilentVideoFileId(fileResource.getId());
|
||||
humanService.lambdaUpdate().set(Human::getSilentVideoFileId, fileResource.getId()).eq(Human::getId, human.getId()).update();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("数字人保存静默视频失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dynamic:
|
||||
if (StrUtil.isBlank(human.getDynamicVideoFileId())) {
|
||||
if (StrUtil.isNotBlank(human.getDynamicTaskUid())) {
|
||||
// 如果任务不为空,就去更新任务的状态
|
||||
HumanStatusDTO dynamicDTO;
|
||||
try {
|
||||
String s = HttpUtil.get(humanBaseUrl + StrUtil.format(status, Map.of("uid", human.getDynamicTaskUid())));
|
||||
dynamicDTO = JSONUtil.toBean(s, HumanStatusDTO.class);
|
||||
log.info("获取动态视频:{}", dynamicDTO.toString());
|
||||
} catch (Exception e) {
|
||||
log.error("数字人获取动态视频状态失败", e);
|
||||
break dynamic;
|
||||
}
|
||||
// 任务完成,则进行保存
|
||||
try {
|
||||
if (200 == dynamicDTO.getCode() && StrUtil.equals("completed", dynamicDTO.getStatus())
|
||||
&& StrUtil.isNotBlank(dynamicDTO.getVideo())) {
|
||||
FileResource fileResource = convertBase64ToFile("dynamic", human.getImageName(), dynamicDTO.getVideo());
|
||||
// 为最后更新状态准备
|
||||
human.setDynamicVideoFileId(fileResource.getId());
|
||||
humanService.lambdaUpdate().set(Human::getDynamicVideoFileId, fileResource.getId()).eq(Human::getId, human.getId()).update();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("数字人保存动态视频失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 如果两者都走完了,校验如果全部有值,说明就完成了
|
||||
if (StrUtil.isAllNotBlank(human.getSilentVideoFileId(), human.getDynamicVideoFileId())) {
|
||||
humanService.lambdaUpdate().set(Human::getStatus, 1).eq(Human::getId, human.getId()).update();
|
||||
}
|
||||
}
|
||||
log.info("定时任务检查数字人形象结束");
|
||||
|
||||
}
|
||||
|
||||
private FileResource convertBase64ToFile(String prefixName, String imageFileName, String base64) throws Exception {
|
||||
String prefix = FileUtil.getPrefix(imageFileName);
|
||||
|
||||
// 创建一个视频临时文件
|
||||
File tempFile = FileUtil.createTempFile(prefixName + "_" + prefix, ".mp4", true);
|
||||
|
||||
Base64.decodeToFile(base64, tempFile);
|
||||
return fileManageService.uploadFile(tempFile);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue