This topic provides examples on how to use ApsaraVideo for Media Processing (MPS) SDK for Go V2.0 to manage transcoding templates. For example, you can create, modify, delete, and query a transcoding template.
Prerequisites
An SDK client is initialized. For more information, see Initialize a client.
Create a transcoding template
You can call the AddTemplate operation to create a transcoding template.
If an error
The resource "Template" quota has been used up
is reported when you add a transcoding template, your quota for templates is used up. You can submit a ticket to apply for a higher quota.We recommend that you record the ID of a transcoding template. You can also create a transcoding template and obtain the transcoding template ID in the MPS 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:
//
// Use your AccessKey ID and AccessKey secret to initialize a client.
//
// @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 configured.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
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 transcoding configuration for video streams.
Video: tea.String("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10s\"}"),
// The transcoding configuration for audio streams.
Audio: tea.String("{\"Codec\":\"H.264\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}"),
// The general transcoding configuration.
TransConfig: tea.String("{\"TransMode\":\"onepass\"}"),
// The segment configuration.
MuxConfig: tea.String("{\"Segment\":{\"Duration\":\"10\"}}"),
}
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// Write your own code to display the response of the API operation if necessary.
_, _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())
}
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
// The error message.
fmt.Println(tea.StringValue(error.Message))
// The URL of the corresponding error diagnostics page.
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)
}
}
Modify the configurations of a transcoding template
You can call the UpdateTemplate operation to modify the configurations of 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:
//
// Use your AccessKey ID and AccessKey secret to initialize a client.
//
// @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 configured.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
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 transcoding configuration for video streams.
Video: tea.String("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10\"}"),
// The transcoding configuration for audio streams.
Audio: tea.String("{\"Codec\":\"aac\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}"),
// The transmuxing configuration.
MuxConfig: tea.String("{\"Segment\":{\"Duration\":\"10\"}}"),
// The general transcoding configuration.
TransConfig: tea.String("{\"TransMode\":\"onepass\"}"),
}
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// Write your own code to display the response of the API operation if necessary.
_, _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())
}
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
// The error message.
fmt.Println(tea.StringValue(error.Message))
// The URL of the corresponding error diagnostics page.
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 transcoding template
You can 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:
//
// Use your AccessKey ID and AccessKey secret to initialize a client.
//
// @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 configured.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
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 transcoding template that you want to delete.
TemplateId: tea.String("16f01ad6175e4230ac42bb5182cd****"),
}
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// Write your own code to display the response of the API operation if necessary.
_, _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())
}
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
// The error message.
fmt.Println(tea.StringValue(error.Message))
// The URL of the corresponding error diagnostics page.
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 transcoding templates based on template IDs
You can call the QueryTemplateList operation to query transcoding templates based on template IDs.
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:
//
// Use your AccessKey ID and AccessKey secret to initialize a client.
//
// @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 configured.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
# Specify the endpoint. For more information about endpoints, visit 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 IDs of the transcoding templates that you want 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
}
}()
// Write your own code to display the response of the API operation if necessary.
_, _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())
}
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
// The error message.
fmt.Println(tea.StringValue(error.Message))
// The URL of the corresponding error diagnostics page.
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 transcoding templates based on template status
You can call the SearchTemplate operation to query transcoding templates based on template status.
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:
//
// Use your AccessKey ID and AccessKey secret to initialize a client.
//
// @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 configured.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
# Specify the endpoint. For more information about endpoints, visit 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 status of the transcoding templates that you want to query.
State: tea.String("Normal"),
}
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// Write your own code to display the response of the API operation if necessary.
_, _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())
}
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
// The error message.
fmt.Println(tea.StringValue(error.Message))
// The URL of the corresponding error diagnostics page.
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)
}
}