This topic provides examples on how to use the API operations of the media processing module. The API operations are encapsulated in the server operation for Go. You can call the API operations to submit transcoding and snapshot jobs, query snapshot data, and preprocess videos in a production studio.
Initialize a client
Before you can use the SDK, initialize a client. For more information, see Initialization.
Submit a transcoding job without encryption
You can call the SubmitTranscodeJobs operation to submit a transcoding job.
For more information about the request and response parameters of this operation, see SubmitTranscodeJobs. Sample code:
The following code provides an example on transcoding without encryption. For more information about video encryption supported by Alibaba Cloud, see Alibaba Cloud video encryption.
package main
import (
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"encoding/json"
"fmt"
)
func NormalSubmitTranscodeJobs(client *vod.Client) (response *vod.SubmitTranscodeJobsResponse, err error) {
request := vod.CreateSubmitTranscodeJobsRequest()
// Specify the ID of the video that you want to transcode.
request.VideoId = "<VideoId>"
// Specify the transcoding template group.
request.TemplateGroupId = "<TemplateGroupId>"
/*
// Configure optional parameters to be overridden.
overrideParams := BuildOverrideParams()
jsonParams, err := json.Marshal(overrideParams)
if err != nil {
fmt.Println("json.Marshal failed:", err)
return
}
request.OverrideParams = string(jsonParams)
fmt.Println(request.OverrideParams)
*/
request.AcceptFormat = "JSON"
return client.SubmitTranscodeJobs(request)
}
func main() {
client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
if err != nil {
panic(err)
}
response, err := NormalSubmitTranscodeJobs(client)
if err != nil {
panic(err)
}
fmt.Println(response.GetHttpContentString())
fmt.Println(response.RequestId)
for _, job := range response.TranscodeJobs.TranscodeJob {
fmt.Printf("%s\n", job.JobId)
}
}
/*
* Construct watermark parameters to be overridden. You can override only the URL of an image watermark or the content of a text watermark.
* Configure the watermark parameters to be overridden. Make sure that the ID of the watermark is associated with the ID of the transcoding template that you use. The ID of the transcoding template is specified by TranscodeTemplateId.
* You can call this operation to add only a watermark whose ID is associated with the ID of the transcoding template that you use.
*/
func BuildOverrideParams() (overrideParams map[string]interface{}) {
// Override watermarks.
watermarks := []map[string]interface{}{}
// Override an image watermark. Example of the URL of an image file: https://example-bucket-****.oss-cn-shanghai.aliyuncs.com/watermarks/sample.png.
watermark1 := map[string]interface{}{"WatermarkId": "<WatermarkId>", "FileUrl": "<your File URL>"}
watermarks = append(watermarks, watermark1)
// Override a text watermark.
watermark2 := map[string]interface{}{"WatermarkId": "<WatermarkId>", "Content": "new Text"}
watermarks = append(watermarks, watermark2)
overrideParams = map[string]interface{}{"Watermarks": watermarks}
return overrideParams
}
Submit a transcoding job with HLS encryption enabled
You can call the SubmitTranscodeJobs operation to submit a transcoding job with HTTP Live Streaming (HLS) encryption enabled.
For more information about the request and response parameters of this operation, see SubmitTranscodeJobs. Sample code:
The following code provides an example on transcoding with HLS encryption enabled. For more information, see HLS encryption.
package main
import (
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"encoding/json"
"fmt"
)
func HLSEncryptSubmitTranscodeJobs(client *vod.Client) (response *vod.SubmitTranscodeJobsResponse, err error) {
request := vod.CreateSubmitTranscodeJobsRequest()
// Specify the ID of the video that you want to transcode.
request.VideoId = "<VideoId>"
// Specify the transcoding template group.
request.TemplateGroupId = "<TemplateGroupId>"
// Use Key Management Service (KMS) to generate a random encryption key.
encryptConfig, err := BuildEncryptConfig(client)
if err != nil {
panic(err)
}
jsonConfig, err := json.Marshal(encryptConfig)
if err != nil {
fmt.Println("json.Marshal failed:", err)
return
}
request.EncryptConfig = string(jsonConfig)
request.AcceptFormat = "JSON"
return client.SubmitTranscodeJobs(request)
}
func main() {
client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
if err != nil {
panic(err)
}
response, err := HLSEncryptSubmitTranscodeJobs(client)
if err != nil {
panic(err)
}
fmt.Println(response.GetHttpContentString())
fmt.Println(response.RequestId)
for _, job := range response.TranscodeJobs.TranscodeJob {
fmt.Printf("%s\n", job.JobId)
}
}
// The generated random data key for encryption.
func BuildEncryptConfig(client *vod.Client) (encryptConfig map[string]interface{}, err error) {
request := vod.CreateGenerateKMSDataKeyRequest()
// Generate a random data key for encryption. The response contains the plaintext and ciphertext of the data key.
// Pass only the ciphertext of the data key for standard video encryption.
request.AcceptFormat = "JSON"
request.Scheme = "HTTPS"
response, err := client.GenerateKMSDataKey(request)
if err != nil {
panic(err)
}
// The URI of the operation that is used to decrypt the data key. To obtain the URI, concatenate the URL of the decryption service and the ciphertext of the data key. The ciphertext to decrypt varies among videos. Take note that you must deploy the decryption service.
// You can customize the name of the Ciphertext parameter. The name in this example is for reference only.
decryptKeyUri := "http://example.aliyundoc.com/decrypt?" + "Ciphertext=" + response.CiphertextBlob
encryptConfig = map[string]interface{}{"DecryptKeyUri": decryptKeyUri, "KeyServiceType": "KMS",
"CipherText": response.CiphertextBlob}
return encryptConfig, nil
}
Submit a snapshot job
You can call the SubmitSnapshotJob operation to submit a snapshot job.
For more information about the request and response parameters of this operation, see SubmitSnapshotJob. Sample code:
For more information about how to create a snapshot template, see Create a snapshot template.
package main
import (
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"encoding/json"
"fmt"
)
func MySubmitSnapshotJob(client *vod.Client) (response *vod.SubmitSnapshotJobResponse, err error) {
request := vod.CreateSubmitSnapshotJobRequest()
// Specify the ID of the video from which you want to capture snapshots.
request.VideoId = "<VideoId>"
// Specify the ID of the snapshot template.
//request.SnapshotTemplateId = "<snapshotTemplateId>"
// If you specify the ID of the snapshot template, the following parameters are ignored:
request.Count = "50"
request.SpecifiedOffsetTime = "0"
request.Interval = "1"
request.Width = "200"
request.Height = "200"
// Configure image sprite-related parameters. This step is optional.
spriteSnapshotConfig := map[string]interface{}{"CellWidth": 120, "CellHeight": 68, "Columns": 3,
"Lines": 10, "Padding": 20, "Margin": 50}
// Specify whether to retain the source image after an image sprite is generated.
spriteSnapshotConfig["KeepCellPic"] = "keep"
spriteSnapshotConfig["Color"] = "tomato"
jsonSpriteConfig, err := json.Marshal(spriteSnapshotConfig)
if err != nil {
fmt.Println("json.Marshal failed:", err)
return
}
request.SpriteSnapshotConfig = string(jsonSpriteConfig)
request.AcceptFormat = "JSON"
return client.SubmitSnapshotJob(request)
}
func main() {
client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
if err != nil {
panic(err)
}
response, err := MySubmitSnapshotJob(client)
if err != nil {
panic(err)
}
fmt.Println(response.GetHttpContentString())
fmt.Println(response.RequestId)
fmt.Println(response.SnapshotJob.JobId)
}
Query snapshot data
You can call the ListSnapshots operation to query snapshot data.
For more information about the request and response parameters of this operation, see ListSnapshots. Sample code:
package main
import (
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"fmt"
)
func MyListSnapshots(client *vod.Client) (response *vod.ListSnapshotsResponse, err error) {
request := vod.CreateListSnapshotsRequest()
request.VideoId = "<VideoId>"
request.SnapshotType = "CoverSnapshot"
request.PageNo = "1"
request.PageSize = "20"
request.AuthTimeout = "86400"
request.AcceptFormat = "JSON"
return client.ListSnapshots(request)
}
func main() {
client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
if err != nil {
panic(err)
}
response, err := MyListSnapshots(client)
if err != nil {
panic(err)
}
fmt.Println(response.GetHttpContentString())
fmt.Println(response.RequestId)
fmt.Println(response.MediaSnapshot.Total)
for _, snapshot := range response.MediaSnapshot.Snapshots.Snapshot {
fmt.Printf("%d: %s\n", snapshot.Index, snapshot.Url)
}
}
Preprocess videos in a production studio
You can call the SubmitPreprocessJobs operation to preprocess videos in a production studio.
For more information about the request and response parameters of this operation, see SubmitPreprocessJobs. Sample code:
package main
import (
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/vod"
"192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"encoding/json"
"fmt"
)
func MySubmitPreprocessJobs(client *vod.Client) (response *vod.SubmitPreprocessJobsResponse, err error) {
request := vod.CreateSubmitPreprocessJobsRequest()
request.VideoId = "<VideoId>"
request.PreprocessType = "LivePreprocess"
request.AcceptFormat = "JSON"
return client.SubmitPreprocessJobs(request)
}
func main() {
client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
if err != nil {
panic(err)
}
response, err := MySubmitPreprocessJobs(client)
if err != nil {
panic(err)
}
fmt.Println(response.GetHttpContentString())
fmt.Println(response.RequestId)
for _, job := range response.PreprocessJobs.PreprocessJob {
fmt.Printf("%s\n", job.JobId)
}
}