You can use the video snapshot feature to capture snapshots of a specific size at specific points in time of a video. The snapshots are used in scenarios such as video thumbnails, sprites, and progress bar thumbnails. You can use snapshot jobs to specify the point in time and interval at which snapshots to be captured, number of snapshots, snapshot type, and whether to compose snapshots into an image. You can submit snapshot jobs in the ApsaraVideo Media Processing (MPS) console or by using the API or SDKs. This topic provides examples on how to use MPS SDK for Java V2.0 to submit and query snapshot jobs.
Prerequisites
An SDK client is initialized. For more information, see Initialize a client.
Submit a snapshot job
You can call the SubmitSnapshotJob operation to submit a snapshot job.
When you submit a snapshot job by using the SDK, you must perform URL encoding on the file path in which your file resides. Otherwise, the snapshot job fails to be submitted. For more information, see URL encoding.
The name of the file must be valid. Otherwise, the file cannot be found and the snapshot job fails to be submitted. For more information, see Parameter details.
We recommend that you record the ID of the snapshot job. This facilitates subsequent query operations.
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);
}
}
}
Query the results of snapshot jobs
You can call the QuerySnapshotJobList operation to query the results of snapshot jobs.
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);
}
}
}