When preset templates in ApsaraVideo Media Processing do not meet your needs, you can create custom transcoding templates with parameters such as encoding format, frame rate, and resolution. The following Java SDK V2.0 examples show how to add, update, delete, and query transcoding templates.
Prerequisites
Initialize the client before you start. For more information, see Initialization.
Add a template
Call the AddTemplate operation to add a template.
-
If you receive the
The resource "Template" quota has been used uperror when you add a template, your transcoding template quota is exhausted. You can submit a ticket to request a quota increase. -
Record the returned transcoding template ID after you add the template. You can also create and manage transcoding templates in the console. For more information, see Transcoding templates.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>Use an AccessKey pair to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.AddTemplateRequest addTemplateRequest = new com.aliyun.mts20140618.models.AddTemplateRequest()
// The name of the template.
.setName("mps-example")
// The container.
.setContainer("{\"Format\":\"mp4\"}")
// The video stream configuration.
.setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10s\"}")
// The audio stream configuration.
.setAudio("{\"Codec\":\"H.264\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}")
// The general transcoding configurations.
.setTransConfig("{\"TransMode\":\"onepass\"}")
// The segment configuration field.
.setMuxConfig("{\"Segment\":{\"Duration\":\"10\"}}");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// If you copy the code to run, print the API return value.
client.addTemplateWithOptions(addTemplateRequest, runtime);
} catch (TeaException error) {
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Update a template
Call the UpdateTemplate operation to update a transcoding template.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>Use an AccessKey pair to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.UpdateTemplateRequest updateTemplateRequest = new com.aliyun.mts20140618.models.UpdateTemplateRequest()
// The template ID.
.setTemplateId("16f01ad6175e4230ac42bb5182cd****")
// The name of the template.
.setName("MPS-example")
// The container.
.setContainer("{\"Format\":\"mp4\"}")
// The video stream configuration.
.setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10\"}")
// The audio stream configuration.
.setAudio("{\"Codec\":\"aac\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}")
// The muxing configuration.
.setMuxConfig("{\"Segment\":{\"Duration\":\"10\"}}")
// The general transcoding configurations.
.setTransConfig("{\"TransMode\":\"onepass\"}");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// If you copy the code to run, print the API return value.
client.updateTemplateWithOptions(updateTemplateRequest, runtime);
} catch (TeaException error) {
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Delete a template
Call the DeleteTemplate operation to delete a transcoding template.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>Use an AccessKey pair to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.DeleteTemplateRequest deleteTemplateRequest = new com.aliyun.mts20140618.models.DeleteTemplateRequest()
// The ID of the custom transcoding template to delete.
.setTemplateId("16f01ad6175e4230ac42bb5182cd****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// If you copy the code to run, print the API return value.
client.deleteTemplateWithOptions(deleteTemplateRequest, runtime);
} catch (TeaException error) {
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Query templates by ID
Call the QueryTemplateList operation to query templates by ID.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>Use an AccessKey pair to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.QueryTemplateListRequest queryTemplateListRequest = new com.aliyun.mts20140618.models.QueryTemplateListRequest()
// The list of template IDs to query.
.setTemplateIds("16f01ad6175e4230ac42bb5182cd****,88c6ca184c0e424d5w5b665e2a12****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// If you copy the code to run, print the API return value.
client.queryTemplateListWithOptions(queryTemplateListRequest, runtime);
} catch (TeaException error) {
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Query templates by status
Call the SearchTemplate operation to query templates by status.
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>Use an AccessKey pair to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.SearchTemplateRequest searchTemplateRequest = new com.aliyun.mts20140618.models.SearchTemplateRequest()
// The status of the transcoding templates to search for.
.setState("Normal")
// The prefix of the template name to search by.
.setNamePrefix("S00000001")
// The number of entries per page for paged query results.
.setPageSize(10L)
// The current page number.
.setPageNumber(1L);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// If you copy the code to run, print the API return value.
client.searchTemplateWithOptions(searchTemplateRequest, runtime);
} catch (TeaException error) {
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}