在视频中添加可见的明水印(例如,企业Logo、电视台台标),可以突出品牌和版权,增加产品识别度。媒体处理支持图片水印、动画水印和文字水印三种水印类型,您可以按需选择。本文提供了Go SDK为视频添加可见明水印相关功能的API调用示例,包含创建水印模板、提交文字水印作业、提交图片水印作业。
前提条件
使用前请先初始化客户端,详细说明请参见初始化。
创建水印模板
水印模板是一系列包含水印位置、大小的参数,使用水印模板,可以帮助您简化开发操作。调用AddWaterMarkTemplate接口,完成水印模板创建。
/**
* 创建水印模板
* @param client
* @return
*/
func AddWaterMarkTemplate(client *mts.Client) (*mts.AddWaterMarkTemplateResponse, error) {
request := mts.CreateAddWaterMarkTemplateRequest()
//水印参数详情, 参考 https://help.aliyun.com/document_detail/29253.htm?spm=a2c4g.602851.0.0.4ab731bckAeLdq#section-k53-tt4-8b0
config := map[string]string{
"Dx": "10",
"Dy": "5",
"ReferPos": "TopRight",
}
configJson, _ := json.Marshal(config)
request.Config = string(configJson)
request.Name = "test name"
return client.AddWaterMarkTemplate(request)
}
提交水印任务
为视频添加水印会改变画面的内容,需要对视频重新编码。在媒体处理中您需要调用SubmitJobs接口,提交水印任务。
说明
通过SDK提交作业时Object需经URLEncode,否则会导致作业失败。更多信息,请参见URL Encoding说明。
请按照规范填写文件名称,否则会找不到文件导致作业失败。详细名称规范,请参见参数详情。
建议您在提交水印作业时记录任务的JobID,便于后续进行相关操作。
文字水印
/**
* 提交文字水印转码作业
* @param client
* @return
*/
func SubmitTextJobs(client *mts.Client) (*mts.SubmitJobsResponse, error) {
request := mts.CreateSubmitJobsRequest()
//构建input, 需要保证Location区域和服务client区域一致
input := map[string]string{
"Location": "oss-cn-beijing",
"Bucket": "<your bucket name>",
"Object": url.QueryEscape("mps-test/demo/test.mp4"),
}
inputJson, _ := json.Marshal(input)
request.Input = string(inputJson)
//指定输出Bucket
request.OutputBucket = "<your bucket name>"
//输出Bucket的所在区域,格式: oss-cn-****, 例如北京oss-cn-beijing
request.OutputLocation = "oss-cn-beijing"
//构建文字水印输出配置
//Content为文字水印内容,内容需做Base64编码
waterMark := map[string]string{
"Type": "Text",
"TextWaterMark": `{"Content":"5rWL6K+V5paH5a2X5rC05Y2w","FontName":"SimSun","FontSize":"16","Top":2,"Left":10}`,
}
waterMarks := [...]map[string]string{waterMark}
//构建一个输出对象
output := map[string]interface{}{
"OutputObject": url.QueryEscape("mps-test/demo/test-out.mp4"),
"TemplateId": templateId,
"WaterMarks": waterMarks,
}
outputs := [...]map[string]interface{}{output}
outputsJson, _ := json.Marshal(outputs)
request.Outputs = string(outputsJson)
// PipelineId
request.PipelineId = pipelineId
return client.SubmitJobs(request)
}
图片水印
/**
* 提交图片水印转码作业
* @param client
* @return
*/
func SubmitImageJobs(client *mts.Client) (*mts.SubmitJobsResponse, error) {
request := mts.CreateSubmitJobsRequest()
//构建input, 需要保证Location区域和服务client区域一致
input := map[string]string{
"Location": "oss-cn-beijing",
"Bucket": "<your bucket name>",
"Object": url.QueryEscape("mps-test/demo/test.mp4"),
}
inputJson, _ := json.Marshal(input)
request.Input = string(inputJson)
//指定输出Bucket
request.OutputBucket = "<your bucket name>"
//输出Bucket的所在区域,格式: oss-cn-****, 例如北京oss-cn-beijing
request.OutputLocation = "oss-cn-beijing"
//添加图片水印素材
//图片Object可按需替换为png静态图片、png动图(文件后扩展名需为apng)、mov、gif文件,当添加的图片水印素材为非静态图片时,文件扩展名需小写
inputFile := map[string]string{
"Location": "oss-cn-beijing",
"Bucket": "<your bucket name>",
"Object": url.QueryEscape("动态logo.apng"),
}
inputFileJson, _ := json.Marshal(inputFile)
//构建图片水印输出配置
//水印数组大小上限为4,即同一路输出最多支持4个水印
waterMark := map[string]string{
"WaterMarkTemplateId": "23d7a99796fad2bc****",
"Type": "Image",
"Width": "200",
"Height": "100",
"InputFile": string(inputFileJson),
}
waterMarks := [...]map[string]string{waterMark}
//构建一个输出对象
output := map[string]interface{}{
"OutputObject": url.QueryEscape("mps-test/demo/test-out.mp4"),
"TemplateId": templateId,
"WaterMarks": waterMarks,
}
outputs := [...]map[string]interface{}{output}
outputsJson, _ := json.Marshal(outputs)
request.Outputs = string(outputsJson)
// PipelineId
request.PipelineId = pipelineId
return client.SubmitJobs(request)
}
完整代码
package main
import (
"encoding/json"
"fmt"
"net/url"
mts "github.com/aliyun/alibaba-cloud-sdk-go/services/mts"
)
/**
* 创建水印模板
* @param client
* @return
*/
func AddWaterMarkTemplate(client *mts.Client) (*mts.AddWaterMarkTemplateResponse, error) {
request := mts.CreateAddWaterMarkTemplateRequest()
//水印参数详情, 参考 https://help.aliyun.com/document_detail/29253.htm?spm=a2c4g.602851.0.0.4ab731bckAeLdq#section-k53-tt4-8b0
config := map[string]string{
"Dx": "10",
"Dy": "5",
"ReferPos": "TopRight",
}
configJson, _ := json.Marshal(config)
request.Config = string(configJson)
request.Name = "test name"
return client.AddWaterMarkTemplate(request)
}
/**
* 提交文字水印转码作业
* @param client
* @return
*/
func SubmitTextJobs(client *mts.Client) (*mts.SubmitJobsResponse, error) {
request := mts.CreateSubmitJobsRequest()
//构建input, 需要保证Location区域和服务client区域一致
input := map[string]string{
"Location": "oss-cn-beijing",
"Bucket": "<your bucket name>",
"Object": url.QueryEscape("mps-test/demo/test.mp4"),
}
inputJson, _ := json.Marshal(input)
request.Input = string(inputJson)
//指定输出Bucket
request.OutputBucket = "<your bucket name>"
//输出Bucket的所在区域,格式: oss-cn-****, 例如北京oss-cn-beijing
request.OutputLocation = "oss-cn-beijing"
//构建文字水印输出配置
//Content为文字水印内容,内容需做Base64编码
waterMark := map[string]string{
"Type": "Text",
"TextWaterMark": `{"Content":"5rWL6K+V5paH5a2X5rC05Y2w","FontName":"SimSun","FontSize":"16","Top":2,"Left":10}`,
}
waterMarks := [...]map[string]string{waterMark}
//构建一个输出对象
output := map[string]interface{}{
"OutputObject": url.QueryEscape("mps-test/demo/test-out.mp4"),
"TemplateId": templateId,
"WaterMarks": waterMarks,
}
outputs := [...]map[string]interface{}{output}
outputsJson, _ := json.Marshal(outputs)
request.Outputs = string(outputsJson)
// PipelineId
request.PipelineId = pipelineId
return client.SubmitJobs(request)
}
/**
* 提交图片水印转码作业
* @param client
* @return
*/
func SubmitImageJobs(client *mts.Client) (*mts.SubmitJobsResponse, error) {
request := mts.CreateSubmitJobsRequest()
//构建input, 需要保证Location区域和服务client区域一致
input := map[string]string{
"Location": "oss-cn-beijing",
"Bucket": "<your bucket name>",
"Object": url.QueryEscape("mps-test/demo/test.mp4"),
}
inputJson, _ := json.Marshal(input)
request.Input = string(inputJson)
//指定输出Bucket
request.OutputBucket = "<your bucket name>"
//输出Bucket的所在区域,格式: oss-cn-****, 例如北京oss-cn-beijing
request.OutputLocation = "oss-cn-beijing"
//添加图片水印素材
//图片Object可按需替换为png静态图片、png动图(文件后扩展名需为apng)、mov、gif文件,当素材为非静态图片时,文件扩展名需小写
inputFile := map[string]string{
"Location": "oss-cn-beijing",
"Bucket": "<your bucket name>",
"Object": url.QueryEscape("动态logo.apng"),
}
inputFileJson, _ := json.Marshal(inputFile)
//构建图片水印输出配置
//水印数组大小上限为4,即同一路输出最多支持4个水印
waterMark := map[string]string{
"WaterMarkTemplateId": "23d7a9549d796****",
"Type": "Image",
"Width": "200",
"Height": "100",
"InputFile": string(inputFileJson),
}
waterMarks := [...]map[string]string{waterMark}
//构建一个输出对象
output := map[string]interface{}{
"OutputObject": url.QueryEscape("mps-test/demo/test-out.mp4"),
"TemplateId": templateId,
"WaterMarks": waterMarks,
}
outputs := [...]map[string]interface{}{output}
outputsJson, _ := json.Marshal(outputs)
request.Outputs = string(outputsJson)
// PipelineId
request.PipelineId = pipelineId
return client.SubmitJobs(request)
}
const (
//模板ID, 预置模板参考 https://help.aliyun.com/document_detail/29256.html
templateId = "S00000001-200010"
//管道ID, 可以在MPS控制台 > 全局设置 > 管道查看
pipelineId = "bee7a5b5bfe40a0cbf****"
)
func main() {
//初始化调用 client
client, err := InitMtsClient()
if err != nil {
panic(err)
}
response, err := SubmitImageJobs(client)
if err != nil {
panic(err)
}
fmt.Println("RequestId is:", response.RequestId)
//fmt.Println("JobId is:", response.JobResultList.JobResult[0].Job.JobId)
}
相关文档
文档内容是否对您有帮助?