轉碼是將音視頻文件轉換成另一個或多個音視頻文件,以適應不同網絡帶寬、不同終端設備和不同的用戶需求。當控制臺創建的轉碼任務和工作流均無法滿足您的使用場景時,可以按需調用SubmitJobs接口進行轉碼。本文提供了Python SDK V2.0轉碼相關功能的API調用示例。
完整代碼
import os
import sys
from typing import List
from alibabacloud_mts20140618.client import Client as Mts20140618Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_mts20140618 import models as mts_20140618_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client() -> Mts20140618Client:
"""
使用AK&SK初始化賬號Client
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 必填,請確保代碼運行環境設置了環境變量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
# 必填,請確保代碼運行環境設置了環境變量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
)
config.endpoint = f'mts.cn-hangzhou.aliyuncs.com'
return Mts20140618Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
client = Sample.create_client()
submit_jobs_request = mts_20140618_models.SubmitJobsRequest(
#作業輸入
input='{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"example.flv","Referer": "用戶自行在OSS控制臺設置的OSS防盜鏈參數"}',
#作業輸出配置
outputs='[{"OutputObject":"exampleOutput.mp4","TemplateId":"6181666213ab41b9bc21da8ff5ff****","WaterMarks":[{"InputFile":{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"image_01.png"},"WaterMarkTemplateId":"9b772ce2740d4d55876d8b542d47****"}],"UserData":"testid-001"}]',
#輸出文件所在的OSS Bucket
output_bucket='exampleBucket',
#輸出文件所在的 OSS Bucket 的地域(OSS Region)
output_location='oss-cn-hangzhou',
#管道ID
pipeline_id='dd3dae411e704030b921e52698e5****'
)
runtime = util_models.RuntimeOptions()
try:
# 復制代碼運行請自行打印 API 的返回值
client.submit_jobs_with_options(submit_jobs_request, runtime)
except Exception as error:
# 此處僅做打印展示,請謹慎對待異常處理,在工程項目中切勿直接忽略異常。
# 錯誤 message
print(error.message)
# 診斷地址
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
@staticmethod
async def main_async(
args: List[str],
) -> None:
client = Sample.create_client()
submit_jobs_request = mts_20140618_models.SubmitJobsRequest(
#作業輸入。
input='{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"example.flv","Referer": "用戶自行在OSS控制臺設置的OSS防盜鏈參數"}',
#作業輸出配置。
outputs='[{"OutputObject":"exampleOutput.mp4","TemplateId":"6181666213ab41b9bc21da8ff5ff****","WaterMarks":[{"InputFile":{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"image_01.png"},"WaterMarkTemplateId":"9b772ce2740d4d55876d8b542d47****"}],"UserData":"testid-001"}]',
#輸出文件所在的OSS Bucket。
output_bucket='exampleBucket',
#輸出文件所在的 OSS Bucket 的地域(OSS Region)。
output_location='oss-cn-hangzhou',
#管道ID。
pipeline_id='dd3dae411e704030b921e52698e5****'
)
runtime = util_models.RuntimeOptions()
try:
# 復制代碼運行請自行打印 API 的返回值
await client.submit_jobs_with_options_async(submit_jobs_request, runtime)
except Exception as error:
# 此處僅做打印展示,請謹慎對待異常處理,在工程項目中切勿直接忽略異常。
# 錯誤 message
print(error.message)
# 診斷地址
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
Sample.main(sys.argv[1:])
相關文檔
文檔內容是否對您有幫助?