If the preset transcoding templates of ApsaraVideo Media Processing (MPS) cannot meet your business requirements, you can create a custom transcoding template and configure the settings of the template, such as the encoding format, frame rate, and resolution. This topic provides examples on how to call API operations to create, modify, delete, and query a transcoding template. The API operations are encapsulated in MPS SDK for Java.
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 the error message
The resource "Template" quota has been used up
is returned, your transcoding template quota is used up. You can submit a ticket to apply for the template quota.We recommend that you record the returned ID of the transcoding template that you create. You can also create a transcoding template and obtain its ID in the MPS console. For more information, see Transcoding templates.
/**
* Create a transcoding template.
* @param client
* @return
* @throws Exception
*/
public static AddTemplateResponse addTemplate(DefaultAcsClient client) throws Exception {
AddTemplateRequest request = new AddTemplateRequest();
request.setName("test-template");
request.setContainer("{\"Format\":\"mp4\"}");
request.setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Width\":\"1280\",\"Height\":\"720\",\"Fps\":\"25\",\"Gop\":\"300\"}");
request.setAudio("{\"Codec\":\"AAC\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}");
request.setTransConfig("{\"TransMode\":\"onepass\"}");
return client.getAcsResponse(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
* @throws Exception
*/
public static UpdateTemplateResponse updateTemplate(DefaultAcsClient client) throws Exception {
UpdateTemplateRequest request = new UpdateTemplateRequest();
// The ID of the transcoding template. To view the ID, log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane. You can also create a transcoding template and obtain its ID by calling the AddTemplate operation.
request.setTemplateId("81766fad9d7beb87d36ae****");
request.setName("update name");
request.setContainer("{\"Format\":\"m3u8\"}");
request.setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Width\":\"1280\",\"Height\":\"720\",\"Fps\":\"25\",\"Gop\":\"300\"}");
request.setAudio("{\"Codec\":\"AAC\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}");
request.setTransConfig("{\"TransMode\":\"onepass\",\"IsCheckReso\":\"true\"}");
request.setMuxConfig("{\"Segment\":{\"Duration\":\"10\"}}");
return client.getAcsResponse(request);
}
Delete a transcoding template
You can call the DeleteTemplate operation to delete a transcoding template.
/**
* Delete a transcoding template.
* @param client
* @return
* @throws Exception
*/
public static DeleteTemplateResponse deleteTemplate(DefaultAcsClient client) throws Exception {
DeleteTemplateRequest request = new DeleteTemplateRequest();
* The ID of the transcoding template. To view the ID, log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane. You can also create a transcoding template and obtain its ID by calling the AddTemplate operation.
request.setTemplateId("81769d796feb87d36ae****");
return client.getAcsResponse(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
* @throws Exception
*/
public static QueryTemplateListResponse queryTemplateList(DefaultAcsClient client) throws Exception {
QueryTemplateListRequest request = new QueryTemplateListRequest();
// The ID of the transcoding template. To view the ID, log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane. You can also create a transcoding template and obtain its ID by calling the AddTemplate operation.
// You can query up to 10 transcoding templates at a time. Separate multiple transcoding template IDs with commas (,).
request.setTemplateIds("8176796feb8eb87d36ae****");
return client.getAcsResponse(request);
}
Query transcoding templates in specific states
You can call the SearchTemplate operation to query transcoding templates in specific states.
/**
* Query transcoding templates in specific states.
* @param client
* @return
* @throws Exception
*/
public static SearchTemplateResponse searchTemplate(DefaultAcsClient client) throws Exception {
SearchTemplateRequest request = new SearchTemplateRequest();
// The status of the transcoding template.
// Valid values: All, Normal, and Deleted. A value of All specifies all templates. A value of Normal specifies regular templates. A value of Deleted specifies deleted templates.
request.setState("Normal");
return client.getAcsResponse(request);
}
Sample code
import com.alibaba.fastjson.JSON;
import com.aliyun.mps.utils.InitClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.*;
/**
* ***** Usage notes ******
* This sample code provides examples on the basic operations on a transcoding template, including creating, modifying, deleting, and querying a transcoding template.
*
* ***** References ******
* addTemplate Creates a transcoding template. For more information, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-addtemplate.
* updateTemplate Modifies the configurations of a transcoding template. For more information, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-updatetemplate.
* deleteTemplate Deletes a template. For more information, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-deletetemplate.
* queryTemplateList Queries transcoding templates based on template IDs. For more information, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-querytemplatelist.
* searchTemplate Queries transcoding templates in specific states. For more information, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-searchtemplate.
*
* For more information about template parameters, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a.
*
* ***** main method ******
* This sample code provides examples only for the AddTemplate and UpdateTemplate operations in the main method. If you want to call other operations, replace the response.
*/
public class Template{
public static void main(String[] args) throws ClientException {
// Initialize a client.
DefaultAcsClient client = InitClient.initMpsClient();
AddTemplateResponse response;
try {
response = addTemplate(client);
System.out.println("RequestId is:" + response.getRequestId());
System.out.println("TemplateI is:" + response.getTemplate().getId());
} catch (Exception e) {
e.printStackTrace();
}
// UpdateTemplateResponse response;
// try {
// response = updateTemplate(client);
// System.out.println("RequestId is:" + response.getRequestId());
// System.out.println("TemplateId is:" + response.getTemplate().getId());
// System.out.println("Template is:" + JSON.toJSON(response.getTemplate()));
// } catch (Exception e) {
// e.printStackTrace();
// }
}
/**
* Create a transcoding template.
* @param client
* @return
* @throws Exception
*/
public static AddTemplateResponse addTemplate(DefaultAcsClient client) throws Exception {
AddTemplateRequest request = new AddTemplateRequest();
request.setName("test-template");
request.setContainer("{\"Format\":\"mp4\"}");
request.setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Width\":\"1280\",\"Height\":\"720\",\"Fps\":\"25\",\"Gop\":\"300\"}");
request.setAudio("{\"Codec\":\"AAC\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}");
request.setTransConfig("{\"TransMode\":\"onepass\"}");
return client.getAcsResponse(request);
}
/**
* Modify the configurations of a transcoding template.
* @param client
* @return
* @throws Exception
*/
public static UpdateTemplateResponse updateTemplate(DefaultAcsClient client) throws Exception {
UpdateTemplateRequest request = new UpdateTemplateRequest();
// The ID of the transcoding template. To view the ID, log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane. You can also create a transcoding template and obtain its ID by calling the AddTemplate operation.
request.setTemplateId("8176eb87d36ae96fad9d7****");
request.setName("update name");
request.setContainer("{\"Format\":\"m3u8\"}");
request.setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Width\":\"1280\",\"Height\":\"720\",\"Fps\":\"25\",\"Gop\":\"300\"}");
request.setAudio("{\"Codec\":\"AAC\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}");
request.setTransConfig("{\"TransMode\":\"onepass\",\"IsCheckReso\":\"true\"}");
request.setMuxConfig("{\"Segment\":{\"Duration\":\"10\"}}");
return client.getAcsResponse(request);
}
/**
* Delete a transcoding template.
* @param client
* @return
* @throws Exception
*/
public static DeleteTemplateResponse deleteTemplate(DefaultAcsClient client) throws Exception {
DeleteTemplateRequest request = new DeleteTemplateRequest();
// The ID of the transcoding template. To view the ID, log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane. You can also create a transcoding template and obtain its ID by calling the AddTemplate operation.
request.setTemplateId("8176f9d796eb87d36ae****");
return client.getAcsResponse(request);
}
/**
* Query transcoding templates based on template IDs.
* @param client
* @return
* @throws Exception
*/
public static QueryTemplateListResponse queryTemplateList(DefaultAcsClient client) throws Exception {
QueryTemplateListRequest request = new QueryTemplateListRequest();
// The ID of the transcoding template. To view the ID, log on to the MPS console and choose Template Management > Transcoding Templates in the left-side navigation pane. You can also create a transcoding template and obtain its ID by calling the AddTemplate operation.
// You can query up to 10 transcoding templates at a time. Separate the transcoding template IDs with commas (,).
request.setTemplateIds("8176f9d796eb87d36ae****");
return client.getAcsResponse(request);
}
/**
* Query transcoding templates in specific states.
* @param client
* @return
* @throws Exception
*/
public static SearchTemplateResponse searchTemplate(DefaultAcsClient client) throws Exception {
SearchTemplateRequest request = new SearchTemplateRequest();
// The state of the transcoding template.
// Valid values: All, Normal, and Deleted. A value of All specifies all templates. A value of Normal specifies regular templates. A value of Deleted specifies deleted templates.
request.setState("Normal");
return client.getAcsResponse(request);
}
}