All Products
Search
Document Center

ApsaraVideo Media Processing:Capture snapshots

Last Updated:Mar 12, 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 submit snapshot jobs in the ApsaraVideo Media Processing (MPS) console or by using the API or SDKs. This topic provides examples on how to use MPS SDK for Go to submit and query snapshot jobs by calling API operations. You can specify the points in time when snapshots are captured, the interval between two consecutive snapshots, the number of snapshots to be captured, types of snapshots to be captured, and whether to compose multiple snapshots into one image sprite.

Prerequisites

The SDK client is initialized. For more information, see Initialize a client.

Submit a snapshot job

You can call the SubmitSnapshotJob operation to submit a snapshot job.

Note
  • When you submit a snapshot job by using the SDK, you must perform URL encoding on the file path in which your file resides. Otherwise, the snapshot job fails to be submitted. For more information, see URL encoding.

  • Make sure that the name of your file is valid. Otherwise, the file cannot be found and the snapshot job fails to be submitted. For more information, see Parameter details.

  • We recommend that you record the ID of the snapshot job. This facilitates subsequent query operations.

/**
 * Submit a snapshot job
 * @param client
 * @return
 */
 func SubmitSnapshotJob(client *mts.Client) (*mts.SubmitSnapshotJobResponse, error) {
	request := mts.CreateSubmitSnapshotJobRequest()
	// Construct an input object. Make sure that the value of the Location parameter is the region in which the client is deployed.
	input := map[string]string{
		"Location": "oss-cn-beijing",
		"Bucket":   "<your bucket name>",
		"Object":   url.QueryEscape("mps-test/demo/test.mp4"),
	}
	inputJson, _ := json.Marshal(input)
	request.Input = string(inputJson)

	// The output path of the snapshots.
	outputFile := map[string]string{
		"Location": "oss-cn-beijing",
		"Bucket":   "<your bucket name>",
		"Object":   url.QueryEscape("mps-test/demo/test-{Count}.jpg"),
	}
	outputFileJson, _ := json.Marshal(outputFile)
	
	snapshotConfig := map[string]string{
		"OutputFile": string(outputFileJson),
		"Time": "100",
		"Interval": "0",
		"Num": "6",
		"FrameType": "normal", // The type of the snapshot. Default value: normal. A value of normal indicates normal frames. A value of intra indicates keyframes.
	}
	snapshotConfigJson, _ := json.Marshal(snapshotConfig)
	request.SnapshotConfig = string(snapshotConfigJson)

	// PipelineId
	request.PipelineId = pipelineId

	return client.SubmitSnapshotJob(request)
}

Query the results of snapshot jobs

You can call the QuerySnapshotJobList operation to query the results of snapshot jobs.

/**
 * Query the results of snapshot jobs
 * @param client
 * @return
 */
 func QuerySnapshotJobList(client *mts.Client) (*mts.QuerySnapshotJobListResponse, error) {
	request := mts.CreateQuerySnapshotJobListRequest()
	// You can query the results of up to 10 snapshot jobs at a time. Separate multiple snapshot job IDs with commas (,). You can obtain the job ID when you call the SubmitSnapshotJob operation to submit a job.
	request.SnapshotJobIds = "7ffbc8f9af8ba6fad2b****";

	return client.QuerySnapshotJobList(request)
}

Sample code

package main

import (
	"encoding/json"
	"fmt"
	"net/url"

	mts "github.com/aliyun/alibaba-cloud-sdk-go/services/mts"
)

/**
 * Submit a snapshot job
 * @param client
 * @return
 */
 func SubmitSnapshotJob(client *mts.Client) (*mts.SubmitSnapshotJobResponse, error) {
	request := mts.CreateSubmitSnapshotJobRequest()
	// Construct an input object. Make sure that the value of the Location parameter is the region in which the client is deployed.
	input := map[string]string{
		"Location": "oss-cn-beijing",
		"Bucket":   "<your bucket name>",
		"Object":   url.QueryEscape("mps-test/demo/test.mp4"),
	}
	inputJson, _ := json.Marshal(input)
	request.Input = string(inputJson)

	// The output path of the snapshots.
	outputFile := map[string]string{
		"Location": "oss-cn-beijing",
		"Bucket":   "<your bucket name>",
		"Object":   url.QueryEscape("mps-test/demo/test-{Count}.jpg"),
	}
	outputFileJson, _ := json.Marshal(outputFile)
	
	snapshotConfig := map[string]string{
		"OutputFile": string(outputFileJson),
		"Time": "100",
		"Interval": "0",
		"Num": "6",
		"FrameType": "normal", // The type of the snapshot. Default value: normal. A value of normal indicates normal frames. A value of intra indicates keyframes.
	}
	snapshotConfigJson, _ := json.Marshal(snapshotConfig)
	request.SnapshotConfig = string(snapshotConfigJson)

	// PipelineId
	request.PipelineId = pipelineId

	return client.SubmitSnapshotJob(request)
}

/**
 * Query the results of snapshot jobs
 * @param client
 * @return
 */
 func QuerySnapshotJobList(client *mts.Client) (*mts.QuerySnapshotJobListResponse, error) {
	request := mts.CreateQuerySnapshotJobListRequest()
	// You can query the results of up to 10 snapshot jobs at a time. Separate multiple snapshot job IDs with commas (,). You can obtain the job ID when you call the SubmitSnapshotJob operation to submit a job.
	request.SnapshotJobIds = "7ffbc6fad2b8f9af8ba****";

	return client.QuerySnapshotJobList(request)
}

const (
	// The ID of the MPS queue. To view the MPS queue ID, log on to the MPS console and choose Global Settings > MPS Queue and Callback in the left-side navigation pane.
	pipelineId = "bee7a5b96fad2bfe40a0cbf****"
)

func main() {
	// Initialize a client.
	client, err := InitMtsClient()
	if err != nil {
		panic(err)
	}
	response, err := SubmitSnapshotJob(client)
	if err != nil {
		panic(err)
	}
	fmt.Println("RequestId is:", response.RequestId)
	fmt.Println("JobId is:", response.SnapshotJob.Id)

}

References