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. This topic provides sample code for using ApsaraVideo Media Processing (MPS) SDK for PHP to capture snapshots.

Sample code

<?php
require_once './aliyun-php-sdk-core/Config.php'; 
use Mts\Request\V20140618 as Mts;

function initMtsClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // The ID of the region in which your MPS service is deployed.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

try {
    $client = initMtsClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
    $response = submitSnapshotJob($client);
    print_r($response);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

function submitSnapshotJob($client) {
    $pipelineId = 'd7cedd984be7dd63395c****';   # The ID of the MPS queue. You can log on to the MPS console to view the ID.
    $templateId = "S00000001-100020"; # The ID of the transcoding template. You can log on to the MPS console to view the ID.
    $ossLocation = 'oss-cn-shanghai';
    $bucket = '<bucket name>';
    $ossInputObject = 'input.mp4';
    $ossOutputObject = 'output_{Count}.jpg';
    $request = new Mts\SubmitSnapshotJobRequest();
    # The job input.
    $input = array('Location' => $ossLocation,
               'Bucket' => $bucket,
               'Object' => urlencode($ossInputObject));

    # The location of the output snapshots.
    $output = array('Location' => $ossLocation,
                'Bucket' => $bucket,
                'Object' => urlencode($ossOutputObject));
    # The configurations of the snapshot job.
    $snapshotConfig = array('OutputFile' => $output);
    $snapshotConfig['Time'] = 2;
    $snapshotConfig['Interval'] = 2;
    $snapshotConfig['Num'] = 3;
    $snapshotConfig['Height'] = 360;
    # The request parameters.
    $request->setInput(json_encode($input));
    $request->setSnapshotConfig(json_encode($snapshotConfig));
    $request->setPipelineId($pipelineId);
    return $client->getAcsResponse($request);
}

References