This topic describes the parameters and examples to capture video snapshots.

Precautions

  • OSS currently can only capture images from video objects in the H.264 format.
  • OSS does not automatically store captured images. You must manually download the captured images to the local device.

Parameters

Operation type: video

Operation name: snapshot

Parameter Description Valid value
t The time when the image needs to capture. [0, video duration]

Unit: ms

w The width of the image to capture. If this parameter is set to 0, the width of the image is automatically calculated. [0, video width]

Unit: pixel (px)

h The height of the image to capture. If this parameter is set to 0, the height of the image is automatically calculated. If w and h are both set to 0, the width and height of the captured image are the same as those of the video file. [0, video height]

Unit: px

m The mode to capture the image. If this parameter is not specified, the image is captured in default mode. In other words, the image at the specified point in time in the video is captured. If this parameter is set to fast, the most recent key image before the specified time is captured. fast
f The format of the image to capture. jpg and png
ar Specifies whether to automatically rotate the image based on the video information. If this parameter is set to auto, the system automatically rotates the image based on the video information. auto

Examples

  • Use the fast mode to capture the image at the seventh second of the video. Export it as a JPG image with the width of 800 px and height of 600 px.

    The URL used to process the image is in the following format: <Source video URL>? x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast

  • Capture the image at the fiftieth second of the video accurately. Export it as a JPG image with the width of 800 px and height of 600 px.

    The URL used to process the image is in the following format: <Source video URL>? x-oss-process=video/snapshot,t_50000,f_jpg,w_800,h_600

Generate a signed URL for a captured snapshot

You can use OSS SDKs to generate a signed URL for a captured snapshot. The code used to generate signed URLs for video snapshots is similar to that used to generated signed URLs for images. You only need to change the operation performed on images in the code to snapshot.

The following code uses OSS SDK for Java to provides an example on how to generate a signed URL for a captured snapshot:
// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";
// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// Configure video snapshots.
String style = "video/snapshot,t_50000,f_jpg,w_800,h_600";
// Set the expiration time to 10 minutes.
Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10 );
GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
req.setExpiration(expiration);
req.setProcess(style);
URL signedUrl = ossClient.generatePresignedUrl(req);
System.out.println(signedUrl);
// Shut down the OSSClient instance.
ossClient.shutdown();

For more information about how to use OSS SDKs to generate a signed URL to process images, see: