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 V2.0 to submit and query snapshot jobs. 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
An 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.
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.
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
You can call the QuerySnapshotJobList operation to query the results of snapshot jobs.
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)
}
}