Skip to content

Commit 70813e6

Browse files
victorwjwuwanjiewu
andauthored
add transcode task and ut tests (#175)
* add:transcode_task_method * add:ut_for_trancode_task * add:ut_for_trancode_task Co-authored-by: wanjiewu <wanjiewu@tencent.com>
1 parent bff6762 commit 70813e6

File tree

4 files changed

+516
-1
lines changed

4 files changed

+516
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
*.pyc
1+
*.pyc

demo/ci_media.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# -*- coding=utf-8
2+
from qcloud_cos import CosConfig
3+
from qcloud_cos import CosS3Client
4+
from qcloud_cos.cos_comm import CiDetectType
5+
6+
import sys
7+
import logging
8+
import os
9+
import time
10+
11+
# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x
12+
13+
# https://cloud.tencent.com/document/product/436/48987
14+
15+
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
16+
17+
# 设置用户属性, 包括secret_id, secret_key, region
18+
# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成
19+
# 这里秘钥是从环境变量取得,如自己测试可改成自己对应的秘钥
20+
secret_id = os.environ["SECRETID"] # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
21+
secret_key = os.environ["SECRETKEY"] # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
22+
region = 'ap-chongqing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
23+
# COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224
24+
token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048
25+
26+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme='https') # 获取配置对象
27+
client = CosS3Client(config)
28+
29+
30+
bucket_name = 'demo-1253960454'
31+
32+
33+
def ci_get_media_queue():
34+
# 查询媒体队列信息
35+
response = client.ci_get_media_queue(
36+
Bucket=bucket_name
37+
)
38+
print(response)
39+
return response
40+
41+
def ci_create_media_transcode_watermark_jobs():
42+
# 创建转码任务
43+
body = {
44+
'Input':{
45+
'Object':'117374C.mp4'
46+
},
47+
'QueueId': 'pe943803693bd42d1a3105804ddaee525',
48+
'Tag': 'Transcode',
49+
'Operation': {
50+
'Output':{'Bucket':bucket_name, 'Region':region, 'Object':'117374C_output.mp4'},
51+
'TemplateId': 't02db40900dc1c43ad9bdbd8acec6075c5',
52+
# "WatermarkTemplateId": ["", ""],
53+
'Watermark': [
54+
{
55+
'Type':'Text',
56+
'Pos':'TopRight',
57+
'LocMode':'Absolute',
58+
'Dx':'64',
59+
'Dy': '64',
60+
'StartTime':'0',
61+
'EndTime':'1000.5',
62+
'Text': {
63+
'Text': '水印内容',
64+
'FontSize': '90',
65+
'FontType': 'simfang.ttf',
66+
'FontColor': '0xFFEEFF',
67+
'Transparency': '100',
68+
},
69+
},
70+
{
71+
'Type':'Image',
72+
'Pos':'TopLeft',
73+
'LocMode':'Absolute',
74+
'Dx':'100',
75+
'Dy': '100',
76+
'StartTime':'0',
77+
'EndTime':'1000.5',
78+
'Image': {
79+
'Url': 'http://'+bucket_name+".cos."+region+".myqcloud.com/1215shuiyin.jpg",
80+
'Mode': 'Fixed',
81+
'Width': '128',
82+
'Height': '128',
83+
'Transparency': '100',
84+
},
85+
}
86+
]
87+
}
88+
}
89+
# dict中数组类型的标签,都需要特殊处理
90+
lst = [
91+
'<Watermark>',
92+
'<WatermarkTemplateId>',
93+
'</WatermarkTemplateId>',
94+
'</Watermark>'
95+
]
96+
response = client.ci_create_media_jobs(
97+
Bucket=bucket_name,
98+
Jobs=body,
99+
Lst=lst,
100+
ContentType='application/xml'
101+
)
102+
print(response)
103+
return response
104+
105+
def ci_create_media_transcode_jobs():
106+
# 创建转码任务
107+
body = {
108+
'Input':{
109+
'Object':'117374C.mp4'
110+
},
111+
'QueueId': 'pe943803693bd42d1a3105804ddaee525',
112+
'Tag': 'Transcode',
113+
'Operation': {
114+
'Output':{'Bucket':bucket_name, 'Region':region, 'Object':'117374C_output.mp4'},
115+
'TemplateId': 't02db40900dc1c43ad9bdbd8acec6075c5'
116+
}
117+
}
118+
response = client.ci_create_media_jobs(
119+
Bucket=bucket_name,
120+
Jobs=body,
121+
Lst={},
122+
ContentType='application/xml'
123+
)
124+
print(response)
125+
return response
126+
127+
def ci_list_media_transcode_jobs():
128+
# 转码任务
129+
response = client.ci_list_media_jobs(
130+
Bucket=bucket_name,
131+
QueueId='pe943803693bd42d1a3105804ddaee525',
132+
Tag='Transcode',
133+
ContentType='application/xml'
134+
)
135+
print(response)
136+
return response
137+
138+
def ci_get_media_transcode_jobs():
139+
# 转码任务
140+
response = client.ci_get_media_jobs(
141+
Bucket=bucket_name,
142+
JobIDs='j3feb7ccc28fc11eca50b6f68c211dc6c,jb83bcc5a28fb11ecae48a1f29371c5f8',
143+
ContentType='application/xml'
144+
)
145+
print(response)
146+
return response
147+
148+
if __name__ == "__main__":
149+
#ci_get_media_queue()
150+
ci_get_media_transcode_jobs()
151+
#ci_create_media_transcode_jobs()

qcloud_cos/cos_client.py

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4816,6 +4816,232 @@ def ci_auditing_document_query(self, Bucket, JobID, **kwargs):
48164816
**kwargs
48174817
)
48184818

4819+
def ci_get_media_queue(self, Bucket, **kwargs):
4820+
"""查询媒体处理队列接口 https://cloud.tencent.com/document/product/436/54045
4821+
4822+
:param Bucket(string): 存储桶名称.
4823+
:param kwargs(dict): 设置请求的headers.
4824+
:return(dict): 查询成功返回的结果,dict类型.
4825+
4826+
.. code-block:: python
4827+
4828+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
4829+
client = CosS3Client(config)
4830+
# 查询媒体处理队列接口
4831+
response = client.ci_get_media_queue(
4832+
Bucket='bucket'
4833+
)
4834+
print response
4835+
"""
4836+
headers = mapped(kwargs)
4837+
final_headers = {}
4838+
params = {}
4839+
for key in headers:
4840+
if key.startswith("response"):
4841+
params[key] = headers[key]
4842+
else:
4843+
final_headers[key] = headers[key]
4844+
headers = final_headers
4845+
4846+
params = format_values(params)
4847+
4848+
path = "/queue"
4849+
url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci)
4850+
logger.info("get_media_queue result, url=:{url} ,headers=:{headers}, params=:{params}".format(
4851+
url=url,
4852+
headers=headers,
4853+
params=params))
4854+
rt = self.send_request(
4855+
method='GET',
4856+
url=url,
4857+
bucket=Bucket,
4858+
auth=CosS3Auth(self._conf, path, params=params),
4859+
params=params,
4860+
headers=headers)
4861+
4862+
data = xml_to_dict(rt.content)
4863+
# 单个元素时将dict转为list
4864+
format_dict(data, ['QueueList'])
4865+
return data
4866+
4867+
def ci_create_media_jobs(self, Bucket, Jobs={}, Lst={}, **kwargs):
4868+
""" 创建任务接口 https://cloud.tencent.com/document/product/436/54013
4869+
4870+
:param Bucket(string): 存储桶名称.
4871+
:param Jobs(dict): 创建任务的配置.
4872+
:param Lst(dict): 创建任务dict转xml时去除Key数组.
4873+
:param kwargs(dict): 设置请求的headers.
4874+
:return(dict): 查询成功返回的结果,dict类型.
4875+
4876+
.. code-block:: python
4877+
4878+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
4879+
client = CosS3Client(config)
4880+
# 创建任务接口
4881+
response = client.ci_create_media_jobs(
4882+
Bucket='bucket'
4883+
Jobs={},
4884+
Lst={}
4885+
)
4886+
print response
4887+
"""
4888+
headers = mapped(kwargs)
4889+
final_headers = {}
4890+
params = {}
4891+
for key in headers:
4892+
if key.startswith("response"):
4893+
params[key] = headers[key]
4894+
else:
4895+
final_headers[key] = headers[key]
4896+
headers = final_headers
4897+
4898+
params = format_values(params)
4899+
xml_config = format_xml(data=Jobs, root='Request', lst=Lst)
4900+
path = "/jobs"
4901+
url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci)
4902+
logger.info("create_media_jobs result, url=:{url} ,headers=:{headers}, params=:{params}, xml_config=:{xml_config}".format(
4903+
url=url,
4904+
headers=headers,
4905+
params=params,
4906+
xml_config=xml_config))
4907+
rt = self.send_request(
4908+
method='POST',
4909+
url=url,
4910+
bucket=Bucket,
4911+
data=xml_config,
4912+
auth=CosS3Auth(self._conf, path, params=params),
4913+
params=params,
4914+
headers=headers)
4915+
4916+
data = xml_to_dict(rt.content)
4917+
# 单个元素时将dict转为list
4918+
format_dict(data, ['JobsDetail'])
4919+
return data
4920+
4921+
def ci_get_media_jobs(self, Bucket, JobIDs, **kwargs):
4922+
""" 查询任务接口 https://cloud.tencent.com/document/product/436/54010
4923+
4924+
:param Bucket(string): 存储桶名称.
4925+
:param JobIDs(string): 任务ID,以,分割多个任务ID.
4926+
:param kwargs(dict): 设置请求的headers.
4927+
:return(dict): 查询成功返回的结果,dict类型.
4928+
4929+
.. code-block:: python
4930+
4931+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
4932+
client = CosS3Client(config)
4933+
# 创建任务接口
4934+
response = client.ci_get_media_jobs(
4935+
Bucket='bucket'
4936+
JobIDs={}
4937+
)
4938+
print response
4939+
"""
4940+
headers = mapped(kwargs)
4941+
final_headers = {}
4942+
params = {}
4943+
for key in headers:
4944+
if key.startswith("response"):
4945+
params[key] = headers[key]
4946+
else:
4947+
final_headers[key] = headers[key]
4948+
headers = final_headers
4949+
4950+
params = format_values(params)
4951+
path = "/jobs/" + JobIDs
4952+
url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci)
4953+
logger.info("get_media_jobs result, url=:{url} ,headers=:{headers}, params=:{params}".format(
4954+
url=url,
4955+
headers=headers,
4956+
params=params))
4957+
rt = self.send_request(
4958+
method='GET',
4959+
url=url,
4960+
bucket=Bucket,
4961+
auth=CosS3Auth(self._conf, path, params=params),
4962+
params=params,
4963+
headers=headers)
4964+
logger.debug("ci_get_media_jobs result, url=:{url} ,content=:{content}".format(
4965+
url=url,
4966+
content=rt.content))
4967+
4968+
data = xml_to_dict(rt.content)
4969+
# 单个元素时将dict转为list
4970+
format_dict(data, ['JobsDetail'])
4971+
return data
4972+
4973+
def ci_list_media_jobs(self, Bucket, QueueId, Tag, StartCreationTime=None, EndCreationTime=None, OrderByTime='Desc', States='All', Size=10, NextToken='', **kwargs):
4974+
""" 查询任务接口 https://cloud.tencent.com/document/product/436/54011
4975+
4976+
:param Bucket(string): 存储桶名称.
4977+
:param QueueId(string): 队列ID.
4978+
:param Tag(string): 任务类型.
4979+
:param StartCreationTime(string): 开始时间.
4980+
:param EndCreationTime(string): 结束时间.
4981+
:param OrderByTime(string): 排序方式.
4982+
:param States(string): 任务状态.
4983+
:param Size(string): 任务个数.
4984+
:param NextToken(string): 请求的上下文,用于翻页.
4985+
:param kwargs(dict): 设置请求的headers.
4986+
:return(dict): 查询成功返回的结果,dict类型.
4987+
4988+
.. code-block:: python
4989+
4990+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
4991+
client = CosS3Client(config)
4992+
# 创建任务接口
4993+
response = client.ci_get_media_jobs(
4994+
Bucket='bucket'
4995+
QueueId='',
4996+
Tag='Transcode'
4997+
)
4998+
print response
4999+
"""
5000+
headers = mapped(kwargs)
5001+
final_headers = {}
5002+
params = {}
5003+
for key in headers:
5004+
if key.startswith("response"):
5005+
params[key] = headers[key]
5006+
else:
5007+
final_headers[key] = headers[key]
5008+
headers = final_headers
5009+
5010+
params = format_values(params)
5011+
path = "/jobs"
5012+
url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci)
5013+
url = u"{url}?{QueueId}&{Tag}&{OrderByTime}&{States}&{Size}&{NextToken}".format(
5014+
url=to_unicode(url),
5015+
QueueId=to_unicode('queueId='+QueueId),
5016+
Tag=to_unicode('tag='+Tag),
5017+
OrderByTime=to_unicode('orderByTime='+OrderByTime),
5018+
States=to_unicode('states='+States),
5019+
Size=to_unicode('size='+str(Size)),
5020+
NextToken=to_unicode('nextToken='+NextToken)
5021+
)
5022+
if StartCreationTime is not None:
5023+
url = u"{url}&{StartCreationTime}".format(StartCreationTime=to_unicode('startCreationTime='+StartCreationTime))
5024+
if EndCreationTime is not None:
5025+
url = u"{url}&{EndCreationTime}".format(EndCreationTime=to_unicode('endCreationTime='+EndCreationTime))
5026+
logger.info("list_media_jobs result, url=:{url} ,headers=:{headers}, params=:{params}".format(
5027+
url=url,
5028+
headers=headers,
5029+
params=params))
5030+
rt = self.send_request(
5031+
method='GET',
5032+
url=url,
5033+
bucket=Bucket,
5034+
auth=CosS3Auth(self._conf, path, params=params),
5035+
params=params,
5036+
headers=headers)
5037+
logger.debug("list_media_jobs result, url=:{url} ,content=:{content}".format(
5038+
url=url,
5039+
content=rt.content))
5040+
data = xml_to_dict(rt.content)
5041+
# 单个元素时将dict转为list
5042+
format_dict(data, ['JobsDetail'])
5043+
return data
5044+
48195045

48205046
if __name__ == "__main__":
48215047
pass

0 commit comments

Comments
 (0)