1、功能介绍
华为云提供了媒体处理服务端SDK,您可以直接集成服务端SDK来调用媒体处理的相关API,从而实现对媒体处理的快速操作。 该示例展示了如何通过Python版SDK创建、取消和查询截图任务。
您将学到什么?
如何通过Python版SDK来体验媒体处理(MPC)创建、取消和查询截图任务功能
2、开发时序图
创建截图任务

取消截图任务

查询截图任务

3、前置条件
- 1、已注册华为帐号并开通华为云,已进行实名认证。
- 2、已具备开发环境 ,支持python 3及以上版本。
- 3、已获取账号对应的 Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥 。
- 4、已获取转码服务对应区域的项目ID,请在控制台“我的凭证 > API凭证”页面上查看项目ID。具体请参见API凭证。
- 5、已将需要处理的媒资文件上传至MPC同区域的OBS桶中,并将OBS桶进行授权,允许MPC访问。具体请参见上传音视频文件和获取云资源授权。
4、SDK获取和安装
媒体转码服务端SDK支持python 3及以上版本。执行“ python --version”检查当前python的版本信息。
使用服务端SDK前,您需要安装“huaweicloudsdkcore ”和“huaweicloudsdkmpc”,具体的SDK版本号请参见SDK开发中心。
使用pip安装
执行如下命令安装华为云Python SDK核心库以及相关服务库:
Copied!
# 安装核心库
pip install huaweicloudsdkcore
# 安装MPC服务库
pip install huaweicloudsdkmpc
使用源码安装
执行如下命令安装华为云 Python SDK核心库以及相关服务库:
Copied!
# 安装核心库
cd huaweicloudsdkcore-${version}
python setup.py install
# 安装MPC服务库
cd huaweicloudsdkmpc-${version}
python setup.py install
6、关键代码片段
6.1、创建截图任务
Copied!
@staticmethod
def create_thumbnails_task():
"""
创建截图任务
@return taskId 提交的截图任务的任务ID
"""
_input = ObsObjInfo(
bucket="<example-bucket>",
location="<Region ID>",
object="<example-path/input.mp4>"
)
output = ObsObjInfo(
bucket="<example-bucket>",
location="<Region ID>",
object="<example-path/output>"
)
request = CreateThumbnailsTaskRequest()
body = CreateThumbReq()
list_thumbnail_para_dots = []
list_thumbnail_para_dots.append(50000)
thumbnail_parabody = ThumbnailPara()
thumbnail_parabody.type = "DOTS"
thumbnail_parabody.output_filename = "photo"
thumbnail_parabody.time = 10
thumbnail_parabody.start_time = 100
thumbnail_parabody.duration = 1
thumbnail_parabody.dots = list_thumbnail_para_dots
thumbnail_parabody.format = 1
thumbnail_parabody.width = 96
thumbnail_parabody.height = 96
body.thumbnail_para = thumbnail_parabody
body.output = output
body.input = _input
request.body = body
try:
response = ThumbnailsTaskSolution.init_mpc_client().create_thumbnails_task(request)
print(response)
return response.task_id
except ConnectionException as e:
logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e)
except RequestTimeoutException as e:
logger.error("The server processing times out and does not return a response: ", e)
except ServiceResponseException as e:
logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg)
return ""
6.2、取消截图任务
Copied!
@staticmethod
def delete_thumbnails_task(task_id):
"""
取消截图任务
@param taskId 任务ID
"""
request = DeleteThumbnailsTaskRequest()
request.task_id = task_id
try:
response = ThumbnailsTaskSolution.init_mpc_client().delete_thumbnails_task(request)
print(response)
except ConnectionException as e:
logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e)
except RequestTimeoutException as e:
logger.error("The server processing times out and does not return a response: ", e)
except ServiceResponseException as e:
logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg)
6.3、查询截图任务
Copied!
@staticmethod
def list_thumbnails_task(task_id):
"""
查询截图任务
@param taskId 任务ID
"""
request = ListThumbnailsTaskRequest()
list_request_task_id = []
list_request_task_id.append(task_id)
request.task_id = list_request_task_id
try:
response = ThumbnailsTaskSolution.init_mpc_client().list_thumbnails_task(request)
print(response)
except ConnectionException as e:
logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e)
except RequestTimeoutException as e:
logger.error("The server processing times out and does not return a response: ", e)
except ServiceResponseException as e:
logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg)
7、运行结果
创建截图任务,返回截图任务ID
Copied!
{"task_id": "1024"}
查询解截图任务的状态和结果
Copied!
{
"task_array" : [
{
"task_id" : 2528,
"status" : "SUCCEEDED",
"create_time" : 20201118121333,
"end_time" : 20201118121336,
"input" : {
"bucket" : "example-bucket",
"location" : "region01",
"object" : "example-input.ts"
},
"output" : {
"bucket" : "example-bucket",
"location" : "region01",
"object" : "example-output/example-path"
},
"thumbnail_info" : [ {
"pic_name" : "9.jpg"
}, {
"pic_name" : "5.jpg"
} ]
}
],
"is_truncated" : 0,
"total" : 1
}
取消已下发的截图任务
取消截图任务成功时,状态码:204,响应体为空。
取消截图任务失败时,状态码:400,响应体如下:
Copied!
{
"error_code" : "MPC.10202",
"error_msg" : "Invalid request parameter"
}
8、参考
本示例的代码工程仅用于简单演示,实际开发过程中应严格遵循开发指南。访问以下链接可以获取详细信息:开发指南
1、功能介绍
华为云提供了媒体处理服务端SDK,您可以直接集成服务端SDK来调用媒体处理的相关API,从而实现对媒体处理的快速操作。 该示例展示了如何通过Python版SDK创建、取消和查询截图任务。
您将学到什么?
如何通过Python版SDK来体验媒体处理(MPC)创建、取消和查询截图任务功能
2、开发时序图
创建截图任务
取消截图任务
查询截图任务
3、前置条件
4、SDK获取和安装
媒体转码服务端SDK支持python 3及以上版本。执行“ python --version”检查当前python的版本信息。
使用服务端SDK前,您需要安装“huaweicloudsdkcore ”和“huaweicloudsdkmpc”,具体的SDK版本号请参见SDK开发中心。
使用pip安装
执行如下命令安装华为云Python SDK核心库以及相关服务库:
# 安装核心库 pip install huaweicloudsdkcore # 安装MPC服务库 pip install huaweicloudsdkmpc
使用源码安装
执行如下命令安装华为云 Python SDK核心库以及相关服务库:
# 安装核心库 cd huaweicloudsdkcore-${version} python setup.py install # 安装MPC服务库 cd huaweicloudsdkmpc-${version} python setup.py install
5、接口参数说明
关于接口参数的详细说明可参见:
a.新建截图任务
b.取消截图任务
c.查询截图任务
6、关键代码片段
6.1、创建截图任务
@staticmethod def create_thumbnails_task(): """ 创建截图任务 @return taskId 提交的截图任务的任务ID """ # 设置截图输入视频地址 _input = ObsObjInfo( bucket="<example-bucket>", location="<Region ID>", object="<example-path/input.mp4>" ) output = ObsObjInfo( bucket="<example-bucket>", location="<Region ID>", object="<example-path/output>" ) # 创建截图请求 request = CreateThumbnailsTaskRequest() body = CreateThumbReq() list_thumbnail_para_dots = [] list_thumbnail_para_dots.append(50000) # 设置截图类型,此处理按时间点截图 thumbnail_parabody = ThumbnailPara() # 设置采样类型,指定时间点截图 thumbnail_parabody.type = "DOTS" thumbnail_parabody.output_filename = "photo" thumbnail_parabody.time = 10 thumbnail_parabody.start_time = 100 thumbnail_parabody.duration = 1 thumbnail_parabody.dots = list_thumbnail_para_dots thumbnail_parabody.format = 1 thumbnail_parabody.width = 96 thumbnail_parabody.height = 96 body.thumbnail_para = thumbnail_parabody body.output = output body.input = _input request.body = body # 发送截图请求 try: response = ThumbnailsTaskSolution.init_mpc_client().create_thumbnails_task(request) print(response) return response.task_id except ConnectionException as e: logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e) except RequestTimeoutException as e: logger.error("The server processing times out and does not return a response: ", e) except ServiceResponseException as e: logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg) return ""
6.2、取消截图任务
@staticmethod def delete_thumbnails_task(task_id): """ 取消截图任务 @param taskId 任务ID """ request = DeleteThumbnailsTaskRequest() request.task_id = task_id try: response = ThumbnailsTaskSolution.init_mpc_client().delete_thumbnails_task(request) print(response) except ConnectionException as e: logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e) except RequestTimeoutException as e: logger.error("The server processing times out and does not return a response: ", e) except ServiceResponseException as e: logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg)
6.3、查询截图任务
@staticmethod def list_thumbnails_task(task_id): """ 查询截图任务 @param taskId 任务ID """ request = ListThumbnailsTaskRequest() list_request_task_id = [] list_request_task_id.append(task_id) request.task_id = list_request_task_id try: response = ThumbnailsTaskSolution.init_mpc_client().list_thumbnails_task(request) print(response) except ConnectionException as e: logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e) except RequestTimeoutException as e: logger.error("The server processing times out and does not return a response: ", e) except ServiceResponseException as e: logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg)
7、运行结果
创建截图任务,返回截图任务ID
{"task_id": "1024"}
查询解截图任务的状态和结果
{ "task_array" : [ { "task_id" : 2528, "status" : "SUCCEEDED", "create_time" : 20201118121333, "end_time" : 20201118121336, "input" : { "bucket" : "example-bucket", "location" : "region01", "object" : "example-input.ts" }, "output" : { "bucket" : "example-bucket", "location" : "region01", "object" : "example-output/example-path" }, "thumbnail_info" : [ { "pic_name" : "9.jpg" }, { "pic_name" : "5.jpg" } ] } ], "is_truncated" : 0, "total" : 1 }
取消已下发的截图任务
取消截图任务成功时,状态码:204,响应体为空。
取消截图任务失败时,状态码:400,响应体如下:
{ "error_code" : "MPC.10202", "error_msg" : "Invalid request parameter" }
8、参考
本示例的代码工程仅用于简单演示,实际开发过程中应严格遵循开发指南。访问以下链接可以获取详细信息:开发指南