All Products
Search
Document Center

ApsaraVideo Media Processing:Transcoding templates

Last Updated:Jun 18, 2026

Manage transcoding templates by using Go SDK V2.0. The following examples show how to add, update, delete, and query templates.

Prerequisites

An SDK client is initialized. For more information, see Initialize a client.

Add a template

Call the AddTemplate operation to add a template.

Note
  • If the The resource "Template" quota has been used up error is returned when you add a template, your transcoding template quota is exhausted. To increase the quota, submit a ticket.

  • After you add a template, record the transcoding template ID from the return value. Alternatively, you can create transcoding templates and obtain their IDs in the console. For more information, see Transcoding templates.

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


// Description:
// 
// Initialize the client with an AccessKey pair.
// 
// @return Client
// 
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

  config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
    AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }

  config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
  _result = &mts20140618.Client{}
  _result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  client, _err := CreateClient()
  if _err != nil {
    return _err
  }

  addTemplateRequest := &mts20140618.AddTemplateRequest{
    // The name of the template.
    Name: tea.String("mps-example"),
    // The container format.
    Container: tea.String("{\"Format\":\"mp4\"}"),
    // The video stream configurations.
    Video: tea.String("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10s\"}"),
    // The audio stream configurations.
    Audio: tea.String("{\"Codec\":\"H.264\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}"),
    // The general transcoding configurations.
    TransConfig: tea.String("{\"TransMode\":\"onepass\"}"),
    // The segment configurations.
    MuxConfig: tea.String("{\"Segment\":{\"Duration\":\"10\"}}"),
  }
  runtime := &util.RuntimeOptions{}
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // If you copy the code to run, print the return value of the API call.
    _, _err = client.AddTemplateWithOptions(addTemplateRequest, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // The following code is for demonstration purposes only. Handle exceptions with caution. Do not ignore exceptions in your project.
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The diagnosis address.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}

Update a template configuration

Call the UpdateTemplate operation to update a transcoding template configuration.

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


// Description:
//
// Initialize the client with an AccessKey pair.
//
// @return Client
//
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

  config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
    AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }

  config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
  _result = &mts20140618.Client{}
  _result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  client, _err := CreateClient()
  if _err != nil {
    return _err
  }

  updateTemplateRequest := &mts20140618.UpdateTemplateRequest{
    // The ID of the template.
    TemplateId: tea.String("16f01ad6175e4230ac42bb5182cd****"),
    // The name of the template.
    Name: tea.String("MPS-example"),
    // The container format.
    Container: tea.String("{\"Format\":\"mp4\"}"),
    // The video stream configurations.
    Video: tea.String("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10\"}"),
    // The audio stream configurations.
    Audio: tea.String("{\"Codec\":\"aac\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}"),
    // The muxing configurations.
    MuxConfig: tea.String("{\"Segment\":{\"Duration\":\"10\"}}"),
    // The general transcoding configurations.
    TransConfig: tea.String("{\"TransMode\":\"onepass\"}"),
  }
  runtime := &util.RuntimeOptions{}
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // If you copy the code to run, print the return value of the API call.
    _, _err = client.UpdateTemplateWithOptions(updateTemplateRequest, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // The following code is for demonstration purposes only. Handle exceptions with caution. Do not ignore exceptions in your project.
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The diagnosis address.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}

Delete a template

Call the DeleteTemplate operation to delete a transcoding template.

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


// Description:
//
// Initialize the client with an AccessKey pair.
//
// @return Client
//
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

  config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
    AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }

  config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
  _result = &mts20140618.Client{}
  _result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  client, _err := CreateClient()
  if _err != nil {
    return _err
  }

  deleteTemplateRequest := &mts20140618.DeleteTemplateRequest{
    // The ID of the custom transcoding template to delete.
    TemplateId: tea.String("16f01ad6175e4230ac42bb5182cd****"),
  }
  runtime := &util.RuntimeOptions{}
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // If you copy the code to run, print the return value of the API call.
    _, _err = client.DeleteTemplateWithOptions(deleteTemplateRequest, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // The following code is for demonstration purposes only. Handle exceptions with caution. Do not ignore exceptions in your project.
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The diagnosis address.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}

Query templates by ID

Call the QueryTemplateList operation to query templates by ID.

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


// Description:
//
// Initialize the client with an AccessKey pair.
//
// @return Client
//
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

  config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
    AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }
  // For more information about endpoints, see https://api.aliyun.com/product/Mts
  config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
  _result = &mts20140618.Client{}
  _result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  client, _err := CreateClient()
  if _err != nil {
    return _err
  }

  queryTemplateListRequest := &mts20140618.QueryTemplateListRequest{
    // The list of template IDs to query.
    TemplateIds: tea.String("16f01ad6175e4230ac42bb5182cd****,88c6ca184c0e424d5w5b665e2a12****"),
  }
  runtime := &util.RuntimeOptions{}
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // If you copy the code to run, print the return value of the API call.
    _, _err = client.QueryTemplateListWithOptions(queryTemplateListRequest, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // The following code is for demonstration purposes only. Handle exceptions with caution. Do not ignore exceptions in your project.
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The diagnosis address.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}

Query templates by state

Call the SearchTemplate operation to query templates by state.

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


// Description:
//
// Initialize the client with an AccessKey pair.
//
// @return Client
//
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

  config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
    AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }
  // For more information about endpoints, see https://api.aliyun.com/product/Mts
  config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
  _result = &mts20140618.Client{}
  _result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  client, _err := CreateClient()
  if _err != nil {
    return _err
  }

  searchTemplateRequest := &mts20140618.SearchTemplateRequest{
    // The state of the transcoding templates to search for.
    State: tea.String("Normal"),
  }
  runtime := &util.RuntimeOptions{}
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // If you copy the code to run, print the return value of the API call.
    _, _err = client.SearchTemplateWithOptions(searchTemplateRequest, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // The following code is for demonstration purposes only. Handle exceptions with caution. Do not ignore exceptions in your project.
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The diagnosis address.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}