O recurso de snapshot de vídeo captura imagens de tamanho específico em momentos determinados. Esses snapshots servem para gerar miniaturas de vídeo, sprites e miniaturas da barra de progresso. Nos jobs de snapshot, defina o momento e o intervalo das capturas, a quantidade de imagens, o tipo de snapshot e se deseja compor os snapshots em uma única imagem. Envie jobs de snapshot pelo console do ApsaraVideo Media Processing (MPS), pela API ou por SDKs. Este tópico apresenta exemplos de uso do MPS SDK for Java V2.0 para enviar e consultar jobs de snapshot.
Pré-requisitos
Cliente SDK inicializado. Para mais informações, consulte Inicializar um cliente.
Envie um job de snapshot
Chame a operação SubmitSnapshotJob para enviar um job de snapshot.
Ao enviar um job de snapshot com o SDK, aplique codificação URL no caminho do arquivo. Caso contrário, o envio falhará. Para mais informações, consulte Codificação URL.
O nome do arquivo deve ser válido. Nomes inválidos impedem a localização do arquivo e causam falha no envio. Para mais detalhes, consulte Detalhes dos parâmetros.
Anote o ID do job de snapshot para facilitar consultas futuras.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* <b>description</b> :
* <p>Use your AccessKey ID and AccessKey secret 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 configured.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
.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.SubmitSnapshotJobRequest submitSnapshotJobRequest = new com.aliyun.mts20140618.models.SubmitSnapshotJobRequest()
// The job input.
.setInput("{\"Bucket\":\"example-bucket\",\"Location\":\"example-location\",\"Object\":\"example%2Ftest.flv\"}")
// The configuration of the snapshot job.
.setSnapshotConfig("{\"OutputFile\":{\"Bucket\":\"example-001\",\"Location\":\"example-location\",\"Object\":\"{Count}.jpg\"},\"Time\":\"5\",\"Num\":\"10\",\"Interval\":\"20\"}")
// The user-defined data.
.setUserData("testid-001")
// The ID of the MPS queue.
.setPipelineId("dd3dae411e704030b921e52698e5****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Write your own code to display the response of the API operation if necessary.
client.submitSnapshotJobWithOptions(submitSnapshotJobRequest, runtime);
} catch (TeaException 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.
System.out.println(error.getMessage());
// The URL of the corresponding error diagnostics page.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _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.
System.out.println(error.getMessage());
// The URL of the corresponding error diagnostics page.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Consultar resultados de jobs de snapshot
Chame a operação QuerySnapshotJobList para obter os resultados dos jobs de snapshot.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* <b>description</b> :
* <p>Use your AccessKey ID and AccessKey secret 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 configured.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
.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.QuerySnapshotJobListRequest querySnapshotJobListRequest = new com.aliyun.mts20140618.models.QuerySnapshotJobListRequest()
// The IDs of snapshot jobs.
.setSnapshotJobIds("72dfa5e679ab4be9a3ed9974c736****")
// The state of snapshot jobs to be queried.
.setState("Snapshoting")
// The beginning of the time range within which the snapshot jobs to be queried were created.
.setStartOfJobCreatedTimeRange("2014-01-10T12:00:00Z")
// The end of the time range within which the snapshot jobs to be queried were created.
.setEndOfJobCreatedTimeRange("2014-01-12T12:00:00Z")
// The ID of the MPS queue.
.setPipelineId("b11c171cced04565b1f38f1ecc39****")
// The pagination token that is used in the next request to retrieve a new page of results.
.setNextPageToken("cc6cbef8e8d5481ca536f5d2a466****")
// The maximum number of entries to return on each page.
.setMaximumPageSize(30L);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Write your own code to display the response of the API operation if necessary.
client.querySnapshotJobListWithOptions(querySnapshotJobListRequest, runtime);
} catch (TeaException 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.
System.out.println(error.getMessage());
// The URL of the corresponding error diagnostics page.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _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.
System.out.println(error.getMessage());
// The URL of the corresponding error diagnostics page.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}