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

获取视频信息(Python SDK)

最近更新时间2024.02.04 18:31:08

首次发布时间2023.10.12 11:15:43

获取视频信息功能可以获取指定视频资源的元信息。本文介绍如何通过 TOS Python SDK 获取视频信息。

示例代码

以下代码展示如何获取视频信息。

import json
import os
import tos
from tos.enum import TierType
from tos.models2 import RestoreJobParameters

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
# 填写 Bucket 所在区域对应的 Endpoint。如果以华北2(北京)为例,则 your endpoint 填写为 tos-cn-beijing.volces.com,your region 填写为 cn-beijing。
endpoint = "your endpoint"
region = "your region"
bucket_name = "bucket-test"
video_key = "video.mp4"
style = "video/info"

try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现。
    client = tos.TosClientV2(ak, sk, endpoint, region)
    client.put_object_from_file(bucket=bucket_name, key=video_key, file_path=video_key)
    object_stream = client.get_object(bucket=bucket_name, key=video_key, process=style)
    video_info = json.load(object_stream)
    print(video_info)
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常。
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息。
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存。
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

相关文档

关于获取视频信息的详细介绍,请参见查看视频信息