全部產品
Search
文件中心

ApsaraVideo Media Processing:拼接-開板和尾板

更新時間:Jun 30, 2024

在視頻製作中,可以將含有關鍵資訊的視頻起始、結尾內容以畫中畫的形式拼接嵌入到正片視頻中,稱為開板、尾板。本文為您介紹ApsaraVideo for Media Processing中Python SDK設定開板和尾板的範例程式碼。

範例程式碼

import os
import json

from urllib.parse import quote
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.SubmitJobsRequest import SubmitJobsRequest

# 讀取環境變數中設定的ACCESS_KEY   ACCESS_KEY_SECRET資訊
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 為調用的服務地區ID,支援的地區參考 https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/service-regions
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

mps_region_id = 'cn-shanghai'
pipeline_id = '9bad31a2c3d1c22c4df9c6****'
template_id = 'S00000001-200030'
oss_location = 'oss-cn-shanghai'
oss_bucket = '<your bucket name>'
oss_input_object = 'input.mp4'
oss_output_object = 'output.mp4'
head_object = 'head.mp4'
tail_object = 'tail.mp4'
request = SubmitJobsRequest()
request.set_accept_format('json')
# Input參數結構 object需要進行url編碼
job_input = {'Location': oss_location,
             'Bucket': oss_bucket,
             'Object': quote(head_object) }
request.set_Input(json.dumps(job_input))
# Output參數結構
output = {'OutputObject': quote(oss_output_object)}
output['TemplateId'] = template_id
# Output->OpeningList開板參數結構
opening_video = {'OpenUrl': 'http://%s.%s.aliyuncs.com/%s'%(oss_bucket, oss_location, quote(head_object)),
                 'Width': 640,
                 'Start': 2}
output['OpeningList'] = [opening_video]
# Output->TailSlateList尾板參數結構
tailslate_video = {'TailUrl': 'http://%s.%s.aliyuncs.com/%s'%(oss_bucket, oss_location, quote(tail_object)),
                   'Width': 640,
                   'BlendDuration': 3,
                   'BgColor': 'Black'}
output['TailSlateList'] = [tailslate_video]

# Outputs輸出參數結構
outputs = [output]
request.set_Outputs(json.dumps(outputs))
request.set_OutputBucket(oss_bucket)
request.set_OutputLocation(oss_location)
request.set_PipelineId(pipeline_id)

response_str = client.do_action_with_exception(request)
response = json.loads(response_str)
# 輸出列印
print(str(response, encoding='utf-8'))