You need to enable JavaScript to run this app.
导航

媒资播放

最近更新时间2023.10.13 17:01:59

首次发布时间2021.02.23 10:42:41

本文为您提供了服务端 Go SDK 的媒资播放模块相关的 API 调用示例。主要包含:签发 PlayAuthToken、获取播放地址、签发私有 DRM 加密 AuthToken 等。

初始化

使用前请先完成初始化,参考初始化

签发 PlayAuthToken

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个PlayAuthToken,您可以循环调用生成方法。PlayAuthToken用于客户端播放,详见客户端播放

package vod

import (
        "encoding/json"
        "fmt"
        "testing"
        "time"

        "github.com/volcengine/volc-sdk-golang/service/vod"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
)

func TestVod_GetPlayAuthToken(t *testing.T) {
        vid := "your vid"
        tokenExpireTime := 600 // Token Expire Duration(s)
        instance := vod.NewInstance()

        //instance.SetCredential(base.Credentials{
        // AccessKeyID:     "your ak",
        // SecretAccessKey: "your sk",
        //})

        // or set ak and ak as follow
        //instance.SetAccessKey("")
        //instance.SetSecretKey("")

        query := &request.VodGetPlayInfoRequest{
                Vid:             vid,
                Format:          "mp4",
                Definition:      "360p",
                FileType:        "video",
                LogoType:        "",
                Ssl:             "1",
                NeedThumbs:      "0",
                NeedBarrageMask: "0",
                PlayConfig:       "{\"PlayDomain\":\"zjh.baidu.com.vod.msadhbui21.com\"}",
        }
        newToken, _ := instance.GetPlayAuthToken(query, tokenExpireTime)
        fmt.Println(newToken)
}

获取播放地址

接口请求参数和返回参数详见 OpenAPI:获取播放地址

package vod

import (
        "fmt"
        "testing"

        "github.com/volcengine/volc-sdk-golang/base"
        "github.com/volcengine/volc-sdk-golang/service/vod"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
)

func Test_GetPlayInfo(t *testing.T) {
        instance := vod.NewInstance()
        instance.SetCredential(base.Credentials{
                AccessKeyID:     "your ak",
                SecretAccessKey: "your sk",
        })

        query := &request.VodGetPlayInfoRequest{
                Vid:                "your Vid",
                Format:             "your Format",
                Codec:              "your Codec",
                Definition:         "your Definition",
                FileType:           "your FileType",
                LogoType:           "your LogoType",
                Base64:             "your Base64",
                Ssl:                "your Ssl",
                NeedThumbs:         "your NeedThumbs",
                NeedBarrageMask:    "your NeedBarrageMask",
                CdnType:            "your CdnType",
                UnionInfo:          "your UnionInfo",
                HDRDefinition:      "your HDRDefinition",
                PlayScene:          "your PlayScene",
                DrmExpireTimestamp: "your DrmExpireTimestamp",
                Quality:            "your Quality",
                PlayConfig:         "your PlayConfig",
                NeedOriginal:       "your NeedOriginal",
        }

        resp, status, err := instance.GetPlayInfo(query)
        fmt.Println(status)
        fmt.Println(err)
        fmt.Println(resp.String())
}

签发私有 DRM 加密 AuthToken(PrivateDrmAuthToken)

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个PrivateDrmAuthToken,您可以循环调用生成方法。PrivateDrmAuthToken用于 Web 客户端播放视频点播自研 DRM 加密音视频,详见客户端播放

说明

在 Web 端播放时,调用 GetPrivateDrmAuthToken 生成方法,需要指定参数 DrmType 取值为 webdevice

package vod

import (
        "encoding/json"
        "fmt"
        "testing"
        "time"

        "github.com/volcengine/volc-sdk-golang/service/vod"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
)

func TestVod_GetPrivateDrmAuthToken(t *testing.T) {
        instance := vod.NewInstance()

        //instance.SetCredential(base.Credentials{
        // AccessKeyID:     "your ak",
        // SecretAccessKey: "your sk",
        //})

        // or set ak and ak as follow
        //instance.SetAccessKey("")
        //instance.SetSecretKey("")
        query := &request.VodGetPrivateDrmPlayAuthRequest{
                Vid:         "your vid",
                DrmType:     "your drmType",
                PlayAuthIds: "your playAuthIds",
                UnionInfo:   "your unionInfo",
        }
        tokenExpireTime := 6000000 // Token Expire Duration(s)
        newToken, _ := instance.GetPrivateDrmAuthToken(query, tokenExpireTime)
        fmt.Println(newToken)
}

签发 Hls 标准加密 AuthToken(Sha1HlsDrmAuthToken)

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个 Sha1HlsDrmAuthToken,您可以循环调用生成方法。Sha1HlsDrmAuthToken 用于 Web 客户端播放 Hls 加密音视频,详见客户端播放

package vod

import (
        "encoding/json"
        "fmt"
        "testing"
        "time"

        "github.com/volcengine/volc-sdk-golang/service/vod"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
)

func TestVod_GetSha1HlsDrmAuthToken(t *testing.T) {
        instance := vod.NewInstance()
        instance.SetCredential(base.Credentials{
        AccessKeyID:     "your ak",
        SecretAccessKey: "your sk",
        })

        // or set ak and ak as follow
        //instance.SetAccessKey("your ak")
        //instance.SetSecretKey("your sk")
        expireDuration := int64(6000000) //change to your expire duration (s), no default duration
        token, _ := instance.CreateSha1HlsDrmAuthToken(expireDuration)
        fmt.Println(token)
}