通过阅读本文,您可以了解通过服务端SDK调用OpenAPI发起并查询智能媒体服务截图任务的示例代码。
使用说明
发起截图任务前,需要先创建截图模板。通过TemplateId(截图模板ID)发起截图任务,发起截图任务时,支持对截图模板参数进行覆盖重置,截图任务处理成功后,可以根据返回的JobId(任务ID)查询该截图任务的详细信息、截图任务列表及截图的访问URL。
示例代码
您可以通过阿里云OpenAPI开发者门户在线调试。
import json
import os
import sys
from typing import List
from alibabacloud_ice20201109.client import Client as ICE20201109Client
# 引入阿里云IMS SDK
from alibabacloud_ice20201109 import models as ice20201109_models
# Credentials和云产品SDK都需引入Client,此处为创建别名
from alibabacloud_credentials.client import Client as CredClient
# 引入阿里云SDK核心包
from alibabacloud_tea_openapi.models import Config
#######需要的依赖#############
#pip install alibabacloud_credentials
#pip install alibabacloud_ice20201109==1.3.11
class Sample:
# 初始化客户端
@staticmethod
def create_client(region: str) -> ICE20201109Client:
# 使用默认凭证初始化Credentials Client
cred = CredClient()
config = Config(credential = cred)
# 配置云产品服务接入地址(endpoint)
config.endpoint = 'ice.' + region + '.aliyuncs.com'
# 使用Credentials Client初始化ECS Client
return ICE20201109Client(config)
# @staticmethod
# def create_client(
# access_key_id: str,
# access_key_secret: str,
# region: str,
# ) -> ICE20201109Client:
# 如需硬编码AccessKey ID和AccessKey Secret,代码如下,但强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全
# config = open_api_models.Config(
# # 必填,您的 AccessKey ID,
# access_key_id = access_key_id,
# # 必填,您的 AccessKey Secret,
# access_key_secret = access_key_secret
# )
# # 访问的域名
# config.endpoint = 'ice.' + region + '.aliyuncs.com'
# return ICE20201109Client(config)
# 读取命令行参数
@staticmethod
def main() -> None:
region = 'cn-shanghai'
# 初始化客户端
client = Sample.create_client(region)
# 创建截图模板
snapshot_template_config = '{"Type":"Normal","FrameType":"normal","Time":0,"Count":10}'
create_custom_template_request = ice20201109_models.CreateCustomTemplateRequest(
name = 'sdk_sample_snapshot_template',
type = 2,
template_config = snapshot_template_config
)
create_custom_template_response = client.create_custom_template(create_custom_template_request)
print('[LOG]', json.dumps(create_custom_template_response.to_map()))
template_id = create_custom_template_response.body.custom_template.template_id
# 发起截图任务
submit_snapshot_job_request = ice20201109_models.SubmitSnapshotJobRequest(
name = 'sdk_sample_snapshot_job',
input = ice20201109_models.SubmitSnapshotJobRequestInput(
type = 'Media',
media = '3945e42d-1823498****'
),
output = ice20201109_models.SubmitSnapshotJobRequestOutput(
type = 'Media',
media = '3945e42d-1823498****'
),
template_config = ice20201109_models.SubmitSnapshotJobRequestTemplateConfig(
template_id = template_id
)
)
submit_snapshot_job_response = client.submit_snapshot_job(submit_snapshot_job_request)
print('[LOG]', json.dumps(submit_snapshot_job_response.to_map()))
job_id = submit_snapshot_job_response.body.job_id
# 查询截图任务详细信息
get_snapshot_job_request = ice20201109_models.GetSnapshotJobRequest(
job_id=job_id
)
get_snapshot_job_response = client.get_snapshot_job(get_snapshot_job_request)
print('[LOG]', json.dumps(get_snapshot_job_response.to_map()))
# 获取截图任务列表
list_snapshot_jobs_request = ice20201109_models.ListSnapshotJobsRequest(
page_size=5
)
list_snapshot_jobs_response = client.list_snapshot_jobs(list_snapshot_jobs_request)
print('[LOG]', json.dumps(list_snapshot_jobs_response.to_map()))
# 获取截图任务输出图片的访问URL
get_snapshot_urls_request = ice20201109_models.GetSnapshotUrlsRequest(
job_id='20b48fb04483915d4f2cd8ac****',
page_size=5
)
get_snapshot_urls_response = client.get_snapshot_urls(get_snapshot_urls_request)
print('[LOG]', json.dumps(get_snapshot_urls_response.to_map()))
if __name__ == '__main__':
Sample.main()
相关接口
文档内容是否对您有帮助?