All Products
Search
Document Center

ApsaraVideo Media Processing:Manage transcoding templates

Last Updated:Jul 04, 2023

This topic provides examples on how to use ApsaraVideo for Media Processing (MPS) SDK for Go to call the API operations of MPS to manage transcoding templates. For example, you can create, modify, delete, and query a transcoding template.

Prerequisites

MPS SDK for Java is initialized. For more information, see Initialization.

Create a transcoding template

You can call the AddTemplate operation to create a transcoding template.

Note
  • 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 template.

/**
 * Create a transcoding template.
 * @param client
 * @return
 */
 func AddTemplate(client *mts.Client) (*mts.AddTemplateResponse, error) {
	request := mts.CreateAddTemplateRequest()
	request.Name = "test-template"
	// Configure the parameters related to the container configurations.
	container := map[string]string {
		"Format": "mp4",
	}
	containerJson, _ := json.Marshal(container)
	request.Container = string(containerJson)
	// Configure the parameters related to the video configurations.
	video := map[string]string {
		"Codec": "H.264",
		"Profile": "high",
		"Bitrate": "500",
		"Width": "1280",
		"Height": "720",
		"Fps": "25",
		"Gop": "300",
	}
	videoJson, _ := json.Marshal(video)
	request.Video = string(videoJson)
	// Configure the parameters related to the audio configurations.
	audio := map[string]string {
		"Codec": "AAC",
		"Samplerate": "44100",
		"Bitrate": "500",
		"Channels": "2",
	}
	audioJson, _ := json.Marshal(audio)
	request.Audio = string(audioJson)
	// Configure the parameters related to transcoding configurations.
	transConfig := map[string]string {
		"TransMode": "onepass",
	}
	transConfigJson, _ := json.Marshal(transConfig)
	request.TransConfig = string(transConfigJson)

	return client.AddTemplate(request)
 }

Modify the configurations of a transcoding template

You can call the UpdateTemplate operation to modify the configurations of a transcoding template.

/**
 * Modify the configurations of a transcoding template.
 * @param client
 * @return
 */
 func UpdateTemplate(client *mts.Client) (*mts.UpdateTemplateResponse, error) {
	request := mts.CreateUpdateTemplateRequest()
	// The ID of the transcoding template. You can log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane to view the transcoding template ID. You can also log on to the MPS console to create a transcoding template and record the transcoding template ID.
	request.TemplateId = "606179a72c64ee2b6****"
	request.Name = "update-template"
	// Configure the parameters related to the container configurations.
	container := map[string]string {
		"Format": "m3u8",
	}
	containerJson, _ := json.Marshal(container)
	request.Container = string(containerJson)
	// Configure the parameters related to the video configurations.
	video := map[string]string {
		"Codec": "H.264",
		"Profile": "high",
		"Bitrate": "500",
		"Width": "1280",
		"Height": "720",
		"Fps": "25",
		"Gop": "300",
	}
	videoJson, _ := json.Marshal(video)
	request.Video = string(videoJson)
	// Configure the parameters related to the audio configurations.
	audio := map[string]string {
		"Codec": "AAC",
		"Samplerate": "44100",
		"Bitrate": "500",
		"Channels": "2",
	}
	audioJson, _ := json.Marshal(audio)
	request.Audio = string(audioJson)
	// Configure the parameters related to transcoding configurations.
	transConfig := map[string]string {
		"TransMode": "onepass",
		"IsCheckReso": "true",
	}
	transConfigJson, _ := json.Marshal(transConfig)
	request.TransConfig = string(transConfigJson)
	// Configure the parameters related to packaging configurations.
	muxConfig := map[string]string {
		"Segment": `{"Duration":"10"}`
	}
	muxConfigJson, _ := json.Marshal(muxConfig)
	request.MuxConfig = string(muxConfigJson)
	return client.UpdateTemplate(request)
 }

Delete a transcoding template

You can call the DeleteTemplate operation to delete a transcoding template.

/**
 * Delete a transcoding template.
 * @param client
 * @return
 */
 func DeleteTemplate(client *mts.Client) (*mts.DeleteTemplateResponse, error) {
	request := mts.CreateDeleteTemplateRequest()
	// The ID of the transcoding template. You can log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane to view the transcoding template ID. You can also log on to the MPS console to create a transcoding template and record the transcoding template ID.
	request.TemplateId = "606179a72c64ee2b6****"
	return client.DeleteTemplate(request)
 }

Query transcoding templates based on template IDs

You can call the QueryTemplateList operation to query transcoding templates based on template IDs.

/**
 * Query transcoding templates based on template IDs.
 * @param client
 * @return
 */
 func QueryTemplateList(client *mts.Client) (*mts.QueryTemplateListResponse, error) {
	request := mts.CreateQueryTemplateListRequest()
	// The ID of the transcoding template. You can log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane to view the transcoding template ID. You can also log on to the MPS console to create a transcoding template and record the transcoding template ID.
	// You can query up to 10 transcoding templates at a time. Separate multiple transcoding template IDs with commas (,).
	request.TemplateIds = "81767eb87d36aed796****";
	return client.QueryTemplateList(request)
 }

Query transcoding templates based on the template status

You can call the SearchTemplate operation to query transcoding templates based on the template status.

/**
 * Query transcoding templates based on the template status.
 * @param client
 * @return
 */
 func SearchTemplate(client *mts.Client) (*mts.SearchTemplateResponse, error) {
	request := mts.CreateSearchTemplateRequest()
   // The status of the template. A value of All indicates that all transcoding templates are queried. A value of Normal indicates that normal transcoding templates are queried. A value of Deleted indicates that deleted transcoding templates are queried.
	request.State = "Normal";
	return client.SearchTemplate(request)
 }

Complete sample code

package main

import (
	"encoding/json"
	"fmt"
	mts "github.com/aliyun/alibaba-cloud-sdk-go/services/mts"
)

/**
 * Create a transcoding template.
 * @param client
 * @return
 */
 func AddTemplate(client *mts.Client) (*mts.AddTemplateResponse, error) {
	request := mts.CreateAddTemplateRequest()
	request.Name = "test-template"
	// Configure the parameters related to the container configurations.
	container := map[string]string {
		"Format": "mp4",
	}
	containerJson, _ := json.Marshal(container)
	request.Container = string(containerJson)
	// Configure the parameters related to the video configurations.
	video := map[string]string {
		"Codec": "H.264",
		"Profile": "high",
		"Bitrate": "500",
		"Width": "1280",
		"Height": "720",
		"Fps": "25",
		"Gop": "300",
	}
	videoJson, _ := json.Marshal(video)
	request.Video = string(videoJson)
	// Configure the parameters related to the audio configurations.
	audio := map[string]string {
		"Codec": "AAC",
		"Samplerate": "44100",
		"Bitrate": "500",
		"Channels": "2",
	}
	audioJson, _ := json.Marshal(audio)
	request.Audio = string(audioJson)
	// Configure the parameters related to transcoding configurations.
	transConfig := map[string]string {
		"TransMode": "onepass",
	}
	transConfigJson, _ := json.Marshal(transConfig)
	request.TransConfig = string(transConfigJson)

	return client.AddTemplate(request)
 }

/**
 * Modify the configurations of a transcoding template.
 * @param client
 * @return
 */
 func UpdateTemplate(client *mts.Client) (*mts.UpdateTemplateResponse, error) {
	request := mts.CreateUpdateTemplateRequest()
	// The ID of the transcoding template. You can log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane to view the transcoding template ID. You can also create a transcoding template by calling the AddTemplate operation and record the transcoding template ID.
	request.TemplateId = "606179a72c64ee2b6****"
	request.Name = "update-template"
	// Configure the parameters related to the container configurations.
	container := map[string]string {
		"Format": "m3u8",
	}
	containerJson, _ := json.Marshal(container)
	request.Container = string(containerJson)
	// Configure the parameters related to the video configurations.
	video := map[string]string {
		"Codec": "H.264",
		"Profile": "high",
		"Bitrate": "500",
		"Width": "1280",
		"Height": "720",
		"Fps": "25",
		"Gop": "300",
	}
	videoJson, _ := json.Marshal(video)
	request.Video = string(videoJson)
	// Configure the parameters related to the audio configurations.
	audio := map[string]string {
		"Codec": "AAC",
		"Samplerate": "44100",
		"Bitrate": "500",
		"Channels": "2",
	}
	audioJson, _ := json.Marshal(audio)
	request.Audio = string(audioJson)
	// Configure the parameters related to transcoding configurations.
	transConfig := map[string]string {
		"TransMode": "onepass",
		"IsCheckReso": "true",
	}
	transConfigJson, _ := json.Marshal(transConfig)
	request.TransConfig = string(transConfigJson)
	// Configure the parameters related to packaging configurations.
	muxConfig := map[string]string {
		"Segment": `{"Duration":"10"}`,
	}
	muxConfigJson, _ := json.Marshal(muxConfig)
	request.MuxConfig = string(muxConfigJson)
	return client.UpdateTemplate(request)
 }

/**
 * Delete a transcoding template.
 * @param client
 * @return
 */
 func DeleteTemplate(client *mts.Client) (*mts.DeleteTemplateResponse, error) {
	request := mts.CreateDeleteTemplateRequest()
	// The ID of the transcoding template. You can log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane to view the transcoding template ID. You can also log on to the MPS console to create a transcoding template and record the transcoding template ID.
	request.TemplateId = "606179a72c64ee2b6****"
	return client.DeleteTemplate(request)
 }

/**
 * Query transcoding templates based on template IDs.
 * @param client
 * @return
 */
 func QueryTemplateList(client *mts.Client) (*mts.QueryTemplateListResponse, error) {
	request := mts.CreateQueryTemplateListRequest()
	// The ID of the transcoding template. You can log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane to view the transcoding template ID. You can also log on to the MPS console to create a transcoding template and record the transcoding template ID.
	// You can query up to 10 transcoding templates at a time. Separate multiple transcoding template IDs with commas (,).
	request.TemplateIds = "8176Deletedeb87d36ae****";
	return client.QueryTemplateList(request)
 }

/**
 * Query transcoding templates based on the template status.
 * @param client
 * @return
 */
 func SearchTemplate(client *mts.Client) (*mts.SearchTemplateResponse, error) {
	request := mts.CreateSearchTemplateRequest()
   // The status of the template. A value of All indicates that all transcoding templates are queried. A value of Normal indicates that normal transcoding templates are queried. A value of Deleted indicates that deleted transcoding templates are queried.
	request.State = "Normal";
	return client.SearchTemplate(request)
 }

func main() {
	// Initialize the SDK client.
	client, err := InitMtsClient()
	if err != nil {
		panic(err)
	}
	response, err := AddTemplate(client)
	if err != nil {
		panic(err)
	}
	fmt.Println("RequestId is:", response.RequestId)
}