视频水印

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

初始化客户端

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

添加水印

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

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

说明
package main

import (
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "encoding/json"
    "fmt"
)

func MyAddWatermark(client *vod.Client) (response *vod.AddWatermarkResponse, err error) {
    request := vod.CreateAddWatermarkRequest()

    request.Name = "watermark-sample"

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

    // 文字水印设置示例
    request.Type = "Text"
    // 水印的文本内容、字体、大小、颜色、透明度等配置
    watermarkConfig := map[string]interface{}{"Content": "watermark Text", "FontName": "SimSun", "FontSize": 25,
        "FontColor": "Black", "FontAlpha": 0.2, "BorderColor": "White", "BorderWidth": 1, "Top": 20, "Left": 15}
    jsonConfig, err := json.Marshal(watermarkConfig)
    if err != nil {
        fmt.Println("json.Marshal failed:", err)
        return
    }
    request.WatermarkConfig = string(jsonConfig)

    /* 
    // 图片水印设置示例
    request.Type = "Image"
    // 水印显示的开始时间和结束时间
    timeline := map[string]interface{}{"Start": 2, "Duration": "ToEND"}
    // 水印的位置等配置
    watermarkConfig := map[string]interface{}{"Dx": 8, "Dy": 8, "Width": 55, "Height": 55, "ReferPos": "BottomRight", "Timeline": timeline}
    jsonConfig, err := json.Marshal(watermarkConfig)
    if err != nil {
        fmt.Println("json.Marshal failed:", err)
        return
    }
    request.WatermarkConfig = string(jsonConfig)
    */

    request.AcceptFormat = "JSON"

    return client.AddWatermark(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyAddWatermark(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Println(response.RequestId)
    fmt.Printf("%s: %s\n", response.WatermarkInfo.WatermarkId, response.WatermarkInfo.WatermarkConfig)
}

修改水印

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

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

注意

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

package main

import (
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "encoding/json"
    "fmt"
)

func MyUpdateWatermark(client *vod.Client) (response *vod.UpdateWatermarkResponse, err error) {
    request := vod.CreateUpdateWatermarkRequest()

    request.WatermarkId = "5a5abf8e458a9d5c97dea86e686cdf18"
    request.Name = "new-watermark-name"

    // 更改文字水印设置示例
    // 水印的文本内容、字体、大小、颜色、透明度等配置
    watermarkConfig := map[string]interface{}{"Content": "New watermark Text", "FontName": "SimSun", "FontSize": 25,
        "FontColor": "Black", "FontAlpha": 0.2, "BorderColor": "White", "BorderWidth": 1, "Top": 20, "Left": 15}
    jsonConfig, err := json.Marshal(watermarkConfig)
    if err != nil {
        fmt.Println("json.Marshal failed:", err)
        return
    }
    request.WatermarkConfig = string(jsonConfig)

    /*
    // 更改图片水印设置示例
    // 水印显示的开始时间和结束时间
    timeline := map[string]interface{}{"Start": 2, "Duration": "ToEND"}
    // 水印的位置等配置
    watermarkConfig := map[string]interface{}{"Dx": 8, "Dy": 8, "Width": 55, "Height": 55, "ReferPos": "BottomRight",
        "Timeline": timeline}
    jsonConfig, err := json.Marshal(watermarkConfig)
    if err != nil {
        fmt.Println("json.Marshal failed:", err)
        return
    }
    request.WatermarkConfig = string(jsonConfig)
    */

    request.AcceptFormat = "JSON"

    return client.UpdateWatermark(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyUpdateWatermark(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Println(response.RequestId)
    fmt.Printf("%s: %s\n", response.WatermarkInfo.WatermarkId, response.WatermarkInfo.WatermarkConfig)
}

删除水印

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

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

package main

import (
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "encoding/json"
    "fmt"
)

func MyDeleteWatermark(client *vod.Client) (response *vod.DeleteWatermarkResponse, err error) {
    request := vod.CreateDeleteWatermarkRequest()
    request.WatermarkId = "<WatermarkId>"

    request.AcceptFormat = "JSON"

    return client.DeleteWatermark(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyDeleteWatermark(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Println(response.RequestId)
}

查询水印列表

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

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

package main

import (
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "encoding/json"
    "fmt"
)

func MyListWatermark(client *vod.Client) (response *vod.ListWatermarkResponse, err error) {
    request := vod.CreateListWatermarkRequest()

    request.AcceptFormat = "JSON"

    return client.ListWatermark(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyListWatermark(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Println(response.RequestId)
    for _, watermark := range response.WatermarkInfos {
        fmt.Printf("%s: %s %s\n", watermark.WatermarkId, watermark.Name, watermark.WatermarkConfig)
    }
}

查询单个水印

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

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

package main

import (
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "encoding/json"
    "fmt"
)

func MyGetWatermark(client *vod.Client) (response *vod.GetWatermarkResponse, err error) {
    request := vod.CreateGetWatermarkRequest()
    request.WatermarkId = "<WatermarkId>"

    request.AcceptFormat = "JSON"

    return client.GetWatermark(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyGetWatermark(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Println(response.RequestId)
    watermark := response.WatermarkInfo
    fmt.Printf("%s: %s %s\n", response.WatermarkInfo.WatermarkId, watermark.Name, watermark.WatermarkConfig)
}

设置默认水印

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

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

package main

import (
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "encoding/json"
    "fmt"
)

func MySetDefaultWatermark(client *vod.Client) (response *vod.SetDefaultWatermarkResponse, err error) {
    request := vod.CreateSetDefaultWatermarkRequest()
    request.WatermarkId = "<WatermarkId>"

    request.AcceptFormat = "JSON"

    return client.SetDefaultWatermark(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MySetDefaultWatermark(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Println(response.RequestId)
}