All Products
Search
Document Center

ApsaraVideo VOD:Video watermarks

Last Updated:Aug 22, 2023

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 Python. You can call the API operations to add, modify, delete, and query watermarks. You can also specify a watermark as the default one.

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.

Sample code:

Note
  • For more information about the file 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.

from aliyunsdkvod.request.v20170321 import AddWatermarkRequest
def add_watermark(clt):
    request = AddWatermarkRequest.AddWatermarkRequest()

    request.set_Name('watermark-sample')
    # If you want to use an image watermark, the image must be stored in an OSS bucket and the image file and source video file must be stored in the same region, such as China (Shanghai).
    # Example of FileUrl: http://example-bucket-****.oss-cn-shanghai.aliyuncs.com/watermark/test-****.png.  
    request.set_FileUrl('<your File URL>')

    # Example on how to configure a text watermark:
    request.set_Type('Text')
    # Specify the text content, font, size, color, transparency, and other configurations.
    watermarkConfig = {'Content': 'watermark Text', 'FontName': 'SimSun', 'FontSize': 25, 'FontColor': 'Black',
                       'FontAlpha': 0.2, 'BorderColor': 'White', 'BorderWidth': 1, 'Top': 20, 'Left': 15}
    request.set_WatermarkConfig(json.dumps(watermarkConfig))

    """
    # Example on how to configure an image watermark:
    request.set_Type('Image')
    # The start time and duration of the watermark.
    timeline = {'Start': 2, 'Duration': 'ToEND'}
    # The position configurations of the watermark.
    watermarkConfig = {'Dx': 8, 'Dy': 8, 'Width': 55, 'Height': 55, 'ReferPos': 'BottomRight', 'Timeline': timeline}
    request.set_WatermarkConfig(json.dumps(watermarkConfig))
    """

    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client()
    watermark = add_watermark(clt)
    print(json.dumps(watermark, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Modify a watermark

You can call the UpdateWatermark operation to modify a watermark.

Click UpdateWatermark to learn more about this API operation.

Sample code:

Important

You cannot call this operation to modify the URL of the image watermark. If you want to modify the URL of the image watermark, create a watermark.

from aliyunsdkvod.request.v20170321 import UpdateWatermarkRequest
def update_watermark(clt):
    request = UpdateWatermarkRequest.UpdateWatermarkRequest()
    request.set_WatermarkId('<watermarkId>')
    request.set_Name('new-watermark-name')

    # Example on how to modify a text watermark:
    # Specify the text content, font, size, color, transparency, and other configurations.
    watermarkConfig = {'Content': 'watermark Text', 'FontName': 'SimSun', 'FontSize': 25, 'FontColor': 'Black',
                       'FontAlpha': 0.2, 'BorderColor': 'White', 'BorderWidth': 1, 'Top': 20, 'Left': 15}
    request.set_WatermarkConfig(json.dumps(watermarkConfig))

    """
    # Example on how to modify an image watermark:
    # The start time and duration of the watermark.
    timeline = {'Start': 2, 'Duration': 'ToEND'}
    # The position configurations of the watermark.
    watermarkConfig = {'Dx': 8, 'Dy': 8, 'Width': 55, 'Height': 55, 'ReferPos': 'BottomRight', 'Timeline': timeline}
    request.set_WatermarkConfig(json.dumps(watermarkConfig))
    """

    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client()
    watermark = update_watermark(clt)
    print(json.dumps(watermark, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Delete a watermark

You can call the DeleteWatermark operation to delete a watermark.

Click DeleteWatermark to learn more about this API operation.

Query watermarks

  • You can call the GetWatermark operation to query the details about a single watermark.

    Click GetWatermark to learn more about this API operation.

  • You can call the ListWatermark operation to query multiple watermarks.

    Click ListWatermark 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.