在視頻中添加可見的明水印(例如,企業(yè)Logo、電視臺臺標),可以突出品牌和版權,增加產品識別度。媒體處理支持圖片水印、動畫水印和文字水印三種水印類型,您可以按需選擇。本文提供了Go SDK為視頻添加可見明水印相關功能的API調用示例,包含創(chuàng)建水印模板、提交文字水印作業(yè)、提交圖片水印作業(yè)。
前提條件
使用前請先初始化客戶端,詳細說明請參見初始化。
創(chuàng)建水印模板
水印模板是一系列包含水印位置、大小的參數,使用水印模板,可以幫助您簡化開發(fā)操作。調用AddWaterMarkTemplate接口,完成水印模板創(chuàng)建。
/**
* 創(chuàng)建水印模板
* @param client
* @return
*/
func AddWaterMarkTemplate(client *mts.Client) (*mts.AddWaterMarkTemplateResponse, error) {
request := mts.CreateAddWaterMarkTemplateRequest()
//水印參數詳情, 參考 http://bestwisewords.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提交作業(yè)時Object需經URLEncode,否則會導致作業(yè)失敗。更多信息,請參見URL Encoding說明。
請按照規(guī)范填寫文件名稱,否則會找不到文件導致作業(yè)失敗。詳細名稱規(guī)范,請參見參數詳情。
建議您在提交水印作業(yè)時記錄任務的JobID,便于后續(xù)進行相關操作。
文字水印
/**
* 提交文字水印轉碼作業(yè)
* @param client
* @return
*/
func SubmitTextJobs(client *mts.Client) (*mts.SubmitJobsResponse, error) {
request := mts.CreateSubmitJobsRequest()
//構建input, 需要保證Location區(qū)域和服務client區(qū)域一致
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的所在區(qū)域,格式: 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)
}
圖片水印
/**
* 提交圖片水印轉碼作業(yè)
* @param client
* @return
*/
func SubmitImageJobs(client *mts.Client) (*mts.SubmitJobsResponse, error) {
request := mts.CreateSubmitJobsRequest()
//構建input, 需要保證Location區(qū)域和服務client區(qū)域一致
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的所在區(qū)域,格式: oss-cn-****, 例如北京oss-cn-beijing
request.OutputLocation = "oss-cn-beijing"
//添加圖片水印素材
//圖片Object可按需替換為png靜態(tài)圖片、png動圖(文件后擴展名需為apng)、mov、gif文件,當添加的圖片水印素材為非靜態(tài)圖片時,文件擴展名需小寫
inputFile := map[string]string{
"Location": "oss-cn-beijing",
"Bucket": "<your bucket name>",
"Object": url.QueryEscape("動態(tài)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"
)
/**
* 創(chuàng)建水印模板
* @param client
* @return
*/
func AddWaterMarkTemplate(client *mts.Client) (*mts.AddWaterMarkTemplateResponse, error) {
request := mts.CreateAddWaterMarkTemplateRequest()
//水印參數詳情, 參考 http://bestwisewords.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)
}
/**
* 提交文字水印轉碼作業(yè)
* @param client
* @return
*/
func SubmitTextJobs(client *mts.Client) (*mts.SubmitJobsResponse, error) {
request := mts.CreateSubmitJobsRequest()
//構建input, 需要保證Location區(qū)域和服務client區(qū)域一致
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的所在區(qū)域,格式: 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)
}
/**
* 提交圖片水印轉碼作業(yè)
* @param client
* @return
*/
func SubmitImageJobs(client *mts.Client) (*mts.SubmitJobsResponse, error) {
request := mts.CreateSubmitJobsRequest()
//構建input, 需要保證Location區(qū)域和服務client區(qū)域一致
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的所在區(qū)域,格式: oss-cn-****, 例如北京oss-cn-beijing
request.OutputLocation = "oss-cn-beijing"
//添加圖片水印素材
//圖片Object可按需替換為png靜態(tài)圖片、png動圖(文件后擴展名需為apng)、mov、gif文件,當素材為非靜態(tài)圖片時,文件擴展名需小寫
inputFile := map[string]string{
"Location": "oss-cn-beijing",
"Bucket": "<your bucket name>",
"Object": url.QueryEscape("動態(tài)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, 預置模板參考 http://bestwisewords.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)
}
相關文檔
文檔內容是否對您有幫助?