全部产品

转码模板

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

初始化客户端

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

添加转码模板组

调用AddTranscodeTemplateGroup接口,完成添加转码模板组功能。

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

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"
    "fmt"
    "encoding/json"
)

func MyAddTranscodeTemplateGroup(client *vod.Client) (response *vod.AddTranscodeTemplateGroupResponse, err error) {
    request := vod.CreateAddTranscodeTemplateGroupRequest()

    request.Name = "SampleTranscodeTemplateGroup"

    transcodeTemplateList := BuildTranscodeTemplateList()
    jsonTranscodeConfig, err := json.Marshal(transcodeTemplateList)
    if err != nil {
        fmt.Println("json.Marshal failed:", err)
        return
    }
    request.TranscodeTemplateList = string(jsonTranscodeConfig)

    request.AcceptFormat = "JSON"

    return client.AddTranscodeTemplateGroup(request)
}

// 构建需要添加的转码模板配置数据
func BuildTranscodeTemplateList() (transcodeTemplateList []map[string]interface{}) {

    transcodeTemplate := map[string]interface{}{}

    // 设置模板名称
    transcodeTemplate["TemplateName"] = "MP4-for-Low-Definition"

    // 清晰度
    transcodeTemplate["Definition"] = "LD"

    // 视频流转码配置
    videoConfig := map[string]interface{}{"Width": 640, "Bitrate": 400, "Fps": 25, "Remove": false, "Codec": "H.264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig

    // 音频流转码配置
    audioConfig := map[string]interface{}{"Codec": "AAC", "Bitrate": "64", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig

    // 封装容器
    container := map[string]interface{}{"Format": "mp4"}
    transcodeTemplate["Container"] = container

    // 条件转码配置
    condition := map[string]interface{}{"IsCheckReso": false, "IsCheckResoFail": false, "IsCheckVideoBitrate": false,
        "IsCheckVideoBitrateFail": false, "IsCheckAudioBitrate": false, "IsCheckAudioBitrateFail": false}
    transcodeTemplate["TransConfig"] = condition

    /*
    // 加密配置(只支持HLS)
    encryptSetting := map[string]interface{}{"EncryptType": "Private"}
    transcodeTemplate["EncryptSetting"] = encryptSetting
    */

    // 水印ID(多水印关联)
    watermarkIdList := []string{"USER_DEFAULT_WATERMARK"}
    //watermarkIdList = append(watermarkIdList, "<WatermarkId>")
    transcodeTemplate["WatermarkIds"] = watermarkIdList

    transcodeTemplateList = append(transcodeTemplateList, transcodeTemplate)

    return transcodeTemplateList
}


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

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

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

修改转码模板组

调用UpdateTranscodeTemplateGroup接口,完成修改转码模板组功能。

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

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"
    "fmt"
    "encoding/json"
)

func MyUpdateTranscodeTemplateGroup(client *vod.Client) (response *vod.UpdateTranscodeTemplateGroupResponse, err error) {
    request := vod.CreateUpdateTranscodeTemplateGroupRequest()

    request.TranscodeTemplateGroupId = "<TranscodeTemplateGroupId>"

    // 修改模板组名称,可选
    request.Name = "new-SampleTranscodeTemplateGroup"

    // 修改模板组已有模板的配置,可选
    transcodeTemplateList := BuildNewTranscodeTemplateList()
    jsonTranscodeConfig, err := json.Marshal(transcodeTemplateList)
    if err != nil {
        fmt.Println("json.Marshal failed:", err)
        return
    }
    request.TranscodeTemplateList = string(jsonTranscodeConfig)

    request.AcceptFormat = "JSON"

    return client.UpdateTranscodeTemplateGroup(request)
}

// 构建需要修改的转码模板配置数据
func BuildNewTranscodeTemplateList() (transcodeTemplateList []map[string]interface{}) {

    transcodeTemplate := map[string]interface{}{}

    // 设置转码模板ID,必填
    transcodeTemplate["TranscodeTemplateId"] = "<TranscodeTemplateId>"

    // 以下都是可选项

    // 设置新的模板名称(如果需要更改)
    transcodeTemplate["TemplateName"] = "new-MP4-for-High-Definition"

    // 视频流转码配置
    videoConfig := map[string]interface{}{"Width": 640, "Bitrate": 500, "Fps": 60, "Remove": false, "Codec": "H.264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig

    // 音频流转码配置
    audioConfig := map[string]interface{}{"Codec": "AAC", "Bitrate": "128", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig

    // 封装容器
    container := map[string]interface{}{"Format": "mp4"}
    transcodeTemplate["Container"] = container

    // 条件转码配置
    condition := map[string]interface{}{"IsCheckReso": false, "IsCheckResoFail": false, "IsCheckVideoBitrate": false,
        "IsCheckVideoBitrateFail": false, "IsCheckAudioBitrate": false, "IsCheckAudioBitrateFail": false}
    transcodeTemplate["TransConfig"] = condition

    /*
    // 加密配置(只支持HLS)
    encryptSetting := map[string]interface{}{"EncryptType": "Private"}
    transcodeTemplate["EncryptSetting"] = encryptSetting
    */

    // 水印ID(多水印关联)
    watermarkIdList := []string{"USER_DEFAULT_WATERMARK"}
    //watermarkIdList = append(watermarkIdList, "<WatermarkId>")
    transcodeTemplate["WatermarkIds"] = watermarkIdList

    transcodeTemplateList = append(transcodeTemplateList, transcodeTemplate)

    return transcodeTemplateList
}


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

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

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

查询转码模板组列表

调用ListTranscodeTemplateGroup接口,完成查询转码模板组列表功能。

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

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"
    "fmt"
)

func MyListTranscodeTemplateGroup(client *vod.Client) (response *vod.ListTranscodeTemplateGroupResponse, err error) {
    request := vod.CreateListTranscodeTemplateGroupRequest()

    request.AcceptFormat = "JSON"

    return client.ListTranscodeTemplateGroup(request)
}

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

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

    fmt.Println(response.GetHttpContentString())
    for _, item := range response.TranscodeTemplateGroupList {
        fmt.Printf("%s: %s\n", item.TranscodeTemplateGroupId, item.Name)
    }
}

查询单个转码模板组

调用GetTranscodeTemplateGroup接口,完成查询单个转码模板组功能。

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

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"
    "fmt"
)

func MyGetTranscodeTemplateGroup(client *vod.Client) (response *vod.GetTranscodeTemplateGroupResponse, err error) {
    request := vod.CreateGetTranscodeTemplateGroupRequest()
    request.TranscodeTemplateGroupId = "<TranscodeTemplateGroupId>"

    request.AcceptFormat = "JSON"

    return client.GetTranscodeTemplateGroup(request)
}

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

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

    fmt.Println(response.GetHttpContentString())
    for _, item := range response.TranscodeTemplateGroup.TranscodeTemplateList {
        fmt.Printf("%s: %s\n", item.TranscodeTemplateId, item.TemplateName)
    }
}

设置默认转码模板组

调用SetDefaultTranscodeTemplateGroup接口,完成设置默认转码模板组功能。

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

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"
    "fmt"
)

func MySetDefaultTranscodeTemplateGroup(client *vod.Client) (response *vod.SetDefaultTranscodeTemplateGroupResponse, err error) {
    request := vod.CreateSetDefaultTranscodeTemplateGroupRequest()
    request.TranscodeTemplateGroupId = "<TranscodeTemplateGroupId>"

    request.AcceptFormat = "JSON"

    return client.SetDefaultTranscodeTemplateGroup(request)
}

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

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

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

删除转码模板组

调用DeleteTranscodeTemplateGroup接口,完成删除转码模板组功能。

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

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"
    "fmt"
)

func MyDeleteTranscodeTemplateGroup(client *vod.Client) (response *vod.DeleteTranscodeTemplateGroupResponse, err error) {
    request := vod.CreateDeleteTranscodeTemplateGroupRequest()
    request.TranscodeTemplateGroupId = "<TranscodeTemplateGroupId>"

    request.AcceptFormat = "JSON"

    return client.DeleteTranscodeTemplateGroup(request)
}

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

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

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