You can add visible watermarks such as company logos or TV station logos to your videos to enhance brand visibility, protect copyrights, and boost product recognition. ApsaraVideo Media Processing (MPS) provides the following types of watermarks: image watermark, animated watermark, and test watermark. You can select a watermark type based on your business requirements. This topic provides examples on how to call the API operations that are encapsulated in MPS SDK for Java to manage watermarks, including creating watermark templates, submitting text watermark jobs, and submitting image watermark jobs.
Prerequisites
The SDK client is initialized. For more information, see Initialize a client.
Create a watermark template
A watermark template specifies the settings of multiple parameters, such as the parameters that determine the position and size of watermarks. You can use a watermark template to simplify the watermarking process. This section describes how to call the AddWaterMarkTemplate operation to create a watermark template.
You can create a watermark template only for image watermarks.
A watermark template specifies only watermark attributes, such as the location and size, but does not specify watermark content. You need to specify watermark content when you submit a watermark job.
If a call to this operation succeeds, a watermark template ID is returned. You can also create a watermark template and obtain the template ID in the MPS console. For more information, see Manage watermark templates.
If the "The resource "WatermarkTemplate" quota has been used up" error message is returned, your quota for watermark templates is used up. In this case, you can submit a ticket to increase the quota.
/**
* Create a watermark template.
* @param client
* @return
* @throws Exception
*/
public static AddWaterMarkTemplateResponse addWaterMarkTemplate(DefaultAcsClient client) throws Exception {
AddWaterMarkTemplateRequest request = new AddWaterMarkTemplateRequest();
request.setName("test name");
// For more information about watermark parameters, visit 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);
}
Submit a watermark job
If you add a watermark to your video, the video images change, and you need to re-encode your video. MPS provides the SubmitJobs operation for you to submit a watermark job.
When you call the operation to submit a job, you must perform URL encoding on the path to the file that you want to use as the watermark. Otherwise, the job fails. For more information, see URL encoding.
You must specify a valid file name to ensure that the file can be found when the job runs. For more information, see Parameter details.
We recommend that you record the ID of a submitted job. This helps you with subsequent operations.
Submit a text watermark job
/**
* Submit a text watermark job.
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitTextWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
// Construct output parameters.
JSONArray outputs = new JSONArray();
// Configure an input object. Make sure that the value of the Location parameter is the region in which the client resides.
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
// Construct an output object.
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);
// Configure the output settings of watermarks.
JSONArray waterMarks = new JSONArray(); // A watermark array consists of up to four watermarks. This means that a stream can contain a maximum of four watermarks.
// Text watermarks.
JSONObject textWaterMarks = new JSONObject();
textWaterMarks.put("Type","Text");
// Specify the text to be used as watermarks for the Content parameter. The specified text must be encoded in Base64. The following codes shows an example.
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());
// Specify an output bucket.
request.setOutputBucket("<your bucket name>");
// Specify the region in which the output bucket resides. The region is in the oss-cn-**** format. Example: oss-cn-beijing.
request.setOutputLocation("oss-cn-beijing");
// The ID of the MPS queue.
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
Submit an image watermark job
/**
* Submit an image watermark job.
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitImageWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
// Construct output parameters.
JSONArray outputs = new JSONArray();
// Configure an input object. Make sure that the value of the Location parameter is the region in which the client resides.
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
// Construct an output object.
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);
// Configure the output settings of watermarks.
JSONArray waterMarks = new JSONArray(); // A watermark array consists of up to four watermarks. This means that a stream can contain a maximum of four watermarks.
// Image watermarks.
JSONObject imageWaterMarks = new JSONObject();
imageWaterMarks.put("WaterMarkTemplateId",waterMarkTemplateId);
imageWaterMarks.put("Type","Image");
imageWaterMarks.put("Width","200");
imageWaterMarks.put("Height","100");
// The path to the image to be used as watermarks.
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());
// Specify an output bucket.
request.setOutputBucket("<your bucket name>");
// Specify the region in which the output bucket resides. The region is in the oss-cn-**** format. Example: oss-cn-beijing.
request.setOutputLocation("oss-cn-beijing");
// The ID of the MPS queue.
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
Sample code
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;
/**
* ***** Usage notes ******
* This demo provides examples on the watermark template and job management.
*
* ***** References ******
* addWaterMarkTemplate Creates a watermark template: https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-addwatermarktemplate
* submitTextWaterMarkJobs Submits a text watermark job: https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-submitjobs
* submitImageWaterMarkJobs Submits an image watermark job: https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-submitjobs
*
* For more information about the parameters, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a.
*/
public class WaterMark {
// The ID of the MPS queue. To obtain the ID of the MPS queue, log on to the MPS console. In the left-side navigation pane, choose Global Settings > MPS Queue and Callback.
private static String pipelineId = "bee7a5bfe40a0cbf4a526f****";
// The ID of the preset template. For information about the preset templates, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/yuzhimobanxiangqing.
private static String templateId = "S00000001-200010";
// The ID of the watermark template. To obtain the ID of the watermark template, log on to the MPS console. In the left-side navigation pane, choose Template Management > Watermark Templates.
private static String waterMarkTemplateId = "bee7a4a526fa0cbf****";
public static void main(String[] args) throws ClientException {
// Initialize the 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 {
// //Text watermarks.
// //response = submitTextWaterMarkJobs(client);
// //Image watermarks.
// response = submitImageWaterMarkJobs(client);
// System.out.println("RequestId is:"+response.getRequestId());
// //Call the getJobResultList operation to obtain returned results.
// 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();
// }
}
/**
* Create a watermark template.
* @param client
* @return
* @throws Exception
*/
public static AddWaterMarkTemplateResponse addWaterMarkTemplate(DefaultAcsClient client) throws Exception {
AddWaterMarkTemplateRequest request = new AddWaterMarkTemplateRequest();
request.setName("test name");
// For more information about watermark parameters, visit 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);
}
/**
* Submit a text watermark job.
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitTextWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
// Construct output parameters.
JSONArray outputs = new JSONArray();
// Configure an input object. Make sure that the value of the Location parameter is the region in which the client resides.
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
// Construct an output object.
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);
// Configure the output settings of watermarks.
JSONArray waterMarks = new JSONArray(); // A watermark array consists of up to four watermarks. This means that a stream can contain a maximum of four watermarks.
// Text watermarks.
JSONObject textWaterMarks = new JSONObject();
textWaterMarks.put("Type","Text");
// Specify the text to be used as watermarks for the Content parameter. The specified text must be encoded in 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());
// Specify an output bucket.
request.setOutputBucket("<your bucket name>");
// Specify the region in which the output bucket resides. The region is in the oss-cn-**** format. Example: oss-cn-beijing.
request.setOutputLocation("oss-cn-beijing");
// The ID of the MPS queue.
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* Submit an image watermark job.
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse submitImageWaterMarkJobs(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
// Construct output parameters.
JSONArray outputs = new JSONArray();
// Configure an input object. Make sure that the value of the Location parameter is the region in which the client resides.
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
// Construct an output object.
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);
// Configure the output settings of watermarks.
JSONArray waterMarks = new JSONArray(); // A watermark array consists of up to four watermarks. This means that a stream can contain a maximum of four watermarks.
// Image watermarks.
JSONObject imageWaterMarks = new JSONObject();
imageWaterMarks.put("WaterMarkTemplateId",waterMarkTemplateId);
imageWaterMarks.put("Type","Image");
imageWaterMarks.put("Width","200");
imageWaterMarks.put("Height","100");
// The path to the image to be used as watermarks.
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());
// Specify an output bucket.
request.setOutputBucket("<your bucket name>");
// Specify the region in which the output bucket resides. The region is in the oss-cn-**** format. Example: oss-cn-beijing.
request.setOutputLocation("oss-cn-beijing");
// The ID of the MPS queue.
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
}