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 Python to capture snapshots.

Sample code

import os
import json

from urllib.parse import quote
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.SubmitSnapshotJobRequest import SubmitSnapshotJobRequest

# Obtain an AccessKey ID and an AccessKey secret from environment variables.
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# The region_id parameter specifies the ID of the region in which your MPS service is deployed. For information about supported regions, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/service-regions.
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

mps_region_id = 'cn-shanghai'
pipeline_id = '9bad3801a2c561c22c4df9c6****'
oss_location = 'oss-cn-shanghai'
oss_bucket = '<your bucket name>'
oss_input_object = 'input.mp4'
oss_output_object = 'output_{Count}.jpg'

request = SubmitSnapshotJobRequest()
request.set_accept_format('json')
# The job input. The input object must be URL-encoded.
job_input = {'Location': oss_location,
             'Bucket': oss_bucket,
             'Object': quote(oss_input_object) }
request.set_Input(json.dumps(job_input))
# The OutputFile parameter in snapshot_config specifies the location of the output snapshots.
job_output = {'Location': oss_location,
              'Bucket': oss_bucket,
              'Object': quote(oss_output_object) }
snapshot_config = {'OutputFile': job_output}
snapshot_config['Time'] = 2
snapshot_config['Interval'] = 2
snapshot_config['Num'] = 3
snapshot_config['Height'] = 360
request.set_SnapshotConfig(json.dumps(snapshot_config))
request.set_PipelineId(pipeline_id)


response = client.do_action_with_exception(request)
# Display the response.
print(str(response, encoding='utf-8'))

References