全部产品

媒体处理

本篇文档提供了Go SDK媒体处理模块相关功能的API调用示例,包含提交转码作业(HLS标准加密、普通)、提交截图作业、查询截图数据、导播台视频预处理。

初始化客户端

使用前请先初始化客户端,请参见初始化

提交转码作业-普通

调用SubmitTranscodeJobs接口,完成提交转码作业-普通功能。

接口参数和返回字段请参见SubmitTranscodeJobs。调用示例如下:

说明

此示例代码为普通转码(不加密)。阿里云视频加密方案请参见阿里云视频加密

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()

    // 需要转码的视频ID
    request.VideoId = "<VideoId>"

    // 指定转码模板组
    request.TemplateGroupId = "<TemplateGroupId>"

    /*
    // 可选设置覆盖参数,如替换水印
    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)
    }
}

/*
* 构建覆盖参数,目前只支持图片水印文件地址、文字水印的内容覆盖;如不使用可忽略
* 需要替换的水印信息对应水印ID必须是关联在指定的转码模板组ID上(即TranscodeTemplateId)
* 不支持通过本接口去增加一个没有关联上的水印
*/
func BuildOverrideParams() (overrideParams map[string]interface{}) {
    // 以替换水印为例
    watermarks := []map[string]interface{}{}

    // 替换图片水印文件
    watermark1 := map[string]interface{}{"WatermarkId": "<WatermarkId>", "FileUrl": "https://sample.oss-cn-shanghai.aliyuncs.com/watermarks/sample.png"}
    watermarks = append(watermarks, watermark1)

    // 替换文字水印
    watermark2 := map[string]interface{}{"WatermarkId": "<WatermarkId>", "Content": "new Text"}
    watermarks = append(watermarks, watermark2)

    overrideParams = map[string]interface{}{"Watermarks": watermarks}

    return overrideParams
}

提交转码作业-HLS标准加密

调用SubmitTranscodeJobs接口,完成提交转码作业-HLS标准加密功能。

接口参数和返回字段请参见SubmitTranscodeJobs。调用示例如下:

说明

此示例代码为使用HLS标准加密提交转码作业的示例。

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"
    "192.168.0.0/16/aliyun/alibaba-cloud-sdk-go/services/kms"
    "fmt"
)

func HLSEncryptSubmitTranscodeJobs(client *vod.Client, kmsClient *kms.Client) (response *vod.SubmitTranscodeJobsResponse, err error) {
    request := vod.CreateSubmitTranscodeJobsRequest()

    // 需要转码的视频ID
    request.VideoId = "<VideoId>"

    // 指定转码模板组
    request.TemplateGroupId = "<TemplateGroupId>"

    // 使用KMS生成随机的加密密钥
    encryptConfig, err := BuildEncryptConfig(kmsClient)
    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)
    }
    kmsClient, err := InitKmsClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := HLSEncryptSubmitTranscodeJobs(client, kmsClient)
    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)
    }
}

/*
* 生成HLS标准加密参数的可选配置,如不使用可忽略
* 此时依赖KMS服务,需要安装KMS依赖包 aliyun-python-sdk-kms,并引入相关类
* 生成密钥API参考:https://help.aliyun.com/document_detail/28948.html
*/
// 初始化KMS客户端,与点播客户端类似
func InitKmsClient(accessKeyId  string, accessKeySecret string) (client *kms.Client, err error) {

    // KMS接入区域,与点播位于同一区域
    regionId := "cn-shanghai"

    // 创建授权对象
    credential := &credentials.AccessKeyCredential{
        accessKeyId,
        accessKeySecret,
    }

    // 自定义config
    config := sdk.NewConfig()
    config.AutoRetry = true      // 失败是否自动重试
    config.MaxRetryTime = 3      // 最大重试次数
    config.Timeout = 3000000000  // 连接超时,单位:纳秒;默认为3秒

    // 创建kmsClient实例
    return kms.NewClientWithOptions(regionId, config, credential)
}

// 随机生成加密的密钥
func BuildEncryptConfig(client *kms.Client) (encryptConfig map[string]interface{}, err error) {
    request := kms.CreateGenerateDataKeyRequest()

    // 随机生成一个加密的密钥,返回的response包含明文密钥以及密文密钥,
    // 视频标准加密只需要传递密文秘钥即可,以保证安全
    request.KeyId = "<serviceKey>"
    request.KeySpec = "AES_128"
    request.AcceptFormat = "JSON"
    request.Scheme = "HTTPS"       // KMS接口必须使用HTTPS

    response, err := client.GenerateDataKey(request)
    if err != nil {
        panic(err)
    }

    // 解密接口地址,该参数需要将每次生成的密文秘钥与接口URL拼接生成,表示每个视频的解密的密文秘钥都不一样;注意您需要自己部署解密服务
    // Ciphertext作为解密接口的参数名称,可自定义,此处只作为参考
    decryptKeyUri := "http://decrypt.demo.com/decrypt?" + "Ciphertext=" + response.CiphertextBlob
    encryptConfig = map[string]interface{}{"DecryptKeyUri": decryptKeyUri, "KeyServiceType": "KMS",
        "CipherText": response.CiphertextBlob}

    return  encryptConfig, nil
}

提交截图作业

调用SubmitSnapshotJob接口,完成提交截图作业功能。

接口参数和返回字段请参见SubmitSnapshotJob。调用示例如下:

说明

创建截图模板详细请参见截图模板

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()

    // 需要截图的视频ID
    request.VideoId = "<VideoId>"

    // 截图模板ID
    //request.SnapshotTemplateId = "<snapshotTemplateId>"

    // 如果设置了截图模板ID,则会忽略以下参数
    request.Count = "50"
    request.SpecifiedOffsetTime = "0"
    request.Interval = "1"
    request.Width = "200"
    request.Height = "200"

    // 雪碧图参数,如不使用可不设置
    spriteSnapshotConfig := map[string]interface{}{"CellWidth": 120, "CellHeight": 68, "Columns": 3,
        "Lines": 10, "Padding": 20, "Margin": 50}
    // 保留雪碧图原始图
    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)
}

查询截图数据

调用ListSnapshots接口,完成查询截图数据功能。

接口参数和返回字段请参见ListSnapshots。调用示例如下:

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)
    }
}

导播台视频预处理

调用SubmitPreprocessJobs接口,完成导播台视频预处理功能。

接口参数和返回字段请参见SubmitPreprocessJobs。调用示例如下:

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)
    }
}