全部产品

视频水印

本篇文档提供了Python SDK视频水印模块相关功能的API调用示例。包含添加水印、修改水印、删除水印、查询水印、设置默认水印等。

初始化客户端

使用前请先初始化客户端,请参见初始化

添加水印

调用AddWatermark接口,完成添加水印功能。

接口参数和返回字段请参见AddWatermark。调用示例如下:

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

    request.set_Name('watermark-sample')
    # 图片水印必须传图片文件的OSS地址,且水印文件需要和视频在同一区域,如:华东2的视频添加水印,水印文件必须存放在华东2
    request.set_FileUrl('http://sample.oss-cn-shanghai.aliyuncs.com/watermark/test.png')

    # 文字水印设置示例
    request.set_Type('Text')
    # 水印的文本内容、字体、大小、颜色、透明度等配置
    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))

    """
    # 图片水印设置示例
    request.set_Type('Image')
    # 水印显示的开始时间和结束时间
    timeline = {'Start': 2, 'Duration': 'ToEND'}
    # 水印的位置等配置
    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('<AccessKeyId>', '<AccessKeySecret>')
    watermark = add_watermark(clt)
    print(json.dumps(watermark, ensure_ascii=False, indent=4))

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

修改水印

调用UpdateWatermark接口,完成修改水印功能。

接口参数和返回字段请参见UpdateWatermark。调用示例如下:

注意

本接口不支持图片水印图片文件地址的修改,如要更换请创建新的水印。

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

    # 更改文字水印设置示例
    # 水印的文本内容、字体、大小、颜色、透明度等配置
    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))

    """
    # 更改图片水印设置示例
    # 水印显示的开始时间和结束时间
    timeline = {'Start': 2, 'Duration': 'ToEND'}
    # 水印的位置等配置
    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('<AccessKeyId>', '<AccessKeySecret>')
    watermark = update_watermark(clt)
    print(json.dumps(watermark, ensure_ascii=False, indent=4))

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

删除水印

调用DeleteWatermark接口,完成删除水印功能。

接口参数和返回字段请参见DeleteWatermark。调用示例如下:

from aliyunsdkvod.request.v20170321 import DeleteWatermarkRequest
def delete_watermark(clt):
    request = DeleteWatermarkRequest.DeleteWatermarkRequest()
    request.set_WatermarkId('<watermarkId>')

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    res = delete_watermark(clt)
    print(json.dumps(res, ensure_ascii=False, indent=4))

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

查询水印列表

调用ListWatermark接口,完成查询水印列表功能。

接口参数和返回字段请参见ListWatermark。调用示例如下:

from aliyunsdkvod.request.v20170321 import ListWatermarkRequest
def list_watermark(clt):
    request = ListWatermarkRequest.ListWatermarkRequest()
    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    watermarks = list_watermark(clt)
    print(watermarks['WatermarkInfos'])
    print(json.dumps(watermarks, ensure_ascii=False, indent=4))

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

查询单个水印

调用GetWatermark接口,完成查询单个水印功能。

接口参数和返回字段请参见GetWatermark。调用示例如下:

from aliyunsdkvod.request.v20170321 import GetWatermarkRequest
def get_watermark(clt):
    request = GetWatermarkRequest.GetWatermarkRequest()
    request.set_WatermarkId('<watermarkId>')

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    watermark = get_watermark(clt)
    print(watermark['WatermarkInfo'])
    print(json.dumps(watermark, ensure_ascii=False, indent=4))

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

设置默认水印

调用SetDefaultWatermark接口,完成设置默认水印功能。

接口参数和返回字段请参见SetDefaultWatermark。调用示例如下:

from aliyunsdkvod.request.v20170321 import SetDefaultWatermarkRequest
def set_default_watermark(clt):
    request = SetDefaultWatermarkRequest.SetDefaultWatermarkRequest()
    request.set_WatermarkId('<watermarkId>')

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    res = set_default_watermark(clt)
    print(json.dumps(res, ensure_ascii=False, indent=4))

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