main
HuangHai 2 months ago
parent a43f8f8692
commit 986ad76d42

@ -87,7 +87,7 @@ public class ImgSpeak {
log.info("人脸检测响应: {}", responseBody); log.info("人脸检测响应: {}", responseBody);
JSONObject responseJson = JSON.parseObject(responseBody); JSONObject responseJson = JSON.parseObject(responseBody);
// 检查是否通过人脸检测 // 检查是否通过人脸检测
JSONObject output = responseJson.getJSONObject("output"); JSONObject output = responseJson.getJSONObject("output");
if (output != null && !output.getBooleanValue("pass")) { if (output != null && !output.getBooleanValue("pass")) {
@ -96,31 +96,31 @@ public class ImgSpeak {
log.error(errorMsg); log.error(errorMsg);
throw new Exception(errorMsg); throw new Exception(errorMsg);
} }
return responseJson; return responseJson;
} }
/** /**
* LivePortraitAPI * LivePortraitAPI
* *
* @param imageUrl URL * @param imageUrl URL
* @param audioUrl URL * @param audioUrl URL
* @param templateId IDnormal, dance, rap * @param templateId IDnormal, dance, rap
* @param eyeMoveFreq 0-1 * @param eyeMoveFreq 0-1
* @param videoFps 30 * @param videoFps 30
* @param mouthMoveStrength 0-1 * @param mouthMoveStrength 0-1
* @param pasteBack truefalse * @param pasteBack truefalse
* @param headMoveStrength 0-1 * @param headMoveStrength 0-1
* @return ID * @return ID
* @throws Exception * @throws Exception
*/ */
@SneakyThrows @SneakyThrows
public static String synthesisVideo(String imageUrl, String audioUrl, String templateId, public static String synthesisVideo(String imageUrl, String audioUrl, String templateId,
double eyeMoveFreq, int videoFps, double mouthMoveStrength, double eyeMoveFreq, int videoFps, double mouthMoveStrength,
boolean pasteBack, double headMoveStrength) { boolean pasteBack, double headMoveStrength) {
// 先进行人脸检测 // 先进行人脸检测
detectFace(imageUrl); detectFace(imageUrl);
// 创建OkHttpClient设置超时时间 // 创建OkHttpClient设置超时时间
OkHttpClient client = new OkHttpClient().newBuilder() OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS)
@ -287,7 +287,7 @@ public class ImgSpeak {
String imageUrl = "https://dsideal.obs.myhuaweicloud.com/HuangHai/%E5%A4%87%E4%BB%BD/p874897.png"; String imageUrl = "https://dsideal.obs.myhuaweicloud.com/HuangHai/%E5%A4%87%E4%BB%BD/p874897.png";
// 音频URL音频 // 音频URL音频
String audioUrl = "https://dsideal.obs.myhuaweicloud.com/HuangHai/%E5%A4%87%E4%BB%BD/p874897.wav"; String audioUrl = "https://dsideal.obs.myhuaweicloud.com/HuangHai/%E5%A4%87%E4%BB%BD/p874897.wav";
// 模板ID - 使用模板 // 模板ID - 使用模板
String templateId = "sing"; // 可选值normal, dance, rap, sing等 String templateId = "sing"; // 可选值normal, dance, rap, sing等
// 眼睛移动频率 // 眼睛移动频率
@ -302,9 +302,9 @@ public class ImgSpeak {
double headMoveStrength = 0.7; double headMoveStrength = 0.7;
// 调用灵动人像LivePortrait视频合成API // 调用灵动人像LivePortrait视频合成API
String taskId = synthesisVideo(imageUrl, audioUrl, templateId, eyeMoveFreq, String taskId = synthesisVideo(imageUrl, audioUrl, templateId, eyeMoveFreq,
videoFps, mouthMoveStrength, pasteBack, headMoveStrength); videoFps, mouthMoveStrength, pasteBack, headMoveStrength);
// 轮询查询任务状态 // 轮询查询任务状态
int maxRetries = 100; int maxRetries = 100;
int retryCount = 0; int retryCount = 0;
@ -320,12 +320,11 @@ public class ImgSpeak {
// 任务成功获取视频URL // 任务成功获取视频URL
videoUrl = result.getJSONObject("output").getJSONObject("results").getString("video_url"); videoUrl = result.getJSONObject("output").getJSONObject("results").getString("video_url");
log.info("生成的视频URL: {}", videoUrl); log.info("生成的视频URL: {}", videoUrl);
// 获取视频时长和比例信息 // 获取视频时长和比例信息
double videoDuration = result.getJSONObject("usage").getDoubleValue("video_duration"); double videoDuration = result.getJSONObject("usage").getDoubleValue("video_duration");
String videoRatio = result.getJSONObject("usage").getString("video_ratio"); String videoRatio = result.getJSONObject("usage").getString("video_ratio");
log.info("视频时长: {}秒, 视频比例: {}", videoDuration, videoRatio); log.info("视频时长: {}秒, 视频比例: {}", videoDuration, videoRatio);
break; break;
} else if ("FAILED".equals(status)) { } else if ("FAILED".equals(status)) {
// 任务失败 // 任务失败
@ -343,7 +342,7 @@ public class ImgSpeak {
if (retryCount >= maxRetries) { if (retryCount >= maxRetries) {
log.error("查询任务状态超时,已达到最大重试次数: {}", maxRetries); log.error("查询任务状态超时,已达到最大重试次数: {}", maxRetries);
} }
// 如果获取到了视频URL则下载保存 // 如果获取到了视频URL则下载保存
if (videoUrl != null && !videoUrl.isEmpty()) { if (videoUrl != null && !videoUrl.isEmpty()) {
String fileName = "liveportrait_sing_" + System.currentTimeMillis() + "_" + taskId + ".mp4"; String fileName = "liveportrait_sing_" + System.currentTimeMillis() + "_" + taskId + ".mp4";

Loading…
Cancel
Save