后台服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
12 KiB

  1. package com.iformall.sm;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.iformall.common.ErrorCode;
  6. import com.iformall.douyin.pay.orderQuery.OrderQueryResult;
  7. import com.iformall.douyin.pay.orderQuery.QueryMerchantResult;
  8. import com.iformall.douyin.pay.orderQuery.QueryRefundResult;
  9. import com.iformall.douyin.pay.orderQuery.QuerySettleResult;
  10. import com.iformall.douyin.pay.preOrder.*;
  11. import com.iformall.enums.EnumMouldPatchType;
  12. import com.iformall.enums.EnumPayStatus;
  13. import com.iformall.enums.EnumVideoType;
  14. import com.iformall.exception.MallinkException;
  15. import com.iformall.utils.Base64Util;
  16. import com.iformall.utils.HttpUtil;
  17. import com.iformall.utils.sign.SignUtils;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang3.StringUtils;
  20. import java.nio.charset.StandardCharsets;
  21. import java.security.MessageDigest;
  22. import java.security.NoSuchAlgorithmException;
  23. import java.util.*;
  24. @Slf4j
  25. public class AiVideoHelper {
  26. // public static String uri = "http://222.128.9.132:22266";
  27. // http://nas.pucao.cn:2001/gen_dh_video
  28. public static String uri = "http://nas.pucao.cn:2001";
  29. public static String url = "http://nas.pucao.cn:2002";
  30. public static String hy_url = "http://nas.pucao.cn:2002";
  31. // public static String url = "http://111.198.0.15:22299";
  32. // public static String hy_url = "http://111.198.0.15:22288";
  33. public static String photo_speak_suffix = "/img_talking";
  34. public static String image_quality_suffix = "/image_qualit";
  35. public static String voice_preview = "/tts_wav";
  36. public static String video_hq = "/video_hq";
  37. public static String doPost(String url, String params) {
  38. return HttpUtil.doAiVideoPost(url,params);
  39. }
  40. public static AiVideoResult createVideo(AiVideoParam videoParam) {
  41. log.info("生成视频start request:" + videoParam.neglectImgString());
  42. String response = doPost(uri+"/gen_dh_video", JSONObject.toJSONString(videoParam));
  43. log.info("生成视频end response:"+response);
  44. AiVideoResult result = new AiVideoResult();
  45. if(StringUtils.isBlank(response)){
  46. result.setSuccess(false);
  47. result.setMsg("请求生成视频异常,请稍后重试");
  48. return result;
  49. }
  50. JSONObject jsonObject = JSON.parseObject(response);
  51. JSONObject status = jsonObject.getJSONObject("status");
  52. Integer code = status.getInteger("code");
  53. if(code == null){
  54. result.setSuccess(false);
  55. result.setMsg("请求生成视频异常,请稍后重试");
  56. return result;
  57. }
  58. if(code.intValue() == 1000){
  59. JSONObject data = jsonObject.getJSONObject("data");
  60. JSONObject video = data.getJSONObject("video");
  61. result.setSuccess(true);
  62. result.setMsg("success");
  63. result.setDuration(video.getFloatValue("duration"));
  64. result.setUrl(video.getString("url"));
  65. }else{
  66. result.setSuccess(false);
  67. result.setMsg(status.getString("msg"));
  68. }
  69. return result;
  70. // Integer code = jsonObject.getInteger("err_no");
  71. // if (null != code && code.intValue() == 0 ) {
  72. // CreatePreOrderResult result = new CreatePreOrderResult();
  73. // result.setSuccess(true);
  74. // result.setOutOrderNo(preOrder.getOutOrderNo());
  75. // JSONObject data = jsonObject.getJSONObject("data");
  76. // if (null != data ) {
  77. // result.setOrderId(data.getString("order_id"));
  78. // result.setToken(data.getString("order_token"));
  79. // }else {
  80. // log.error("createPreOrder reponse empity. request: "+JSON.toJSONString(preOrder.toRequestMap())+" response:"+response);
  81. // result.setSuccess(false);
  82. // result.setMsg("createPreOrder reponse empity."+response);
  83. // }
  84. // return result;
  85. // }else {
  86. // log.error("createPreOrder reponse error. request: "+JSON.toJSONString(preOrder.toRequestMap())+" response:"+response);
  87. // CreatePreOrderResult result = new CreatePreOrderResult();
  88. // result.setSuccess(false);
  89. // result.setMsg("createPreOrder reponse error."+response);
  90. // return result;
  91. // }
  92. }
  93. public static AiPhotoSpeakResult createPhotoSpeakVideo(AiPhotoSpeakParam videoParam) {
  94. String response = doPost(url + photo_speak_suffix, JSONObject.toJSONString(videoParam));
  95. log.info("生成视频end response:" + response);
  96. AiPhotoSpeakResult result = new AiPhotoSpeakResult();
  97. if (StringUtils.isBlank(response)) {
  98. result.setSuccess(false);
  99. result.setMsg("(MetaService)服务被Avatar攻击... 点击“编辑”重试");
  100. return result;
  101. }
  102. JSONObject jsonObject = JSON.parseObject(response);
  103. JSONObject status = jsonObject.getJSONObject("status");
  104. Integer code = status.getInteger("code");
  105. String msg = status.getString("msg");
  106. if (code == null) {
  107. result.setSuccess(false);
  108. result.setMsg("(MetaService)服务被Avatar攻击... 点击“编辑”重试");
  109. return result;
  110. }
  111. if (code.intValue() == 4000) {
  112. JSONObject data = jsonObject.getJSONObject("data");
  113. String videoUrl = data.getString("url");
  114. String saveDir = data.getString("save_dir");
  115. String audioPath = data.getString("audio_path");
  116. result.setSuccess(true);
  117. result.setUrl(videoUrl);
  118. result.setSaveDir(saveDir);
  119. result.setCode(code);
  120. result.setAudioPath(audioPath);
  121. String resultMsg = result.getMsgInfo(code, msg);
  122. result.setMsg(resultMsg);
  123. } else {
  124. result.setSuccess(false);
  125. result.setCode(code);
  126. String resultMsg = result.getMsgInfo(code, msg);
  127. result.setMsg(resultMsg);
  128. }
  129. return result;
  130. }
  131. public static AiCheckPhotoResult checkPhoto(AiCheckPhotoParam param) {
  132. String response = doPost(url + image_quality_suffix, JSONObject.toJSONString(param));
  133. log.info("图片质量审核 end response:" + response);
  134. AiCheckPhotoResult result = new AiCheckPhotoResult();
  135. if (StringUtils.isBlank(response)) {
  136. result.setSuccess(false);
  137. result.setMsg("(MetaService)图片质量审核失败,请稍后重试");
  138. return result;
  139. }
  140. JSONObject jsonObject = JSON.parseObject(response);
  141. JSONObject status = jsonObject.getJSONObject("status");
  142. Integer code = status.getInteger("code");
  143. String msg = status.getString("msg");
  144. if (code == null) {
  145. result.setSuccess(false);
  146. result.setMsg("(MetaService)请求图片质量检查失败,请稍后重试");
  147. return result;
  148. }
  149. if (code.intValue() == 2000) {
  150. result.setCode(code);
  151. result.setSuccess(true);
  152. String resultMsg = result.getMsgInfo(code, msg);
  153. result.setMsg(resultMsg);
  154. } else {
  155. result.setCode(code);
  156. result.setSuccess(false);
  157. String resultMsg = result.getMsgInfo(code, msg);
  158. result.setMsg(resultMsg);
  159. }
  160. return result;
  161. }
  162. public static AiPreviewResult voicePreview(AiPreviewParam param) {
  163. String response = doPost(url + voice_preview, JSONObject.toJSONString(param));
  164. log.info("TTS音色预览 end response:" + response);
  165. AiPreviewResult result = new AiPreviewResult();
  166. if (StringUtils.isBlank(response)) {
  167. result.setSuccess(false);
  168. result.setMsg("(MetaService)TTS音色预览失败,请稍后重试");
  169. return result;
  170. }
  171. JSONObject jsonObject = JSON.parseObject(response);
  172. JSONObject status = jsonObject.getJSONObject("status");
  173. JSONObject data = jsonObject.getJSONObject("data");
  174. String strURL = null;
  175. String time = null;
  176. if (data != null){
  177. strURL = data.getString("url");
  178. time = data.getString("time");
  179. }
  180. Integer code = status.getInteger("code");
  181. String msg = status.getString("msg");
  182. if (code == null) {
  183. result.setSuccess(false);
  184. result.setMsg("(MetaService)TTS音色预览异常,请稍后重试");
  185. return result;
  186. }
  187. if (code.intValue() == 3000) {
  188. result.setCode(200);
  189. result.setSuccess(true);
  190. result.setUrl(url + strURL);
  191. result.setTime(Double.valueOf(time));
  192. String resultMsg = result.getMsgInfo(code, msg);
  193. result.setMsg(resultMsg);
  194. } else {
  195. result.setCode(code);
  196. result.setSuccess(false);
  197. String resultMsg = result.getMsgInfo(code, msg);
  198. result.setMsg(resultMsg);
  199. }
  200. return result;
  201. }
  202. public static AiVideoHqResult videoHq(AiVideoHqParam param) {
  203. String response = doPost(hy_url + video_hq, JSONObject.toJSONString(param));
  204. log.info("视频超分 end response:" + response);
  205. AiVideoHqResult result = new AiVideoHqResult();
  206. if (StringUtils.isBlank(response)) {
  207. result.setSuccess(false);
  208. result.setMsg("(MetaService)视频超分失败,请稍后重试");
  209. return result;
  210. }
  211. JSONObject jsonObject = JSON.parseObject(response);
  212. JSONObject status = jsonObject.getJSONObject("status");
  213. JSONObject data = jsonObject.getJSONObject("data");
  214. String strURL = null;
  215. if (data != null){
  216. strURL = data.getString("url");
  217. }
  218. Integer code = status.getInteger("code");
  219. String msg = status.getString("msg");
  220. if (code == null) {
  221. result.setSuccess(false);
  222. result.setMsg("(MetaService)视频超分异常,请稍后重试");
  223. return result;
  224. }
  225. if (code.intValue() == 3000) {
  226. result.setCode(200);
  227. result.setSuccess(true);
  228. result.setUrl(url + strURL);
  229. String resultMsg = result.getMsgInfo(code, msg);
  230. result.setMsg(resultMsg);
  231. } else {
  232. result.setCode(code);
  233. result.setSuccess(false);
  234. String resultMsg = result.getMsgInfo(code, msg);
  235. result.setMsg(resultMsg);
  236. }
  237. return result;
  238. }
  239. public static void main(String[] args) {
  240. // AiVideoParam videoParam = new AiVideoParam();
  241. // videoParam.setGen_txt("我写了一篇小说,你帮我看看。");
  242. // videoParam.setVideo_template_id("16776650495633934_PgNAW8Sm");
  243. // videoParam.setVoice_id("zh-CN-XiaohanNeural");
  244. // videoParam.setVoice_style("default");
  245. // AiVideoParam.VideoFiles videoFiles = new AiVideoParam.VideoFiles();
  246. // AiVideoParam.BackGround backGround = new AiVideoParam.BackGround();
  247. // backGround.setType("vertical");
  248. // backGround.setImage(Base64Util.imageUrlToBase64("https://suimang.oss-accelerate.aliyuncs.com/builtin/background/grace/1080.jpg"));
  249. // videoFiles.setBack_ground(backGround);
  250. // AiVideoParam.Material digitalHuman = new AiVideoParam.Material();
  251. // int[] digitalHumanCoord = new int[]{0,0};
  252. // digitalHuman.setCoord(digitalHumanCoord);
  253. // digitalHuman.setLevel(0);
  254. // digitalHuman.setRatio(1.0f);
  255. // videoFiles.setDigital_human(digitalHuman);
  256. // videoParam.setVideo_files(videoFiles);
  257. //
  258. // AiVideoResult video = AiVideoHelper.createVideo(videoParam);
  259. //
  260. // AiPhotoSpeakParam param = new AiPhotoSpeakParam();
  261. // param.setGen_txt("人多泰达股份冲冠怒发代发");
  262. // param.setImg(Base64Util.imageUrlToBase64("https://suimang.oss-accelerate.aliyuncs.com/builtin/personphoto/16564099737021232.png"));
  263. // param.setGender("male");
  264. // param.setVoice_id("yue-CN-YunSongNeural");
  265. // param.setVoice_style("default");
  266. // param.setUrl("None");
  267. // AiPhotoSpeakResult video = AiVideoHelper.createPhotoSpeakVideo(param);
  268. // AiPhotoSpeakParam param = new AiPhotoSpeakParam();
  269. // param.setGen_txt("None");
  270. // param.setImg(Base64Util.imageUrlToBase64("https://suimang.oss-accelerate.aliyuncs.com/builtin/personphoto/seed50258.png"));
  271. // param.setGender("None");
  272. // param.setVoice_id("default");
  273. // param.setVoice_style("default");
  274. // param.setUrl("https://video.metavatar.cc/sv/43a5c16d-189251d6a98/43a5c16d-189251d6a98.mp3");
  275. // AiPhotoSpeakResult video = AiVideoHelper.createPhotoSpeakVideo(param);
  276. // AiCheckPhotoParam param = new AiCheckPhotoParam();
  277. // String img = Base64Util.imageUrlToBase64("https://suimang.oss-accelerate.aliyuncs.com/builtin/personmould/16760216806604820_cSHoijDX_grace_1080.jpg");
  278. // param.setImg(img);
  279. // AiCheckPhotoResult result = AiVideoHelper.checkPhoto(param);
  280. // System.out.println(result);
  281. //
  282. AiPreviewParam param = new AiPreviewParam();
  283. param.setGen_txt("今天是个好日子");
  284. param.setVoice_id("ar-DZ-AminaNeural");
  285. param.setVoice_style("default");
  286. param.setGender("female");
  287. AiPreviewResult result = AiVideoHelper.voicePreview(param);
  288. System.out.println(result);
  289. // AiVideoHqParam param = new AiVideoHqParam();
  290. // param.setSave_dir();
  291. // AiVideoHqResult result = AiVideoHelper.videoHq(param);
  292. // System.out.println(result);
  293. }
  294. }