All Products
Search
Document Center

Watermark

Last Updated: Jul 11, 2019

Initialize a client

Initialize a client before using the SDK. For more information, see InitVodClient.

Add a watermark

For more information about the request and response parameters, see AddWatermark.

  • For more information about how to obtain the upload URL and credential for a file, see CreateUploadAttachedMedia.
  • For more information about how to upload a watermark file to Object Storage Service (OSS), see OSS upload.
package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/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"

    // The OSS URL must be specified for an image watermark file. This watermark file must be stored in the same region as the relevant video. For example, if a video is stored in China (Shanghai), its watermark file must also be stored in China (Shanghai).
    request.FileUrl = "http://sample.oss-cn-shanghai.aliyuncs.com/watermark/test.png"

    // Specifies the configuration of a text watermark.
    request.Type = "Text"
    // Specifies the content, font, font size, color, transparency, and other configurations of the text watermark.
    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)

    /* 
    // Specifies the configuration of an image watermark.
    request.Type = "Image"
    // Sets the start time and end time for watermark display.
    timeline := map[string]interface{}{"Start": 2, "Duration": "ToEND"}
    // Specifies the display position and other configurations of the image watermark.
    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)
}

Modify a watermark

For more information about the request and response parameters, see UpdateWatermark.

Note: The URL of an image watermark file cannot be directly modified. If you want to modify the URL, you need to create watermark information again and set the new URL in the information.

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/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"

    // Modifies the configuration of a text watermark.
    // Specifies the content, font, font size, color, transparency, and other configurations of the text watermark.
    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)

    /*
    // Modifies the configuration of an image watermark.
    // Sets the start time and end time for watermark display.
    timeline := map[string]interface{}{"Start": 2, "Duration": "ToEND"}
    // Specifies the display position and other configurations of the image watermark.
    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)
}

Delete a watermark

For more information about the request and response parameters, see DeleteWatermark.

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/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)
}

Query a list of watermarks

For more information about the request and response parameters, see ListWatermark.

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/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)
    }
}

Query a watermark

For more information about the request and response parameters, see GetWatermark.

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/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)
}

Set the default watermark

For more information about the request and response parameters, see SetDefaultWatermark.

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/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)
}