This topic provides examples on how to use the API operations of the video watermark module. The API operations are encapsulated in ApsaraVideo VOD SDK for Java. You can call the API operations to add, modify, delete, and query watermarks.
Usage notes
In this example, an AccessKey pair is used to initialize a client instance.
For more information about the request and response parameters of this operation, visit OpenAPI Explorer. You can click API Documentation in the top navigation bar to view information related to the API operation.
This topic provides sample code only for some complex API operations. To obtain sample code for other API operations, perform the following operations: Visit Alibaba Cloud OpenAPI Explorer. In the left-side navigation pane, find the API operation whose sample code you want to obtain and specify the required parameters on the Parameters tab. Then, click Initiate Call. On the SDK Sample Code tab, select the language to view and download the sample code.
Initialize a client
Before you use the SDK, initialize a client. For more information, see Initialization.
Add a watermark
You can call the AddWatermark operation to add a watermark.
Click AddWatermark to learn more about this API operation.
For more information about how to obtain the upload URL and credential, see CreateUploadAttachedMedia.
For more information about how to upload a watermark file to an Object Storage Service (OSS) bucket, see Upload OSS objects.
Sample code:
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.AddWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.AddWatermarkResponse;
/**
* Obtain the AccessKey information.
*/
public static DefaultAcsClient initVodClient() throws ClientException {
// Specify the region in which ApsaraVideo VOD is activated.
String regionId = "cn-shanghai";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
// We recommend that you do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources in your account is compromised.
// In this example, the system reads the AccessKey pair from environment variables to implement authentication for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* Add a watermark configuration function.
*/
public static AddWatermarkResponse addWatermark(DefaultAcsClient client) throws Exception {
AddWatermarkRequest request = new AddWatermarkRequest();
// The name of the watermark.
request.setName("addwatermark");
// The URL of the watermark file that is stored in an OSS bucket. Example: http://example.oss-cn-shanghai.aliyuncs.com/watermark/test-****.png.
String fileUrl = "<your watermarkFile URL>";
// If you want to use an image watermark, you must specify the OSS URL of the watermark file. The watermark file and video file must reside in the same region. For example, if a video is stored in the China (Shanghai) region, you can use only a watermark file that is stored in the China (Shanghai) region for the video.
request.setFileUrl(fileUrl);
// The watermark configurations.
JSONObject watermarkConfig = null;
// The position of the image watermark.
watermarkConfig = buildImageWatermarkConfig();
// The position of the text watermark.
//watermarkConfig = buildTextWatermarkConfig();
request.setWatermarkConfig(watermarkConfig.toJSONString());
// The type of the watermark. Set the value to Text for text watermarks and Image for image watermarks.
request.setType("Image");
return client.getAcsResponse(request);
}
/**
* Sample code
* @param args
* @throws ClientException
*/
public static void main(String[] args) throws ClientException {
DefaultAcsClient client = initVodClient();
AddWatermarkResponse response = new AddWatermarkResponse();
try {
// The watermark information.
response = addWatermark(client);
// The ID of the watermark.
System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
// The position and effect configurations of the watermark.
System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
// The URL of the watermark file. If you want to use a text watermark, leave this parameter empty.
System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
/**
* Construct the image watermark parameters and modify the corresponding parameter values based on your business requirements.
* @return
*/
public static JSONObject buildImageWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// The horizontal offset of the watermark on the output video.
watermarkConfig.put("Dx", "8");
// The vertical offset of the watermark on the output video.
watermarkConfig.put("Dy", "8");
// The width of the watermark on the output video.
watermarkConfig.put("Width", "55");
// The height of the watermark on the output video.
watermarkConfig.put("Height", "55");
// The relative position of the watermark to the output video. The watermark can be in the upper-left corner, upper-right corner, lower-left corner, or lower-right corner.
watermarkConfig.put("ReferPos", "BottomRight");
// The timeline of the watermark. You can specify the start time and duration for the watermark.
JSONObject timeline = new JSONObject();
// The time when the watermark starts to be displayed.
timeline.put("Start", "2");
// The duration for the watermark.
timeline.put("Duration", "ToEND");
watermarkConfig.put("Timeline", timeline);
return watermarkConfig;
}
/**
* Construct the text watermark parameters and modify the corresponding parameter values based on your business requirements.
* @return
*/
public static JSONObject buildTextWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// The content of the text watermark.
watermarkConfig.put("Content", "testwatermark");
// The font of the text watermark.
watermarkConfig.put("FontName", "SimSun");
// The font size of the text watermark.
watermarkConfig.put("FontSize", 25);
// The font color of the text watermark. You can specify a color name or RGB color code, such as #000000.
watermarkConfig.put("FontColor", "Black");
// The transparency of the text watermark.
watermarkConfig.put("FontAlpha", "0.2");
// The stroke color of the text watermark. You can specify a color name or RGB color code, such as #ffffff.
watermarkConfig.put("BorderColor", "White");
// The stroke width of the text watermark.
watermarkConfig.put("BorderWidth", 1);
// The distance between the upper side of the watermark and the upper side of the output video.
watermarkConfig.put("Top", 20);
// The distance between the left side of the watermark and the left side of the output video.
watermarkConfig.put("Left", 15);
return watermarkConfig;
}
Modify a watermark
You can call the UpdateWatermark operation to modify a watermark.
Click UpdateWatermark to learn more about this API operation.
Sample code:
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.UpdateWatermarkResponse;
/**
* Obtain the AccessKey information.
*/
public static DefaultAcsClient initVodClient() throws ClientException {
// Specify the region in which ApsaraVideo VOD is activated.
String regionId = "cn-shanghai";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
// We recommend that you do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources in your account is compromised.
// In this example, the system reads the AccessKey pair from environment variables to implement authentication for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* Modify a watermark.
* Note: The file URL of an image watermark cannot be modified. If you want to modify the URL, you must create a new image watermark.
*/
public static UpdateWatermarkResponse updateWatermark(DefaultAcsClient client) throws Exception {
UpdateWatermarkRequest request = new UpdateWatermarkRequest();
request.setName("updatewatermark");
// The ID of the watermark whose configurations you want to modify.
request.setWatermarkId("421ddddd4f6e734a526fd2****");
// The watermark configurations.
JSONObject watermarkConfig = null;
// The position of the image watermark.
//watermarkConfig = buildImageWatermarkConfig();
// The position of the text watermark.
watermarkConfig = buildTextWatermarkConfig();
request.setWatermarkConfig(watermarkConfig.toJSONString());
return client.getAcsResponse(request);
}
/**
* Sample code
*/
public static void main(String[] args) {
DefaultAcsClient client = initVodClient();
UpdateWatermarkResponse response = new UpdateWatermarkResponse();
try {
response = updateWatermark(client);
// The ID of the watermark.
System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
// The position and effect configurations of the watermark.
System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
// The URL of the watermark file. If you want to use a text watermark, leave this parameter empty.
System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
/**
* Construct the image watermark parameters and modify the corresponding parameter values based on your business requirements.
* @return
*/
public static JSONObject buildImageWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// The horizontal offset of the watermark on the output video.
watermarkConfig.put("Dx", "10");
// The vertical offset of the watermark on the output video.
watermarkConfig.put("Dy", "10");
// The width of the watermark on the output video.
watermarkConfig.put("Width", "66");
// The height of the watermark on the output video.
watermarkConfig.put("Height", "66");
// The relative position of the watermark to the output video. The watermark can be in the upper-left corner, upper-right corner, lower-left corner, or lower-right corner.
watermarkConfig.put("ReferPos", "BottomLeft");
// The timeline of the watermark. You can specify the start time and duration for the watermark.
JSONObject timeline = new JSONObject();
// The time when the watermark starts to be displayed.
timeline.put("Start", "2");
// The duration for the watermark.
timeline.put("Duration", "ToEND");
watermarkConfig.put("Timeline", timeline);
return watermarkConfig;
}
/**
* Construct the text watermark parameters and modify the corresponding parameter values based on your business requirements.
* @return
*/
public static JSONObject buildTextWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// The content of the text watermark.
watermarkConfig.put("Content", "testwatermark");
// The font of the text watermark.
watermarkConfig.put("FontName", "SimSun");
// The font size of the text watermark.
watermarkConfig.put("FontSize", 40);
// The font color of the text watermark. You can specify a color name or RGB color code, such as #000000.
watermarkConfig.put("FontColor", "Black");
// The transparency of the text watermark.
watermarkConfig.put("FontAlpha", "0.2");
// The stroke color of the text watermark. You can specify a color name or RGB color code, such as #ffffff.
watermarkConfig.put("BorderColor", "White");
// The stroke width of the text watermark.
watermarkConfig.put("BorderWidth", 2);
// The distance between the upper side of the watermark and the upper side of the output video.
watermarkConfig.put("Top", 20);
// The distance between the left side of the watermark and the left side of the output video.
watermarkConfig.put("Left", 15);
return watermarkConfig;
}
Delete a watermark
You can call the DeleteWatermark operation to delete a watermark.
Click DeleteWatermark to learn more about this API operation.
Query multiple watermarks
You can call the ListWatermark operation to query multiple watermarks.
Click ListWatermark to learn more about this API operation.
Query a single watermark
You can call the GetWatermark operation to query the details about a single watermark.
Click GetWatermark to learn more about this API operation.
Specify a watermark as the default one
You can call the SetDefaultWatermark operation to specify a watermark as the default one.
Click SetDefaultWatermark to learn more about this API operation.