Merging combines multiple videos with different formats, encodings, and resolutions. The output is a new video with a single, unified format, encoding, and resolution. This feature is often used to add fixed headers and trailers or to merge recorded live streams. Clipping trims a segment from a video and exports it as a new video. This feature is often used to extract highlights or key content from a video. This topic provides API call examples for the merging and clipping features of the Python SDK.
Sample code
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
# Read the AccessKey ID and AccessKey secret from environment variables.
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# The ID of the region where the service is deployed. For a list of supported regions, see https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)
mps_region_id = 'cn-shanghai'
pipeline_id = '9ba1a2b3c561c22c4df9c6****'
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')
# Structure of the Input parameter.
job_input = {'Location': oss_location,
'Bucket': oss_bucket,
'Object': quote(head_object) }
request.set_Input(json.dumps(job_input))
# Structure of the Output parameter.
output = {'OutputObject': quote(oss_output_object)}
output['TemplateId'] = template_id
output['Video'] = {'Width': 1280,
'Height': 720}
# The MergeList parameter in Output for merging videos.
# The object name must be URL-encoded.
merge_video = {'MergeURL': 'http://%s.%s.aliyuncs.com/%s'%(oss_bucket, oss_location, quote(oss_input_object))}
merge_tail = {'MergeURL': 'http://%s.%s.aliyuncs.com/%s'%(oss_bucket, oss_location, quote(tail_object))}
output['MergeList'] = [merge_video, merge_tail]
# Structure of the Outputs parameter.
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 the response.
print(str(response, encoding='utf-8'))References
该文章对您有帮助吗?