水印
更新時(shí)間:
在視頻中添加可見的明水?。ɡ纾髽I(yè)Logo、電視臺(tái)臺(tái)標(biāo)),可以突出品牌和版權(quán),增加產(chǎn)品識(shí)別度。媒體處理支持圖片水印、動(dòng)畫水印和文字水印三種水印類型,您可以按需選擇。本文提供了Java SDK為視頻添加可見明水印相關(guān)功能的API調(diào)用示例,包含創(chuàng)建水印模板、提交文字水印作業(yè)、提交圖片水印作業(yè)。
前提條件
使用前請(qǐng)先初始化客戶端,詳細(xì)操作請(qǐng)參見初始化。
創(chuàng)建水印模板
水印模板是一系列包含水印位置、大小的參數(shù),使用水印模板,可以幫助您簡化開發(fā)操作。調(diào)用AddWaterMarkTemplate接口,完成水印模板創(chuàng)建。
/**
* 創(chuàng)建水印模板
* @param client
* @return
* @throws Exception
*/
public static AddWaterMarkTemplateResponse addWaterMarkTemplate(DefaultAcsClient client) throws Exception {
AddWaterMarkTemplateRequest request = new AddWaterMarkTemplateRequest();
request.setName("test name");
//水印參數(shù)詳情, 參見http://bestwisewords.com/document_detail/29253.htm#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);
}
提交水印任務(wù)
為視頻添加水印會(huì)改變畫面的內(nèi)容,需要對(duì)視頻重新編碼。在媒體處理中,您需要調(diào)用SubmitJobs接口,提交水印任務(wù)。
說明
通過SDK提交作業(yè)時(shí),Object需經(jīng)URLEncode,否則會(huì)導(dǎo)致作業(yè)失敗。詳細(xì)說明,請(qǐng)參見URL Encoding說明。
請(qǐng)按照規(guī)范填寫文件名稱,否則會(huì)找不到文件導(dǎo)致作業(yè)失敗。詳細(xì)名稱規(guī)范,請(qǐng)參見參數(shù)詳情。
建議您在提交作業(yè)時(shí)記錄任務(wù)的JobID,便于后續(xù)進(jìn)行其他相關(guān)操作。
文字水印
/**
* 提交文字水印轉(zhuǎn)碼作業(yè)
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitTextWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構(gòu)建輸出參數(shù)
JSONArray outputs = new JSONArray();
//構(gòu)建input, 需要保證Location區(qū)域和服務(wù)client區(qū)域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//構(gòu)建一個(gè)輸出對(duì)象
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);
//構(gòu)建水印輸出配置
JSONArray waterMarks = new JSONArray(); //水印數(shù)組大小上限為4,即同一路輸出最多支持4個(gè)水印
//文字水印
JSONObject textWaterMarks = new JSONObject();
textWaterMarks.put("Type","Text");
//Content為文字水印內(nèi)容(示例中的Content原文為:測試文字水印),內(nèi)容需經(jīng)過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的所在區(qū)域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// 指定管道ID
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
圖片水印
/**
* 提交圖片水印轉(zhuǎn)碼作業(yè)
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitImageWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構(gòu)建輸出參數(shù)
JSONArray outputs = new JSONArray();
//構(gòu)建input, 需要保證Location區(qū)域和服務(wù)client區(qū)域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//構(gòu)建一個(gè)輸出對(duì)象
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);
//構(gòu)建水印輸出配置
JSONArray waterMarks = new JSONArray(); //水印數(shù)組大小上限為4,即同一路輸出最多支持4個(gè)水印
//圖片水印
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的所在區(qū)域,格式: 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為基本的水印轉(zhuǎn)碼任務(wù)處理示例
*
* ***** 方法介紹 ******
* addWaterMarkTemplate 添加水印模板 http://bestwisewords.com/document_detail/602851.html
* submitTextWaterMarkJobs 提交文字水印轉(zhuǎn)碼作業(yè) http://bestwisewords.com/document_detail/602834.html
* submitImageWaterMarkJobs 提交圖片水印轉(zhuǎn)碼作業(yè) http://bestwisewords.com/document_detail/602834.html
*
* 具體參數(shù)詳情參考 http://bestwisewords.com/document_detail/29253.htm#section-qdu-5fe-ftz
*/
public class WaterMark {
//管道ID, 可以在MPS控制臺(tái) > 全局設(shè)置 > 管道及回調(diào)查看
private static String pipelineId = "bee7a5bfe40a0cbf4a526f****";
//轉(zhuǎn)碼模板ID, 預(yù)置模板參考 http://bestwisewords.com/document_detail/29256.html
private static String templateId = "S00000001-200010";
//水印模板ID, 可以在MPS控制臺(tái) > 模板管理 > 水印模板查看
private static String waterMarkTemplateId = "bee7a4a526fa0cbf****";
public static void main(String[] args) throws ClientException {
//初始化調(diào)用 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());
// //多個(gè)output請(qǐng)遍歷getJobResultList()獲取結(jié)果
// 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();
// }
}
/**
* 創(chuàng)建水印模板
* @param client
* @return
* @throws Exception
*/
public static AddWaterMarkTemplateResponse addWaterMarkTemplate(DefaultAcsClient client) throws Exception {
AddWaterMarkTemplateRequest request = new AddWaterMarkTemplateRequest();
request.setName("test name");
//水印參數(shù)詳情, 參考 http://bestwisewords.com/document_detail/29253.htm#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);
}
/**
* 提交文字水印轉(zhuǎn)碼作業(yè)
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitTextWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構(gòu)建輸出參數(shù)
JSONArray outputs = new JSONArray();
//構(gòu)建input, 需要保證Location區(qū)域和服務(wù)client區(qū)域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//構(gòu)建一個(gè)輸出對(duì)象
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);
//構(gòu)建水印輸出配置
JSONArray waterMarks = new JSONArray(); //水印數(shù)組大小上限為4,即同一路輸出最多支持4個(gè)水印
//文字水印
JSONObject textWaterMarks = new JSONObject();
textWaterMarks.put("Type","Text");
//Content為文字水印內(nèi)容,內(nèi)容需做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的所在區(qū)域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// 指定管道ID
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* 提交圖片水印轉(zhuǎn)碼作業(yè)
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitImageWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構(gòu)建輸出參數(shù)
JSONArray outputs = new JSONArray();
//構(gòu)建input, 需要保證Location區(qū)域和服務(wù)client區(qū)域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//構(gòu)建一個(gè)輸出對(duì)象
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);
//構(gòu)建水印輸出配置
JSONArray waterMarks = new JSONArray(); //水印數(shù)組大小上限為4,即同一路輸出最多支持4個(gè)水印
//圖片水印
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的所在區(qū)域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// 指定管道ID
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
}
相關(guān)文檔
文檔內(nèi)容是否對(duì)您有幫助?