最近AI视频换脸功能很是火爆,某抖、某手等短视频平台这类视频很🔥,出于对新技术的好奇,对该技术进行了一些研究,发现这类技术文章比较少,通过网络搜索并结合实际,有了以下两种实现方式。有相关需求的朋友,请继续往下阅读并可参考本文进行实现。
一、利用Github上基于Python的开源项目自行搭建,并提供相关API进行调用,这种方式对服务器性能配置要求非常高,暂时不考虑。
二、接入第三方开放平台,通过调用第三方平台人脸融合API,即可快速实现换脸功能。
本文采用第二种方式,通过接入“图灵换脸AI开放平台”,基于JAVA语言进行实现,一键制作古风、现代、民族风的视频,让你火爆朋友圈。具体步骤如下:
步骤一:注册账户并登录
根据页面提示,输入手机号码及短信验证码即可快速注册并进行登录。
步骤二:创建应用
登录平台管理后台,添加应用,按照页面提示进行填写应用基本信息,提交应用并等待平台审核。如下图所示:
步骤三:调用API
等待步骤二应用审核通过后,即可使用AppKey进行调用API,具体接口调用、请求参数、返回结果等内容,请参照“帮助文档”说明进行处理。
步骤四:具体实现
1、查询视频换脸模版分类:
温馨提示:appKey和appSceret需自行替换,登录?“图灵换脸AI开放平台”查看。
/**
* 查询视频换脸模版分类列表
*/
public static void testQueryTemplateType() {
//API请求地址
String url = "https://ai.ncmyt.cn/api/faceVideo/queryTemplateType";
//API请求参数
Map<String, String> params = new HashMap<>();
//1、公共请求参数
params.put("appKey", appKey);
params.put("timestamp", System.currentTimeMillis() + "");
params.put("version", "1.0.1");
//2、业务请求参数
//计算sign
String sign = createSign(params, appSceret);
params.put("sign", sign);
//3、发起POST请求
try {
JSONObject jsonObject = JSON.parseObject(sendHttpPostRequest(url, params));
String data = jsonObject.getString("data");
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && status.equalsIgnoreCase("ok")) {
if (StringUtils.isNotBlank(data)) {
data = Base64Utils.decode(data);//进行Base64解密,得到JSON字符串
}
System.out.println("模版分类列表:" + data);
//[{"name":"最美旗袍","type":"1"},{"name":"56个民族","type":"2"},
// {"name":"古装汉服","type":"3"},{"name":"肌肉精钢","type":"4"},
// {"name":"剧情","type":"5"},{"name":"男士专属","type":"6"},
// {"name":"骑马","type":"7"},{"name":"气质美女","type":"8"},
// {"name":"婚纱","type":"9"}]
}
} catch (Exception e) {
e.printStackTrace();
}
}
?计算sign签名
/**
* 创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
*/
public static String createSign(Map<String, String> params, String appSceret) {
TreeMap<String, String> treeMap = new TreeMap<>();
if (params != null) {
for (String key : params.keySet()) {
if (StringUtils.isNotBlank(params.get(key))) {
treeMap.put(key, params.get(key));
}
}
}
StringBuilder sb = new StringBuilder();
Set es = treeMap.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if (StringUtils.isNotBlank(v) && !"sign".equals(k) && !"appSceret".equals(k)) {
sb.append(k).append("=").append(v).append("&");
}
}
sb.append("appSceret=").append(appSceret);
String sign = mD5Encrypt(sb.toString()).toUpperCase();
//debug信息
//System.out.println(sb.toString() + " => sign:" + sign);
return sign;
}
发送POST请求
/**
* 发送POST请求
* @param httpUrl 请求地址
* @param params 请求参数
*/
public static String sendHttpPostRequest(String httpUrl, Map<String, String> params) {
String responseContent = null;
try {
URL url = new URL(httpUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
if (params != null && params.size() > 0) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
conn.getOutputStream().write(sb.substring(1).getBytes(StandardCharsets.UTF_8));
}
conn.connect();
responseContent = StreamUtils.copyToString(conn.getInputStream(), StandardCharsets.UTF_8);
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return responseContent;
}
MD5加密
private static char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* MD加密
*/
public static String mD5Encrypt(String source) {
if (source == null || "".equals(source)) {
return null;
}
String afterMD5;
try {
byte[] strTemp = source.getBytes(StandardCharsets.UTF_8);
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
str[k++] = HEX_DIGITS[byte0 & 0xf];
}
afterMD5 = new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return afterMD5.toUpperCase();
}
2、查询分类视频模版列表
/**
* 查询分类视频模版列表
*/
public static void testQueryTemplateList() {
//API请求地址
String url = "https://ai.ncmyt.cn/api/faceVideo/queryTemplateList";
//API请求参数
Map<String, String> params = new HashMap<>();
//1、公共请求参数
params.put("appKey", appKey);
params.put("timestamp", System.currentTimeMillis() + "");
params.put("version", "1.0.1");
//2、业务请求参数
params.put("type", "1");//模版类型
params.put("pageSize", "20");//页码大小
params.put("currentPage", "1");//当前第几页
//计算sign
String sign = createSign(params, appSceret);
params.put("sign", sign);
//3、发起POST请求
try {
JSONObject jsonObject = JSON.parseObject(sendHttpPostRequest(url, params));
String data = jsonObject.getString("data");
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && status.equalsIgnoreCase("ok")) {
if (StringUtils.isNotBlank(data)) {
data = Base64Utils.decode(data);//进行Base64解密,得到JSON字符串
}
System.out.println("视频模版列表:" + data);
//[{"groupid":"1","id":793,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/366F5BC4BEB622B5C1CAC7DAD82E7C46.png","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/366F5BC4BEB622B5C1CAC7DAD82E7C46.mp4"},
// {"groupid":"1","id":570,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/EC3584F44F9C9606CA3355ABCB452538.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/EC3584F44F9C9606CA3355ABCB452538.mp4"},
// {"groupid":"1","id":571,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/D40A14A65ED88A55037FCFD3186BDD83.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/D40A14A65ED88A55037FCFD3186BDD83.mp4"},
// {"groupid":"1","id":572,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/52242C4CA5006296457C0012CDFF7C81.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/52242C4CA5006296457C0012CDFF7C81.mp4"},
// {"groupid":"1","id":573,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/D2798FF7AE68B9BBDB92A911E0AAB715.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/D2798FF7AE68B9BBDB92A911E0AAB715.mp4"},
// {"groupid":"1","id":574,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/E2EB49AD065D20EC87DE04A9729E0EAA.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/E2EB49AD065D20EC87DE04A9729E0EAA.mp4"},
// {"groupid":"1","id":575,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/E293F3EF435AD1741709485D9C2FFB7A.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/E293F3EF435AD1741709485D9C2FFB7A.mp4"},
// {"groupid":"1","id":576,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/68AED56B2016DB56008E9DA3C4098A50.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/68AED56B2016DB56008E9DA3C4098A50.mp4"},
// {"groupid":"1","id":659,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/6343C7BE621489702FFA0070FD6DBC7D.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/6343C7BE621489702FFA0070FD6DBC7D.mp4"},
// {"groupid":"1","id":660,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/DAB38C6136B5BF2583EC0D4334A1018E.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/DAB38C6136B5BF2583EC0D4334A1018E.mp4"},
// {"groupid":"1","id":661,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/8C1C700DDF6166DC2EAC7405E24AA812.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/8C1C700DDF6166DC2EAC7405E24AA812.mp4"},
// {"groupid":"1","id":662,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/B0CC6A84280E7AFB3512E803565609E8.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/B0CC6A84280E7AFB3512E803565609E8.mp4"},
// {"groupid":"1","id":663,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/8ADBF46FCDC0786A932EF914FA861E17.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/8ADBF46FCDC0786A932EF914FA861E17.mp4"},
// {"groupid":"1","id":664,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/7D7E23E8F9FC0B71244ACF45A8AF5A04.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/7D7E23E8F9FC0B71244ACF45A8AF5A04.mp4"},
// {"groupid":"1","id":665,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/D0249C73FF3F3ACB4954EB6FBFA313D6.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/D0249C73FF3F3ACB4954EB6FBFA313D6.mp4"},
// {"groupid":"1","id":666,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/B6D58ECDF0A2370144CD54BFCCADF57C.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/B6D58ECDF0A2370144CD54BFCCADF57C.mp4"},
// {"groupid":"1","id":667,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/41AB5D77026A1567229E76E977AC3CFD.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/41AB5D77026A1567229E76E977AC3CFD.mp4"},
// {"groupid":"1","id":702,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/034B719D29570DFAFA0E9E0DBB36830F.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/034B719D29570DFAFA0E9E0DBB36830F.mp4"}]
}
} catch (Exception e) {
e.printStackTrace();
}
}
3、创建换脸视频订单
/**
* 创建换脸视频订单
*/
public static void testCreateOrder() {
//API请求地址
String url = "https://ai.ncmyt.cn/api/faceVideo/createOrder";
//API请求参数
Map<String, String> params = new HashMap<>();
//1、公共请求参数
params.put("appKey", appKey);
params.put("timestamp", System.currentTimeMillis() + "");
params.put("version", "1.0.1");
//2、业务请求参数
params.put("templateId", "570");//视频模版编号
params.put("merchantNo", "NO7758521");//商户系统订单编号(商户系统唯一)
params.put("imgUrl", "https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideoimage/FAD002054198EF17A782ECD46A5F2399.png");//待换脸的图片URL
//计算sign
String sign = createSign(params, appSceret);
params.put("sign", sign);
//3、发起POST请求
try {
JSONObject jsonObject = JSON.parseObject(sendHttpPostRequest(url, params));
String data = jsonObject.getString("data");
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && status.equalsIgnoreCase("ok")) {
if (StringUtils.isNotBlank(data)) {
data = Base64Utils.decode(data);//进行Base64解密,得到JSON字符串
}
System.out.println("创建订单信息:" + data);
//{"appKey":"ab4f010837f00b57","createTime":"2021-09-22 15:01:14",
// "imageUrl":"https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideoimage/FAD002054898EF17A782ECD10A5F2399.png",
// "merchantNo":"NO7758521","notifyStatus":3,"notifyTimes":0,
// "notifyUrl":"http://yuedaojia.natapp1.cc/h5/openApi/testDealFacePlayOrderCallBack",
// "orderNo":"304fff52a0cf45aa338110ca45833369","status":1,"templateId":"570",
// "videoUrl":"https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideo/EC3584F44F9C0106293355ABCB452538.mp4"}
}
} catch (Exception e) {
e.printStackTrace();
}
}
创建订单后,可以通过后台进行查看订单记录,如下图所示:
4、查询订单
/**
* 查询视频换脸订单
*/
public static void testQueryOrder() {
//API请求地址
String url = "https://ai.ncmyt.cn/api/faceVideo/queryOrder";
//API请求参数
Map<String, String> params = new HashMap<>();
//1、公共请求参数
params.put("appKey", appKey);
params.put("timestamp", System.currentTimeMillis() + "");
params.put("version", "1.0.1");
//2、业务请求参数
params.put("orderNo","debc2a0ded5d937babeba5bbc7fb34da");
//计算sign
String sign = createSign(params, appSceret);
params.put("sign", sign);
//3、发起POST请求
try {
JSONObject jsonObject = JSON.parseObject(sendHttpPostRequest(url, params));
String data = jsonObject.getString("data");
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && status.equalsIgnoreCase("ok")) {
if (StringUtils.isNotBlank(data)) {
data = Base64Utils.decode(data);//进行Base64解密,得到JSON字符串
}
System.out.println("订单信息:" + data);
//{
// "appKey":"ab4f091837f00b57","createTime":"2021-09-22 15:02:49",
// "imageUrl":"https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideoimage/FAD002054790EF17A782ECD46A5F2399.png",
// "merchantNo":"NO7758521",
// "notifyStatus":2,
// "notifyTimes":5,
// "notifyUrl":"http://test.natapp.cc/h5/openApi/testDealFacePlayOrderCallBack",
// "orderNo":"debc2a0ded5d937babeba5bbc7fb34da",
// "resultVideoUrl":"https://artsignimg.trsoft.xin/artsign/facevideoout/20210911150247775820919.mp4",
// "status":3,
// "templateId":"570",
// "videoUrl":"https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideo/EC3584F44F9C2606CA3355ABCB452538.mp4"
// }
}
} catch (Exception e) {
e.printStackTrace();
}
}
?以上就是全部API调用的DEMO,完整代码如下:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.StreamUtils;
import sun.misc.BASE64Decoder;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
/**
* 图灵AI视频换脸API调用示例
* @author 爱做饭的大圣
* @date 2021-09-22
*/
public class TestAIFacePlay {
//此处需自行替换
private static final String appKey = "appKey";
private static final String appSceret = "appSceret";
private static char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* MD加密
*/
public static String mD5Encrypt(String source) {
if (source == null || "".equals(source)) {
return null;
}
String afterMD5;
try {
byte[] strTemp = source.getBytes(StandardCharsets.UTF_8);
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
str[k++] = HEX_DIGITS[byte0 & 0xf];
}
afterMD5 = new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return afterMD5.toUpperCase();
}
/**
* 创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
*/
public static String createSign(Map<String, String> params, String appSceret) {
TreeMap<String, String> treeMap = new TreeMap<>();
if (params != null) {
for (String key : params.keySet()) {
if (StringUtils.isNotBlank(params.get(key))) {
treeMap.put(key, params.get(key));
}
}
}
StringBuilder sb = new StringBuilder();
Set es = treeMap.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if (StringUtils.isNotBlank(v) && !"sign".equals(k) && !"appSceret".equals(k)) {
sb.append(k).append("=").append(v).append("&");
}
}
sb.append("appSceret=").append(appSceret);
String sign = mD5Encrypt(sb.toString()).toUpperCase();
//debug信息
//System.out.println(sb.toString() + " => sign:" + sign);
return sign;
}
/**
* Base64解密
*
* @param s base64加密字符串
*/
public static String base64Decode(String s) {
byte[] b;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 发送POST请求
*
* @param httpUrl 请求地址
* @param params 请求参数
*/
public static String sendHttpPostRequest(String httpUrl, Map<String, String> params) {
String responseContent = null;
try {
URL url = new URL(httpUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
if (params != null && params.size() > 0) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
conn.getOutputStream().write(sb.substring(1).getBytes(StandardCharsets.UTF_8));
}
conn.connect();
responseContent = StreamUtils.copyToString(conn.getInputStream(), StandardCharsets.UTF_8);
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return responseContent;
}
/**
* 查询视频换脸模版分类列表
*/
public static void testQueryTemplateType() {
//API请求地址
String url = "https://ai.ncmyt.cn/api/faceVideo/queryTemplateType";
//API请求参数
Map<String, String> params = new HashMap<>();
//1、公共请求参数
params.put("appKey", appKey);
params.put("timestamp", System.currentTimeMillis() + "");
params.put("version", "1.0.1");
//2、业务请求参数
//计算sign
String sign = createSign(params, appSceret);
params.put("sign", sign);
//3、发起POST请求
try {
JSONObject jsonObject = JSON.parseObject(sendHttpPostRequest(url, params));
String data = jsonObject.getString("data");
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && status.equalsIgnoreCase("ok")) {
if (StringUtils.isNotBlank(data)) {
data = base64Decode(data);//进行Base64解密,得到JSON字符串
}
System.out.println("模版分类列表:" + data);
//[{"name":"最美旗袍","type":"1"},{"name":"56个民族","type":"2"},
// {"name":"古装汉服","type":"3"},{"name":"肌肉精钢","type":"4"},
// {"name":"剧情","type":"5"},{"name":"男士专属","type":"6"},
// {"name":"骑马","type":"7"},{"name":"气质美女","type":"8"},
// {"name":"婚纱","type":"9"}]
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询分类视频模版列表
*/
public static void testQueryTemplateList() {
//API请求地址
String url = "https://ai.ncmyt.cn/api/faceVideo/queryTemplateList";
//API请求参数
Map<String, String> params = new HashMap<>();
//1、公共请求参数
params.put("appKey", appKey);
params.put("timestamp", System.currentTimeMillis() + "");
params.put("version", "1.0.1");
//2、业务请求参数
params.put("type", "1");//模版类型
params.put("pageSize", "20");//页码大小
params.put("currentPage", "1");//当前第几页
//计算sign
String sign = createSign(params, appSceret);
params.put("sign", sign);
//3、发起POST请求
try {
JSONObject jsonObject = JSON.parseObject(sendHttpPostRequest(url, params));
String data = jsonObject.getString("data");
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && status.equalsIgnoreCase("ok")) {
if (StringUtils.isNotBlank(data)) {
data = base64Decode(data);//进行Base64解密,得到JSON字符串
}
System.out.println("视频模版列表:" + data);
//[{"groupid":"1","id":793,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/366F5BC4BEfjf0B622B5ffC1CAC7DAD82E7C46.png","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/366F5BC4BEB622B5C1CAC7DAD8219dfE7C46.mp4"},
// {"groupid":"1","id":570,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/EC3584F44F9C9606CA30999355ABCB452538.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/EC3584F44F9C9606CA3355A2344BCB452538.mp4"},
// {"groupid":"1","id":571,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/D40A14A65ED88A55fnf037FCFD3186BDD83.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/D40A14A65ED88A5503fmfm07FCFD3186BDD83.mp4"},
// {"groupid":"1","id":572,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/52242C4CA50062964fffj57C0012CDFF7C81.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/52242C4CA5006296457C001bbf2CDFF7C81.mp4"},
// {"groupid":"1","id":573,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/D2798FF7AE68B9BBffkkfDB92A911E0AAB715.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/D2ffnn798FF7AE68B9BBDB92A911E0AAB715.mp4"},
// {"groupid":"1","id":574,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/E2EB49AD065D20EC87DE0fffmmf4A9729E0EAA.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/E2EB49AD065D20EC87DEffm04A9729E0EAA.mp4"},
// {"groupid":"1","id":575,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/E293F3EF435AD1741709485D9C2FfffjjFB7A.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/E293F3EF435AD17ffhfh41709485D9C2FFB7A.mp4"},
// {"groupid":"1","id":576,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/68AED56B2016DB56008E9DA3C40fffj98A50.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/68AED56B2016DB56008E9DA3C40fkfkfk98A50.mp4"},
// {"groupid":"1","id":659,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/6343C7BE621489702FFA007ffjfj0FD6DBC7D.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/6343C7BE621489fjfjjf702FFA0070FD6DBC7D.mp4"},
// {"groupid":"1","id":660,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/DAB38C6136B5BF258ffjfj3EC0D4334A1018E.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/DAB38C6ffjfjjf136B5BF2583EC0D4334A1018E.mp4"},
// {"groupid":"1","id":661,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/8C1C700DDF6166DC2EAfjfjfj405E24AA812.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/8C1C700DDF6166DC2ncnAC7405E24AA812.mp4"},
// {"groupid":"1","id":662,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/B0CC6A84280E7AFB3512E80fffm565609E8.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/B0CC6A84280E7AFB39392E803565609E8.mp4"},
// {"groupid":"1","id":663,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/8ADBF46FCDC0786A932EF914FfffmfA861E17.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/8ADBF46FCDC0786A3742EF914FA861E17.mp4"},
// {"groupid":"1","id":664,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/7D7E23E8F9FC0B71244ACFffkfkf45A8AF5A04.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/7D7E23E8F9FC0B71044ACF45A8AF5A04.mp4"},
// {"groupid":"1","id":665,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/D0249C73FF3F3ACB4954EB6ffkfkfkFBFA313D6.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/D0249C73FF3F3ACB49fjfj4EB6FBFA313D6.mp4"},
// {"groupid":"1","id":666,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/B6D58ECDF0A2370144CD54BfkfkfkfkFCCADF57C.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/B6D58ECDF0A2370144CD5fhfhFCCADF57C.mp4"},
// {"groupid":"1","id":667,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/41AB5D77026A1567229Efffkkf76E977AC3CFD.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/41AB5D77026A1567229EuuE977AC3CFD.mp4"},
// {"groupid":"1","id":702,"videoimg":"https://artsignimg.trsoft.xin/artsign/facevideocover/034B719D29570DFAFA0E9Efkfkkfkf0DBB36830F.jpg","videootherurl":"https://artsignimg.trsoft.xin/artsign/facevideocompress/034B719D29570DFAFA05996E0DBB36830F.mp4"}]
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建换脸视频订单
*/
public static void testCreateOrder() {
//API请求地址
String url = "https://ai.ncmyt.cn/api/faceVideo/createOrder";
//API请求参数
Map<String, String> params = new HashMap<>();
//1、公共请求参数
params.put("appKey", appKey);
params.put("timestamp", System.currentTimeMillis() + "");
params.put("version", "1.0.1");
//2、业务请求参数
params.put("templateId", "570");//视频模版编号
params.put("merchantNo", "NO7758521");//商户系统订单编号(商户系统唯一)
params.put("imgUrl", "https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideoimage/FAD0020547E8103817A782ECD46A5F2399.png");//待换脸的图片URL
//计算sign
String sign = createSign(params, appSceret);
params.put("sign", sign);
//3、发起POST请求
try {
JSONObject jsonObject = JSON.parseObject(sendHttpPostRequest(url, params));
String data = jsonObject.getString("data");
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && status.equalsIgnoreCase("ok")) {
if (StringUtils.isNotBlank(data)) {
data = base64Decode(data);//进行Base64解密,得到JSON字符串
}
System.out.println("创建订单信息:" + data);
//{"appKey":"ab4f093837f00b57","createTime":"2021-09-22 15:01:14",
// "imageUrl":"https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideoimage/FAD0020547E8E1234A782ECD46A5F2399.png",
// "merchantNo":"NO7758521","notifyStatus":3,"notifyTimes":0,
// "notifyUrl":"http://yuedaojia.natapp1.cc/h5/openApi/testDealFacePlayOrderCallBack",
// "orderNo":"304fff52a0cf45aa338761ca45833369","status":1,"templateId":"570",
// "videoUrl":"https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideo/EC3584F44F9C96010A3355ABCB452538.mp4"}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询视频换脸订单
*/
public static void testQueryOrder() {
//API请求地址
String url = "https://ai.ncmyt.cn/api/faceVideo/queryOrder";
//API请求参数
Map<String, String> params = new HashMap<>();
//1、公共请求参数
params.put("appKey", appKey);
params.put("timestamp", System.currentTimeMillis() + "");
params.put("version", "1.0.1");
//2、业务请求参数
params.put("orderNo", "debc2a0ded5d937babeba5bbc7fb34da");
//计算sign
String sign = createSign(params, appSceret);
params.put("sign", sign);
//3、发起POST请求
try {
JSONObject jsonObject = JSON.parseObject(sendHttpPostRequest(url, params));
String data = jsonObject.getString("data");
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && status.equalsIgnoreCase("ok")) {
if (StringUtils.isNotBlank(data)) {
data = base64Decode(data);//进行Base64解密,得到JSON字符串
}
System.out.println("订单信息:" + data);
//{
// "appKey":"ab4f093837f00b57","createTime":"2021-09-22 15:02:49",
// "imageUrl":"https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideoimage/FAD0020547E8EF170082ECD46A5F2399.png",
// "merchantNo":"NO7758521",
// "notifyStatus":2,
// "notifyTimes":5,
// "notifyUrl":"http://yuedaojia.natapp1.cc/h5/openApi/testDealFacePlayOrderCallBack",
// "orderNo":"debc2a0ded5d937babeba5bbc7fb34da",
// "resultVideoUrl":"https://artsignimg.trsoft.xin/artsign/facevideoout/20210102150247775820919.mp4",
// "status":3,
// "templateId":"570",
// "videoUrl":"https://cartoonaliyun.oss-cn-shanghai.aliyuncs.com/facevideo/EC3584F44F9C9606C28355ABCB452538.mp4"
// }
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// testQueryTemplateType();
// testQueryTemplateList();
// testCreateOrder();
// testQueryOrder();
}
}
步骤五:功能测试
待制作的图片
?换脸后的视频(此处为视频截图)
以上就是本文基于 “图灵换脸AI开放平台”实现视频换脸的全部内容,欢迎转发!有问题请在评论区进行留言,谢谢!
|