Tous les produits
Search
Centre de documentation

ApsaraVideo Media Processing:Modèles de transcodage

Dernière mise à jour :Aug 10, 2026

Gérez les modèles de transcodage avec le SDK Go V2.0. Les exemples suivants montrent comment ajouter, mettre à jour, supprimer et interroger des modèles.

Prérequis

Le client SDK est initialisé. Pour plus d'informations, consultez la rubrique Initialisation d'un client.

Ajouter un modèle

Appelez l'opération AddTemplate pour ajouter un modèle.

Remarque
  • Si l'erreur The resource "Template" quota has been used up s'affiche lors de l'ajout d'un modèle, votre quota de modèles de transcodage est épuisé. Pour augmenter ce quota, soumettez un ticket.

  • Après avoir ajouté un modèle, notez l'ID du modèle de transcodage présent dans la valeur de retour. Vous pouvez également créer des modèles de transcodage et obtenir leurs ID dans la console. Pour plus d'informations, consultez la rubrique Modèles de transcodage.

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)
  }
}

Mettre à jour la configuration d'un modèle

Appelez l'opération UpdateTemplate pour mettre à jour la configuration d'un modèle de transcodage.

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)
  }
}

Supprimer un modèle

Appelez l'opération DeleteTemplate pour supprimer un modèle de transcodage.

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)
  }
}

Interroger des modèles par ID

Appelez l'opération QueryTemplateList pour interroger des modèles par 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)
  }
}

Interroger des modèles par état

Appelez l'opération SearchTemplate pour interroger des modèles par état.

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)
  }
}