在视频中添加可见的明水印(例如,企业Logo、电视台台标),可以突出品牌和版权,增加产品识别度。媒体处理支持图片水印、动画水印和文字水印三种水印类型,您可以按需选择。本文提供了Java SDK为视频添加可见明水印相关功能的API调用示例,包含创建水印模板、提交文字水印作业、提交图片水印作业。
前提条件
使用前请先初始化客户端,详细操作请参见初始化。
创建水印模板
水印模板是一系列包含水印位置、大小的参数,使用水印模板,可以帮助您简化开发操作。调用AddWaterMarkTemplate接口,完成水印模板创建。
/**
* 创建水印模板
* @param client
* @return
* @throws Exception
*/
public static AddWaterMarkTemplateResponse addWaterMarkTemplate(DefaultAcsClient client) throws Exception {
AddWaterMarkTemplateRequest request = new AddWaterMarkTemplateRequest();
request.setName("test name");
//水印参数详情, 参见https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a#section-k53-tt4-8b0
JSONObject waterMarkConfig = new JSONObject();
waterMarkConfig.put("Dx","10");
waterMarkConfig.put("Dy","5");
waterMarkConfig.put("ReferPos","TopRight");
request.setConfig(waterMarkConfig.toJSONString());
return client.getAcsResponse(request);
}
提交水印任务
为视频添加水印会改变画面的内容,需要对视频重新编码。在媒体处理中,您需要调用SubmitJobs接口,提交水印任务。
说明
通过SDK提交作业时,Object需经URLEncode,否则会导致作业失败。详细说明,请参见URL Encoding说明。
请按照规范填写文件名称,否则会找不到文件导致作业失败。详细名称规范,请参见参数详情。
建议您在提交作业时记录任务的JobID,便于后续进行其他相关操作。
文字水印
/**
* 提交文字水印转码作业
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitTextWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//构建输出参数
JSONArray outputs = new JSONArray();
//构建input, 需要保证Location区域和服务client区域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//构建一个输出对象
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/test-out-watermark.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
//构建水印输出配置
JSONArray waterMarks = new JSONArray(); //水印数组大小上限为4,即同一路输出最多支持4个水印
//文字水印
JSONObject textWaterMarks = new JSONObject();
textWaterMarks.put("Type","Text");
//Content为文字水印内容(示例中的Content原文为:测试文字水印),内容需经过Base64编码
textWaterMarks.put("TextWaterMark","{\"Content\":\"5rWL6K+V5paH5a2X5rC05Y2w\",\"FontName\":\"SimSun\",\"FontSize\":\"16\",\"Top\":2,\"Left\":10}");
waterMarks.add(textWaterMarks);
output.put("WaterMarks", waterMarks);
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
//指定输出bucket
request.setOutputBucket("<your bucket name>");
//输出bucket的所在区域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// 指定管道ID
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
图片水印
/**
* 提交图片水印转码作业
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitImageWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//构建输出参数
JSONArray outputs = new JSONArray();
//构建input, 需要保证Location区域和服务client区域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//构建一个输出对象
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/test-out-watermark.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
//构建水印输出配置
JSONArray waterMarks = new JSONArray(); //水印数组大小上限为4,即同一路输出最多支持4个水印
//图片水印
JSONObject imageWaterMarks = new JSONObject();
imageWaterMarks.put("WaterMarkTemplateId",waterMarkTemplateId);
imageWaterMarks.put("Type","Image");
imageWaterMarks.put("Width","200");
imageWaterMarks.put("Height","100");
//水印图片路径
JSONObject logoFile = new JSONObject();
logoFile.put("Bucket","<your bucket name>");
logoFile.put("Location","oss-cn-beijing");
logoFile.put("Object", URLEncoder.encode("logo.png", "utf-8"));
imageWaterMarks.put("InputFile",logoFile.toJSONString());
waterMarks.add(imageWaterMarks);
output.put("WaterMarks", waterMarks);
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
//指定输出bucket
request.setOutputBucket("<your bucket name>");
//输出bucket的所在区域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// 指定管道ID
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
完整代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mps.utils.InitClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* ***** 使用须知 ******
* 本demo为基本的水印转码任务处理示例
*
* ***** 方法介绍 ******
* addWaterMarkTemplate 添加水印模板 https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-addwatermarktemplate
* submitTextWaterMarkJobs 提交文字水印转码作业 https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-submitjobs
* submitImageWaterMarkJobs 提交图片水印转码作业 https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-submitjobs
*
* 具体参数详情参考 https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a
*/
public class WaterMark {
//管道ID, 可以在MPS控制台 > 全局设置 > 管道及回调查看
private static String pipelineId = "bee7a5bfe40a0cbf4a526f****";
//转码模板ID, 预置模板参考 https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/yuzhimobanxiangqing
private static String templateId = "S00000001-200010";
//水印模板ID, 可以在MPS控制台 > 模板管理 > 水印模板查看
private static String waterMarkTemplateId = "bee7a4a526fa0cbf****";
public static void main(String[] args) throws ClientException {
//初始化调用 client
DefaultAcsClient client = InitClient.initMpsClient();
AddWaterMarkTemplateResponse response;
try {
response = addWaterMarkTemplate(client);
System.out.println("RequestId is:" + response.getRequestId());
System.out.println("WaterMarkTemplateId is:" + JSON.toJSON(response.getWaterMarkTemplate().getId()));
} catch (Exception e) {
e.printStackTrace();
}
// SubmitJobsResponse response;
// try {
// //文字水印
// //response = submitTextWaterMarkJobs(client);
// //图片水印
// response = submitImageWaterMarkJobs(client);
// System.out.println("RequestId is:"+response.getRequestId());
// //多个output请遍历getJobResultList()获取结果
// if (response.getJobResultList().get(0).getSuccess()) {
// System.out.println("JobId is:" + response.getJobResultList().get(0).getJob().getJobId());
// System.out.println("Response is:" + JSON.toJSONString(response));
// } else {
// System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() +
// " message:" + response.getJobResultList().get(0).getMessage());
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
}
/**
* 创建水印模板
* @param client
* @return
* @throws Exception
*/
public static AddWaterMarkTemplateResponse addWaterMarkTemplate(DefaultAcsClient client) throws Exception {
AddWaterMarkTemplateRequest request = new AddWaterMarkTemplateRequest();
request.setName("test name");
//水印参数详情, 参考 https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a#section-k53-tt4-8b0
JSONObject waterMarkConfig = new JSONObject();
waterMarkConfig.put("Dx","10");
waterMarkConfig.put("Dy","5");
waterMarkConfig.put("ReferPos","TopRight");
request.setConfig(waterMarkConfig.toJSONString());
return client.getAcsResponse(request);
}
/**
* 提交文字水印转码作业
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitTextWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//构建输出参数
JSONArray outputs = new JSONArray();
//构建input, 需要保证Location区域和服务client区域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//构建一个输出对象
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/test-out-watermark.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
//构建水印输出配置
JSONArray waterMarks = new JSONArray(); //水印数组大小上限为4,即同一路输出最多支持4个水印
//文字水印
JSONObject textWaterMarks = new JSONObject();
textWaterMarks.put("Type","Text");
//Content为文字水印内容,内容需做Base64编码
textWaterMarks.put("TextWaterMark","{\"Content\":\"5rWL6K+V5paH5a2X5rC05Y2w\",\"FontName\":\"SimSun\",\"FontSize\":\"16\",\"Top\":2,\"Left\":10}");
waterMarks.add(textWaterMarks);
output.put("WaterMarks", waterMarks);
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
//指定输出bucket
request.setOutputBucket("<your bucket name>");
//输出bucket的所在区域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// 指定管道ID
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* 提交图片水印转码作业
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitImageWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//构建输出参数
JSONArray outputs = new JSONArray();
//构建input, 需要保证Location区域和服务client区域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//构建一个输出对象
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/test-out-watermark.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
//构建水印输出配置
JSONArray waterMarks = new JSONArray(); //水印数组大小上限为4,即同一路输出最多支持4个水印
//图片水印
JSONObject imageWaterMarks = new JSONObject();
imageWaterMarks.put("WaterMarkTemplateId",waterMarkTemplateId);
imageWaterMarks.put("Type","Image");
imageWaterMarks.put("Width","200");
imageWaterMarks.put("Height","100");
//水印图片路径
JSONObject logoFile = new JSONObject();
logoFile.put("Bucket","<your bucket name>");
logoFile.put("Location","oss-cn-beijing");
logoFile.put("Object", URLEncoder.encode("logo.png", "utf-8"));
imageWaterMarks.put("InputFile",logoFile.toJSONString());
waterMarks.add(imageWaterMarks);
output.put("WaterMarks", waterMarks);
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
//指定输出bucket
request.setOutputBucket("<your bucket name>");
//输出bucket的所在区域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// 指定管道ID
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
}