Video snapshots capture frames of a specific size at specific points in time from a video. They are used for 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. A snapshot job supports specifying capture time points, intervals, snapshot count, snapshot types, and whether to compose multiple snapshots into one image sprite. This topic provides examples on how to call MPS SDK for Go V2.0 to submit and query snapshot jobs.
Prerequisites
An SDK client is initialized. For more information, see Initialize a client.
Submit a snapshot job
Call the SubmitSnapshotJob operation to submit a snapshot job.
-
When you submit a snapshot job by using the SDK, you must URL-encode the file path. Otherwise, the job fails. For more information, see URL encoding.
-
Make sure that your file name is valid. Otherwise, the file cannot be found and the job submission fails. For more information, see Parameter details.
-
Record the snapshot job ID for subsequent queries.
package main
import (
"encoding/json"
"strings"
"fmt"
"os"
mts20140618 "github.com/alibabacloud-go/mts-20140618/v6/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
// Description:
//
// Use your AccessKey ID and AccessKey secret to initialize a client.
//
// @return Client
//
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {
config := &openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
_result = &mts20140618.Client{}
_result, _err = mts20140618.NewClient(config)
return _result, _err
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
submitSnapshotJobRequest := &mts20140618.SubmitSnapshotJobRequest{
Input: tea.String("{\"Bucket\":\"example-bucket\",\"Location\":\"example-location\",\"Object\":\"example%2Ftest.flv\"}"),
SnapshotConfig: tea.String("{\"OutputFile\":{\"Bucket\":\"example-001\",\"Location\":\"example-location\",\"Object\":\"{Count}.jpg\"},\"Time\":\"5\",\"Num\":\"10\",\"Interval\":\"20\"}"),
}
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// Write your own code to display the response of the API operation if necessary.
_, _err = client.SubmitSnapshotJobWithOptions(submitSnapshotJobRequest, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
// The error message.
fmt.Println(tea.StringValue(error.Message))
// The URL of the corresponding error diagnostics page.
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}
Query the results of snapshot jobs
Call the QuerySnapshotJobList operation to query snapshot job results.
package main
import (
"encoding/json"
"strings"
"fmt"
"os"
mts20140618 "github.com/alibabacloud-go/mts-20140618/v6/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
// Description:
//
// Use your AccessKey ID and AccessKey secret to initialize a client.
//
// @return Client
//
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {
config := &openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
_result = &mts20140618.Client{}
_result, _err = mts20140618.NewClient(config)
return _result, _err
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
querySnapshotJobListRequest := &mts20140618.QuerySnapshotJobListRequest{}
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// Write your own code to display the response of the API operation if necessary.
_, _err = client.QuerySnapshotJobListWithOptions(querySnapshotJobListRequest, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
// The error message.
fmt.Println(tea.StringValue(error.Message))
// The URL of the corresponding error diagnostics page.
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}