All Products
Search
Document Center

ApsaraVideo Media Processing:Capture snapshots

Last Updated:Mar 14, 2024

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 specify the points in time when snapshots are captured, the interval between two consecutive snapshots, the number of snapshots to be captured, the types of snapshots to be captured, and whether to compose multiple snapshots into one image sprite. 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 to submit and query snapshot jobs.

Prerequisites

A 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.

Note
  • 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.

  • Make sure that the name of your file is 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.

/**
 * Submit a snapshot job.
 * @param client
 * @return
 * @throws Exception
 */
public static SubmitSnapshotJobResponse submitSnapshotJob(IAcsClient client) throws Exception{

    // Create an API request and specify the required parameters.
    SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();

    JSONObject input = new JSONObject();
    input.put("Bucket","<your bucket name>");
    input.put("Location","oss-cn-beijing");
    input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
    request.setInput(input.toJSONString());

    // The output path of the snapshots.
    JSONObject outputFile = new JSONObject();
    outputFile.put("Bucket","<your bucket name>");
    outputFile.put("Location","oss-cn-beijing");
    outputFile.put("Object",URLEncoder.encode("mps-test/demo/test-{Count}.jpg", "utf-8"));

    // In this example, snapshots start to be captured at the 100th second of the video. Six regular frames are captured at even intervals, the snapshots are not composed into one image sprite, and the output files are not in the VTT format.
    JSONObject snapshotConfig = new JSONObject();
    snapshotConfig.put("OutputFile",outputFile.toJSONString());
    // The point in time when snapshots start to be captured.
    snapshotConfig.put("Time","100");
    // The interval at which snapshots are captured.
    snapshotConfig.put("Interval","0");
    // The number of snapshots.
    snapshotConfig.put("Num","6");
    // The snapshot type. Valid values: normal and intra. Default value: normal. A value of normal indicates normal frames. A value of intra indicates keyframes.
    snapshotConfig.put("FrameType","normal");

//        JSONObject subOut = new JSONObject();
//        // Specifies whether to compose multiple snapshots into one image sprite. Valid values: false and true.
//        subOut.put("IsSptFrag","true");
//        snapshotConfig.put("Format","vtt");
//        snapshotConfig.put("SubOut",subOut);

    request.setSnapshotConfig(snapshotConfig.toJSONString());
    request.setPipelineId(pipelineId);
    //request.setUserData("");

    return client.getAcsResponse(request);
}

Query the results of snapshot jobs

You can call the QuerySnapshotJobList operation to query the results of snapshot jobs.

/**
 * Query the results of snapshot jobs.
 * @param client
 * @return
 * @throws Exception
 */
public static QuerySnapshotJobListResponse querySnapshotJobList(IAcsClient client) throws Exception{

    // Create an API request and specify the required parameters.
    QuerySnapshotJobListRequest request = new QuerySnapshotJobListRequest();

    // You can query the results of up to 10 snapshot jobs at a time. Separate multiple snapshot job IDs with commas (,). You can obtain the job ID when you call the SubmitSnapshotJob operation to submit a job.
    request.setSnapshotJobIds("47859822caba8aadddd4f6e7****");

    return client.getAcsResponse(request);
}

Sample code

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.*;

import java.net.URLEncoder;

/**
 * ***** Usage notes ******
 * In this example, the snapshots are captured at even intervals based on the video duration.
 *
 * ***** Methods ******
 * For more information about the submitSnapshotJob operation, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-submitsnapshotjob.
 * For more information about the querySnapshotJobList operation, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-querysnapshotjoblist.
 *
 * For more information about snapshot-related parameters, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a.
public class SnapshotJob {

    // The ID of the MPS queue. To view the MPS queue ID, log on to the MPS console and choose Global Settings > MPS Queue and Callback in the left-side navigation pane.
    private static String pipelineId = "bee7a0a0cbf34a526****";

    public static void main(String[] args) throws ClientException {

        // Initialize a client.
        DefaultAcsClient client = InitClient.initMpsClient();

        SubmitSnapshotJobResponse response;
        try {
            response = submitSnapshotJob(client);
            System.out.println("RequestId is:" + response.getRequestId());
            System.out.println("JobId is:" + response.getSnapshotJob().getId());
        } catch (Exception e) {
            e.printStackTrace();
        }

//        QuerySnapshotJobListResponse response;
//        try {
//            response = querySnapshotJobList(client);
//            System.out.println("RequestId is:" + response.getRequestId());
//            System.out.println("SnapshotJobList is:" + JSON.toJSON(response.getSnapshotJobList()));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    /**
     * Submit a snapshot job.
     * @param client
     * @return
     * @throws Exception
     */
    public static SubmitSnapshotJobResponse submitSnapshotJob(IAcsClient client) throws Exception{

        // Create an API request and specify the required parameters.
        SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();

        JSONObject input = new JSONObject();
        input.put("Bucket","<your bucket name>");
        input.put("Location","oss-cn-beijing");
        input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
        request.setInput(input.toJSONString());

        // The output path of the snapshots.
        JSONObject outputFile = new JSONObject();
        outputFile.put("Bucket","<your bucket name>");
        outputFile.put("Location","oss-cn-beijing");
        outputFile.put("Object",URLEncoder.encode("mps-test/demo/test-{Count}.jpg", "utf-8"));


        // In this example, snapshots start to be captured at the 100th second of the video. Six regular frames are captured at even intervals, the snapshots are not composed into one image sprite, and the output files are not in the VTT format.
        JSONObject snapshotConfig = new JSONObject();
        snapshotConfig.put("OutputFile",outputFile.toJSONString());
        // The point in time when snapshots start to be captured.
        snapshotConfig.put("Time","100");
        // The interval at which snapshots are captured.
        snapshotConfig.put("Interval","0");
        // The number of snapshots.
        snapshotConfig.put("Num","6");
        // The snapshot type. Valid values: normal and intra. Default value: normal. A value of normal indicates normal frames. A value of intra indicates keyframes.
        snapshotConfig.put("FrameType","normal");

//        JSONObject subOut = new JSONObject();
//        // Specifies whether to compose multiple snapshots into one image sprite. Valid values: false and true.
//        subOut.put("IsSptFrag","true");
//        snapshotConfig.put("Format","vtt");
//        snapshotConfig.put("SubOut",subOut);

        request.setSnapshotConfig(snapshotConfig.toJSONString());
        request.setPipelineId(pipelineId);
        //request.setUserData("");

        return client.getAcsResponse(request);
    }

    /**
     * Query the results of snapshot jobs.
     * @param client
     * @return
     * @throws Exception
     */
    public static QuerySnapshotJobListResponse querySnapshotJobList(IAcsClient client) throws Exception{

        // Create an API request and specify the required parameters.
        QuerySnapshotJobListRequest request = new QuerySnapshotJobListRequest();

        // You can query the results of up to 10 snapshot jobs at a time. Separate multiple snapshot job IDs with commas (,). You can obtain the job ID when you call the SubmitSnapshotJob operation to submit a job.
        request.setSnapshotJobIds("47859822caba8aa421ddd****");

        return client.getAcsResponse(request);
    }
}

            

References