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 sample code for using MPS SDK for Node.js to capture snapshots.

Sample code

import Console from '@alicloud/tea-console';
import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import Env from '@alicloud/darabonba-env';
import Util from '@alicloud/tea-util';
import mts20140618, * as $mts20140618 from '@alicloud/mts20140618';
import * as $tea from '@alicloud/tea-typescript';

/**
 * Install Node.js 8.x or later. 
 * Install Alibaba Cloud SDK for Node.js. npm install @alicloud/pop-core --save
 * Install the Alibaba Cloud SDK Credentials package.   npm install @alicloud/credentials
 * Install MPS SDK for Node.js. npm install --save @alicloud/mts20140618
 * 
 */
/** The ID of the MPS queue. You can log on to the MPS console to view the ID. */
var pipelineId = "d7cedd984be7dd63395c*****"; 
var ossLocation = "oss-cn-shanghai";
var bucket = "<bucket name>";
var ossInputObject = "input.mp4";
var ossOutputObject = "output_{Count}.jpg";



export default class Client {

/** Initialize a client. */
static async createClient(accessKeyId: string, accessKeySecret: string, regionId: string): Promise<mts20140618> {
    let config = new $OpenApi.Config({ });
    config.accessKeyId = accessKeyId;
    config.accessKeySecret = accessKeySecret;
    /** The ID of the region in which MPS is deployed. */
    config.regionId = "cn-shanghai";
    return new mts20140618(config);
}

static async main(args: string[]): Promise<void> {
    let client = await Client.createClient(Env.getEnv("ALIBABA_CLOUD_ACCESS_KEY_ID"), Env.getEnv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), args[0]);

    let request = new $mts20140618.SubmitSnapshotJobRequest({
        input: inputParam(),
        outputBucket: bucket,
        pipelineId: pipelineId,
        outputLocation: ossLocation,
        snapshotConfig: snapshotConfig()
    });
    let response = await client.submitSnapshotJob(request);
    Console.log(Util.toJSONString($tea.toMap(response)));
}
}

Client.main(process.argv.slice(2));


function inputParam() {
    var input:any = {};
    input.Location = ossLocation;
    input.Bucket = bucket;
    input.Object = encodeURIComponent(ossInputObject);
    return JSON.stringify(input);
}
/** The snapshot configurations. */
function snapshotConfig(){
    var snapshotConfig:any = {};
    var output:any = {};
    output.Location = ossLocation;
    output.Bucket = bucket;
    output.Object = encodeURIComponent(ossOutputObject);
    snapshotConfig.OutputFile = JSON.stringify(output);
    snapshotConfig.Time = "2";
    snapshotConfig.Interval = "2";
    snapshotConfig.Num = "3";
    snapshotConfig.Height = "360"; 
  	//return JSON.stringify(snapshotConfig);
}

References