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 PHP to submit and query snapshot jobs.

Prerequisites

MPS SDK for PHP is installed and configured. For more information, see MPS SDK for PHP. To obtain more information about the SDK and view examples on how to use the SDK to call API operations, visit OpenAPI Explorer.

Submit a snapshot job

You can call the SubmitSnapshotJob operation to submit a snapshot job. For more information about the request and response parameters, see SubmitSnapshotJob. Sample code:

<?php
namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Mts\V20140618\Mts;
use AlibabaCloud\Darabonba\Env\Env;
use AlibabaCloud\Tea\Tea;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Console\Console;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Mts\V20140618\Models\SubmitSnapshotJobRequest;


class Sample {

    private $pipelineId = "<PipelineId>";
    private $templateId = "S00000001-100020"; # The ID of the transcoding template. Select a template based on your business requirements.
    private $ossLocation = "<OssLocation>";
    private $bucket = "<bucket name>";
    private $oss_input_object  = "input.mp4"; 
    private $oss_output_object  = "output_{Count}.jpg";

    /**
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @param string $regionId
     * @return Mts
     * We recommend that you set the protocol parameter to HTTPS in a production environment.
     */
    public static function createClient($accessKeyId, $accessKeySecret, $regionId){
        $config = new Config([]);
        $config->accessKeyId = $accessKeyId;
        $config->accessKeySecret = $accessKeySecret;
        $config->regionId = $regionId;
        $config->protocol = "HTTP";
        return new Mts($config);
    }

    /**
     * @return void
     */
    public static function main(){
        $sample = new Sample;
        $client = self::createClient(Env::getEnv("ALIBABA_CLOUD_ACCESS_KEY_ID"), Env::getEnv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), 'cn-shanghai');
        $request = new SubmitSnapshotJobRequest([
            "input" => json_encode(array(
                'Location' => $sample->ossLocation,
                'Bucket' => $sample->bucket,
                'Object' => urlencode($sample->oss_input_object))
            ),
            "snapshotConfig" => $sample->snapshotConfig(),
            "pipelineId" => $sample->pipelineId,
        ]);
        $response = $client->submitSnapshotJob($request);
        Console::log(Utils::toJSONString(Tea::merge($response->body)));
    }


    function snapshotConfig() {
        $outputfile = array(
            'Location' => $this->ossLocation,
            'Bucket' => $this->bucket,
            'Object' => urlencode($this->oss_output_object));
        $snapshotConfig['OutputFile'] = $outputfile;
        $snapshotConfig['Time'] = 2;
        $snapshotConfig['Num'] = 10;
        $snapshotConfig['Interval'] = 20;
        return json_encode($snapshotConfig);
    }

}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
Sample::main();

Query one or more snapshot jobs

You can call the QuerySnapshotJobList operation to query one or more snapshot jobs. For more information about the request and response parameters, see QuerySnapshotJobList. Sample code:

<?php
namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Mts\V20140618\Mts;
use AlibabaCloud\Darabonba\Env\Env;
use AlibabaCloud\Tea\Tea;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Console\Console;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Mts\V20140618\Models\QuerySnapshotJobListRequest;


class Sample {

    /**
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @param string $regionId
     * @return Mts
     * We recommend that you set the protocol parameter to HTTPS in a production environment.
     */
    public static function createClient($accessKeyId, $accessKeySecret, $regionId){
        $config = new Config([]);
        $config->accessKeyId = $accessKeyId;
        $config->accessKeySecret = $accessKeySecret;
        $config->regionId = $regionId;
        $config->protocol = "HTTP";
        return new Mts($config);
    }

    /**
     * @return void
     */
    public static function main(){
        $sample = new Sample;
        $client = self::createClient(Env::getEnv("ALIBABA_CLOUD_ACCESS_KEY_ID"), Env::getEnv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), 'cn-shanghai');
        $request = new QuerySnapshotJobListRequest([
            "snapshotJobIds" => "72dfa5e67974c736******"
        ]);
        $response = $client->querySnapshotJobList($request);
        Console::log(Utils::toJSONString(Tea::merge($response->body)));
    }

}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
Sample::main();

References