Si les modèles prédéfinis d'ApsaraVideo Media Processing ne répondent pas à vos besoins, créez des modèles de transcodage personnalisés en définissant des paramètres tels que le format d'encodage, la fréquence d'images et la résolution. Les exemples suivants, basés sur le SDK Java V2.0, illustrent comment ajouter, mettre à jour, supprimer et interroger des modèles de transcodage.
Prérequis
Initialisez le client avant de commencer. Pour plus d'informations, consultez la rubrique Initialisation.
Ajouter un modèle
Appelez l'opération AddTemplate pour ajouter un modèle.
Si le message d'erreur
The resource "Template" quota has been used ups'affiche lors de l'ajout d'un modèle, votre quota de modèles de transcodage est atteint. Soumettez un ticket pour demander une augmentation de quota.Notez l'ID du modèle de transcodage renvoyé après l'ajout. Vous pouvez également créer et gérer des modèles de transcodage directement depuis la console. Pour en savoir plus, consultez la rubrique Modèles de transcodage.
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);
}
}
}
Mettre à jour un modèle
Appelez l'opération UpdateTemplate pour mettre à jour un modèle de transcodage.
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);
}
}
}
Supprimer un modèle
Appelez l'opération DeleteTemplate pour supprimer un modèle de transcodage.
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);
}
}
}
Interroger des modèles par ID
Appelez l'opération QueryTemplateList pour interroger des modèles par 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);
}
}
}
Interroger des modèles par état
Appelez l'opération SearchTemplate pour interroger des modèles par état.
// 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);
}
}
}