You need to enable JavaScript to run this app.
导航
音视频播放
最近更新时间:2024.12.16 19:49:31首次发布时间:2021.02.23 10:42:40

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

前提条件

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

调用示例

签发临时播放 Token

对于存储在视频点播中的音视频,您可以使用客户端点播 SDK 通过临时播放 Token 自动获取播放地址进行播放。具体播放流程和临时播放 Token 说明,请见通过临时播放 Token 播放。为方便您的使用,视频点播服务端 SDK 对临时播放 Token 的签发进行了封装。您可调用生成方法通过 AK/SK 在本地签出临时播放 Token,不依赖外网。若希望同时生成多个临时播放 Token,可循环调用生成方法。

说明

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

# coding:utf-8
from __future__ import print_function

from volcengine.vod.VodService import VodService
from volcengine.vod.models.request.request_vod_pb2 import VodGetPlayInfoRequest

if __name__ == '__main__':
    # Create a VOD instance in the specified region.
    # vod_service = VodService('cn-north-1')
    vod_service = VodService()

    # 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/65646.
    # 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.
    # vod_service.set_ak('your ak')
    # vod_service.set_sk('your sk')

    try:
        vid = 'your vid'
        req = VodGetPlayInfoRequest()
        req.Vid = vid
        expire = 60  # seconds
        resp = vod_service.get_play_auth_token(req, expire)
    except Exception:
        raise
    else:
        print(resp)
    print('*' * 100)

获取播放地址

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

# coding:utf-8
from __future__ import print_function

from volcengine.vod.VodService import VodService
from volcengine.vod.models.request.request_vod_pb2 import VodGetPlayInfoRequest

if __name__ == '__main__':
    # Create a VOD instance in the specified region.
    # vod_service = VodService('cn-north-1')
    vod_service = VodService()

    # 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/65646.
    # 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.
    # vod_service.set_ak('your ak')
    # vod_service.set_sk('your sk')

    try:
        vid = 'your vid'
        req = VodGetPlayInfoRequest()
        req.Vid = vid
        req.Ssl = '1'
        req.NeedOriginal = '1'
        req.UnionInfo = 'your unionInfo'
        resp = vod_service.get_play_info(req)
    except Exception:
        raise
    else:
        print(resp)
        if resp.ResponseMetadata.Error.Code == '':
            print(resp.Result.PlayInfoList[0].MainPlayUrl)
        else:
            print(resp.ResponseMetadata.Error)
    print('*' * 100)

签发私有加密 Token

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

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

# coding:utf-8
from __future__ import print_function

from volcengine.vod.VodService import VodService
from volcengine.vod.models.request.request_vod_pb2 import VodGetPrivateDrmPlayAuthRequest

if __name__ == '__main__':
    # Create a VOD instance in the specified region.
    # vod_service = VodService('cn-north-1')
    vod_service = VodService()

    # 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/65646.
    # 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.
    # vod_service.set_ak('your ak')
    # vod_service.set_sk('your sk')

    try:
        # 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)
        vid = 'your vid'
        req = VodGetPrivateDrmPlayAuthRequest()
        req.Vid = vid
        # 需指定 DrmType 为 webdevice
        req.DrmType = 'your drm type'
        req.PlayAuthIds = 'a,b,c (your PlayAuthIds)'
        req.UnionInfo = 'your unionInfo'
        expire = 60  # seconds
        resp = vod_service.get_private_drm_play_auth_token(req, expire)
    except Exception:
        raise
    else:
        print(resp)

签发 HlS 标准加密 Token

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

# coding:utf-8
from __future__ import print_function

from volcengine.vod.VodService import VodService

if __name__ == '__main__':
    # Create a VOD instance in the specified region.
    # vod_service = VodService('cn-north-1')
    vod_service = VodService()

    # 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/65646.
    # 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.
    # vod_service.set_ak('your ak')
    # vod_service.set_sk('your sk')

    try:
        expireSeconds = 60000
        resp = vod_service.get_sha1_hls_drm_auth_token(expireSeconds)
    except Exception:
        raise
    else:
        print(resp)