You need to enable JavaScript to run this app.
导航
音视频播放
最近更新时间:2024.09.29 17:41:41首次发布时间:2021.02.23 10:42:41

本文为您提供了服务端 Go SDK 的媒资播放模块的接口调用示例。

前提条件

调用接口前,请先完成 SDK 的安装初始化

调用示例

签发临时播放 token

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

说明

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) {
        // Create a VOD instance in the specified region.
        // instance := vod.NewInstanceWithRegion("cn-north-1")
        vid := "your vid"
        tokenExpireTime := 600 // Token Expire Duration(s)
        instance := vod.NewInstance()

        // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see  https://www.volcengine.com/docs/4/65655.
        // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
        // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
        // instance.SetCredential(base.Credentials{
        // AccessKeyID:     "your ak",
        // SecretAccessKey: "your sk",
        //})

        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) {
        // Create a VOD instance in the specified region.
        // instance := vod.NewInstanceWithRegion("cn-north-1")
        instance := vod.NewInstance()
        
        // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see  https://www.volcengine.com/docs/4/65655.
        // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
        // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
        // 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",
                ForceExpire:        "your ForceExpire",
        }

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

签发私有加密 Token

私有加密 Token 用于 Web 点播 SDK 播放私有加密视频,详情请见以下文档:

私有加密 Token 可由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个私有加密 Token,您可以循环调用生成方法。

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) {
        // Create a VOD instance in the specified region.
        // instance := vod.NewInstanceWithRegion("cn-north-1")
        instance := vod.NewInstance()

        // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see  https://www.volcengine.com/docs/4/65655.
        // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
        // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
        // instance.SetCredential(base.Credentials{
        // AccessKeyID:     "your ak",
        // SecretAccessKey: "your sk",
        //})
        
        // Web 点播 SDK 内部携带 playAuthId、vid、unionInfo 向应用服务端请求获取私有加密 Token,详细示例代码和参数说明请见 [Web 端播放私有加密视频实现方式部分](https://www.volcengine.com/docs/4/68698#%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F-2)
        query := &request.VodGetPrivateDrmPlayAuthRequest{
                Vid:         "your vid",
                // 需指定 DrmType 为 webdevice
                DrmType:     "your drmType",
                PlayAuthIds: "your playAuthIds",
                UnionInfo:   "your unionInfo",
        }
        tokenExpireTime := 6000000 // Token Expire Duration(s)
        newToken, _ := instance.GetPrivateDrmAuthToken(query, tokenExpireTime)
        fmt.Println(newToken)
}

签发 HLS 标准加密 Token

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个 HLS 标准加密 Token,您可以循环调用生成方法。HLS 标准加密 Token 用于 Web 客户端播放 HLS 加密音视频,详见 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) {
        // Create a VOD instance in the specified region.
        // instance := vod.NewInstanceWithRegion("cn-north-1")
        instance := vod.NewInstance()
        
        // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see  https://www.volcengine.com/docs/4/65655.
        // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
        // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
        // instance.SetCredential(base.Credentials{
        // AccessKeyID:     "your ak",
        // SecretAccessKey: "your sk",
        //})
        
        expireDuration := int64(6000000) //change to your expire duration (s), no default duration
        token, _ := instance.CreateSha1HlsDrmAuthToken(expireDuration)
        fmt.Println(token)
}