You need to enable JavaScript to run this app.
对象存储

对象存储

复制全文
视频处理
视频截帧(Python SDK)
复制全文
视频截帧(Python SDK)

视频截帧功能可以从视频流中截取指定时刻的单帧画面,并按指定大小缩放成图片。本文介绍如何通过 TOS Python SDK 进行视频截帧。

示例代码

以下代码展示如何截取第 300ms 的视频帧,并将截取后的图片下载到本地。

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"
file_name = "temp.jpg"
style = "video/snapshot,t_300"

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)
    client.get_object_to_file(bucket=bucket_name, key=video_key, process=style, file_path=file_name)
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))

相关文档

关于视频截帧的详细介绍,请参见视频截帧

最近更新时间:2024.02.04 18:31:08
这个页面对您有帮助吗?
有用
有用
无用
无用