DigitalImgController测试

main
liushujing 2 months ago
parent 36e493dbb5
commit 507d7dac9c

@ -62,21 +62,26 @@ public class MyDigitalHumanController {
@ApiOperation("保存")
public Result save(@RequestBody MyDigitalHumanDTO dto){
//
MyDigitalHumanDTO ret = myDigitalHumanService.genVideo(dto);
//myDigitalHumanService.save(dto);
return new Result<MyDigitalHumanDTO>().ok(ret);
try{
//
MyDigitalHumanDTO ret = myDigitalHumanService.genVideo(dto);
//myDigitalHumanService.save(dto);
return new Result<MyDigitalHumanDTO>().ok(ret);
}catch(RuntimeException e){
return new Result().error(e.getMessage());
}
}
@PutMapping
@ApiOperation("修改")
public Result update(@RequestBody MyDigitalHumanDTO dto){
MyDigitalHumanDTO ret = myDigitalHumanService.genVideo(dto);
//myDigitalHumanService.update(dto);
return new Result<MyDigitalHumanDTO>().ok(ret);
try{
MyDigitalHumanDTO ret = myDigitalHumanService.genVideo(dto);
//myDigitalHumanService.update(dto);
return new Result<MyDigitalHumanDTO>().ok(ret);
}catch(RuntimeException e){
return new Result().error(e.getMessage());
}
}
@DeleteMapping

@ -31,6 +31,4 @@ public class DigitalImgDTO implements Serializable {
@ApiModelProperty(value = "图片类型1数字人形象2数字人场景3数字人背景4音色形象")
private String imgType;
@ApiModelProperty(value = "音频在服务器的目录")
private String voiceServerDir;
}

@ -46,5 +46,6 @@ public class MyDigitalHumanDTO implements Serializable {
@ApiModelProperty(value = "声音存放目录")
private String voicedir;
@ApiModelProperty(value = "声音文本")
private String text;
}

@ -34,5 +34,6 @@ public class VoiceRefFilesDTO implements Serializable {
@ApiModelProperty(value = "语言")
private String language;
@ApiModelProperty(value = "音频在服务器的目录")
private String voiceServerDir;
}

@ -33,9 +33,5 @@ public class DigitalImgEntity {
*/
private String imgType;
/**
*
*/
private String voiceServerDir;
}

@ -35,4 +35,8 @@ public class VoiceRefFilesEntity {
*
*/
private String language;
/**
*
*/
private String voiceServerDir;
}

@ -4,12 +4,20 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pjilisense.flxai.base.service.impl.CrudServiceImpl;
import com.pjilisense.flxai.dao.MyDigitalHumanDao;
import com.pjilisense.flxai.dto.MyDigitalHumanDTO;
import com.pjilisense.flxai.dto.VoiceRefFilesDTO;
import com.pjilisense.flxai.entity.MyDigitalHumanEntity;
import com.pjilisense.flxai.service.MyDigitalHumanService;
import cn.hutool.core.util.StrUtil;
import com.pjilisense.flxai.service.VoiceRefFilesService;
import com.pjilisense.flxai.utils.FileUtil;
import com.pjilisense.flxai.utils.TtsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
/**
* ${comments}
@ -20,6 +28,15 @@ import java.util.Map;
@Service
public class MyDigitalHumanServiceImpl extends CrudServiceImpl<MyDigitalHumanDao, MyDigitalHumanEntity, MyDigitalHumanDTO> implements MyDigitalHumanService {
@Autowired
VoiceRefFilesService voiceRefFilesService;
@Autowired
TtsUtils ttsUtils;
@Autowired
FileUtil fileUtil;
@Override
public QueryWrapper<MyDigitalHumanEntity> getWrapper(Map<String, Object> params){
String id = (String)params.get("id");
@ -34,7 +51,24 @@ public class MyDigitalHumanServiceImpl extends CrudServiceImpl<MyDigitalHumanDao
@Override
public MyDigitalHumanDTO genVideo(MyDigitalHumanDTO dto) {
//TODO:gen video code here
//TODO:gen generate code here
//1.generate voice by text and select voiceid
String voiceId = dto.getVoiceid();
VoiceRefFilesDTO voiceRefFilesDTO = voiceRefFilesService.get(voiceId);
String dir = voiceRefFilesDTO.getVoiceServerDir();
String text = dto.getText();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
String monthpath = sdf.format(new Date()).substring(0,6);
String relPath = "/genvoice/"+monthpath+"/"+ UUID.randomUUID().toString().replace("-","")+".wav";
String destFile = fileUtil.getFilepath(relPath);
boolean ttsres = ttsUtils.sendText(text,dir,destFile);
if(ttsres==true){
dto.setVoicedir(relPath);
} else {
throw new RuntimeException("生成音频失败!");
}
//2.生成视频
if(dto.getId()!=null&&dto.getId().length()>0){
update(dto);
} else {

@ -0,0 +1,346 @@
package com.pjilisense.flxai.utils;
import com.alibaba.fastjson.JSON;
import okhttp3.*;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class OkHttpUtils {
private static volatile OkHttpClient okHttpClient = null;
private static volatile Semaphore semaphore = null;
private Map<String, String> headerMap;
private Map<String, Object> paramMap;
private String json;
private String url;
private Request.Builder request;
/**
* okHttpClienthttps访
*/
private OkHttpUtils() {
if (okHttpClient == null) {
synchronized (OkHttpUtils.class) {
if (okHttpClient == null) {
TrustManager[] trustManagers = buildTrustManagers();
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(500, TimeUnit.SECONDS)
.writeTimeout(500, TimeUnit.SECONDS)
.readTimeout(500, TimeUnit.SECONDS)
.sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
.hostnameVerifier((hostName, session) -> true)
.retryOnConnectionFailure(true)
.build();
addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
}
}
}
}
/**
* 访线
*
* @return
*/
private static Semaphore getSemaphoreInstance() {
//只能1个线程同时访问
synchronized (OkHttpUtils.class) {
if (semaphore == null) {
semaphore = new Semaphore(0);
}
}
return semaphore;
}
/**
* OkHttpUtils
*
* @return
*/
public static OkHttpUtils builder() {
return new OkHttpUtils();
}
/**
* url
*
* @param url
* @return
*/
public OkHttpUtils url(String url) {
this.url = url;
return this;
}
/**
*
*
* @param key
* @param value
* @return
*/
public OkHttpUtils addParam(String key, Object value) {
if (paramMap == null) {
paramMap = new LinkedHashMap<>(16);
}
paramMap.put(key, value);
return this;
}
/**
*
*
* @param json json
* @return
*/
public OkHttpUtils addParam(String json) {
this.json = json;
return this;
}
/**
*
*
* @param key
* @param value
* @return
*/
public OkHttpUtils addHeader(String key, String value) {
if (headerMap == null) {
headerMap = new LinkedHashMap<>(16);
}
headerMap.put(key, value);
return this;
}
/**
* get
*
* @return
*/
public OkHttpUtils get() {
request = new Request.Builder().get();
StringBuilder urlBuilder = new StringBuilder(url);
if (paramMap != null) {
urlBuilder.append("?");
try {
for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
urlBuilder.append(URLEncoder.encode(entry.getKey(), "utf-8")).
append("=").
append(URLEncoder.encode(String.valueOf(entry.getValue()), "utf-8")).
append("&");
}
} catch (Exception e) {
e.printStackTrace();
}
urlBuilder.deleteCharAt(urlBuilder.length() - 1);
}
request.url(urlBuilder.toString());
return this;
}
/**
* post
*
* @param isJsonPost truejsonpostmanpostraw
* false
* @return
*/
public OkHttpUtils post(boolean isJsonPost) {
RequestBody requestBody = null;
if (isJsonPost) {
String json = "";
if (paramMap != null) {
json = JSON.toJSONString(paramMap);
} else {
json = this.json;
}
requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
} else {
FormBody.Builder formBody = new FormBody.Builder();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
formBody.add(key, String.valueOf(paramMap.get(key)));
}
}
requestBody = formBody.build();
}
// }
request = new Request.Builder().post(requestBody).url(url);
return this;
}
/**
*
*
* @return
*/
public String sync() {
setHeader(request);
try {
Response response = okHttpClient.newCall(request.build()).execute();
assert response.body() != null;
String retVal =response.body().string();
response.close();
return retVal;
} catch (IOException e) {
e.printStackTrace();
return "请求失败:" + e.getMessage();
}
}
/**
*
*
* @return
*/
public boolean syncwav(String destFile) {
setHeader(request);
try {
Response response = okHttpClient.newCall(request.build()).execute();
assert response.body() != null;
ResponseBody body = response.body();
java.io.InputStream inputStream = body.byteStream();
byte[] bytes = new byte[8192];
int readCount=-1;
File f= new File(destFile);
if(f.exists()) {
f.delete();
}
FileOutputStream fso = new FileOutputStream(f);
while((readCount=inputStream.read(bytes))!=-1){
fso.write(bytes,0,readCount);
}
fso.close();
response.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
*
*/
public String async() {
StringBuilder buffer = new StringBuilder("");
setHeader(request);
okHttpClient.newCall(request.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
buffer.append("请求出错:").append(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
assert response.body() != null;
buffer.append(response.body().string());
getSemaphoreInstance().release();
}
});
try {
getSemaphoreInstance().acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
return buffer.toString();
}
/**
*
*
* @param callBack
*/
public void async(ICallBack callBack) {
setHeader(request);
okHttpClient.newCall(request.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBack.onFailure(call, e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
assert response.body() != null;
callBack.onSuccessful(call, response.body().string());
}
});
}
/**
* request
*
* @param request
*/
private void setHeader(Request.Builder request) {
if (headerMap != null) {
try {
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* https
*
* @return
*/
private static SSLSocketFactory createSSLSocketFactory(TrustManager[] trustAllCerts) {
SSLSocketFactory ssfFactory = null;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
ssfFactory = sc.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return ssfFactory;
}
private static TrustManager[] buildTrustManagers() {
return new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
}
/**
*
*/
public interface ICallBack {
void onSuccessful(Call call, String data);
void onFailure(Call call, String errorMsg);
}
}

@ -0,0 +1,103 @@
package com.pjilisense.flxai.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
@Repository
public class TtsUtils {
@Value("${tts.url.server}")
private String serverUrl;//http://192.168.200.152:8080/v1/tts
@Value("${tts.url.path}")
private String path;
public boolean sendText(String text,String voiceId,String destFile) {
try {
long ts = System.currentTimeMillis() / 1000;
long start = System.currentTimeMillis();
boolean result = OkHttpUtils.builder()
.url(serverUrl+path)
.addParam(genBodyJson(text,voiceId))
.addHeader("timestamp", String.valueOf(ts))
.addHeader("Content-Type", " application/json;charset=utf-8")
.post(true).syncwav(destFile);
System.out.println("result="+result);
long end = System.currentTimeMillis();
return result;
}catch(Exception e){
return false;
}
}
public String genBodyJson(String text,String voiceId) {
LinkedHashMap<String, Object> postParam = new LinkedHashMap<>();
postParam.put("text", text);
postParam.put("chunk_length", 200);
postParam.put("format", "wav");
postParam.put("references", new ArrayList<Object>());
postParam.put("reference_id", voiceId);
postParam.put("seed", null);
postParam.put("use_memory_cache", "never");
postParam.put("normalize", true);
postParam.put("opus_bitrate", -1000);
postParam.put("latency", "normal");
postParam.put("streaming", false);
postParam.put("max_new_tokens", 1024);
postParam.put("top_p", 0.7);
postParam.put("repetition_penalty", 1.2);
postParam.put("temperature", 0.7);
String json = JSON.toJSONString(postParam);
// parser.add_argument(
// "--reference_audio",
// "-ra",
// type=str,
// nargs="+",
// default=None,
// help="Path to the audio file",
// )
// parser.add_argument(
// "--reference_text",
// "-rt",
// type=str,
// nargs="+",
// default=None,
// help="Reference text for voice synthesis",
// )
// parser.add_argument(
// "--output",
// "-o",
// type=str,
// default="generated_audio",
// help="Output audio file name",
// )
return json;
}
public String hmacSHA256(String appKey, String data) {
try {
// Create HMAC-SHA256 key from the given secret
SecretKeySpec secretKeySpec = new SecretKeySpec(appKey.getBytes(), "HmacSHA256");
// Get an instance of Mac object implementing HMAC-SHA256
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretKeySpec);
// Calculate the HMAC value
byte[] hmacBytes = mac.doFinal(data.getBytes());
// Convert result into a hexadecimal string
StringBuilder sb = new StringBuilder(hmacBytes.length * 2);
for (byte b : hmacBytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException("Failed to calculate HMAC-SHA256", e);
}
}
}

@ -68,3 +68,7 @@ file:
## Linux系统下访问路径
uploadLinux: /user/img/
uploadUrl: /upload/
tts:
url:
server: http://192.168.200.152:8080
path: /v1/tts

@ -8,7 +8,7 @@
<result property="name" column="name"/>
<result property="imgPath" column="img_path"/>
<result property="imgType" column="img_type"/>
<result property="voiceServerDir" column="voice_server_dir"/>
</resultMap>

@ -9,13 +9,15 @@
<result property="voicePath" column="voice_path"/>
<result property="imgPath" column="img_path"/>
<result property="language" column="language"/>
<result property="voiceServerDir" column="voice_server_dir"/>
</resultMap>
<resultMap type="java.util.Map" id="BaseResultMap">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="voicePath" column="voice_path"/>
<result property="imgPath" column="img_path"/>
<result property="language" column="language"/>
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="voicePath" column="voice_path"/>
<result property="imgPath" column="img_path"/>
<result property="language" column="language"/>
<result property="voiceServerDir" column="voice_server_dir"/>
</resultMap>
</mapper>
Loading…
Cancel
Save