API-based transcoding
Overview
When workflows cannot meet your needs, use the API to submit transcoding jobs with custom business logic. For example, you can skip transcoding for certain videos or apply different configurations per video.
Benefits
Flexible job submission based on custom business logic.
Transcoding, container format conversion, watermarking, HLS-AES128 standard encryption, and video editing.
Job completion notifications to a message queue or notification service.
URL-based playback.
Architecture:
Parsing
Configure transcoding templates and watermark templates, and set up message notifications for an MPS queue.
Upload videos to Object Storage Service (OSS).
Call the API to submit transcoding jobs.
Wait for ApsaraVideo Media Processing to complete the job and send a completion message to the queue.
Start playback.
Limits
Each transcoding job generates one output file. Jobs can be submitted in batches.
Supports HLS-AES128 standard encryption only. Alibaba Cloud proprietary cryptography is not supported.
Supports URL-based playback only, not media ID-based playback. To enable automatic definition switchover and multi-format support, associate multiple outputs with different definitions and formats.
Prerequisites
(Optional) Create a custom transcoding template in the ApsaraVideo Media Processing console.
(Optional) Create a custom watermark template in the ApsaraVideo Media Processing console.
Procedure
Upload the input file to OSS by using the OSS console, an upload tool, or the upload SDK.
After receiving the completion message, call the QueryTranscodingJob operation to query the result and retrieve the output file URL.
Obtain the
Bucket,Location, andObjectof the output file from theOutputfield returned by the QueryTranscodingJob operation, generate a presigned URL by using an OSS SDK, and pass the URL to downstream business services.Get the
Bucket,Location, andObjectof the output file from theOutputfield returned by the QueryTranscodingJob operation.Use an OSS SDK to generate a presigned URL for the object, and set a validity period in seconds, for example, 3600 seconds. The following example uses the OSS SDK for Python (
oss2):import oss2 auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET) # Build the endpoint based on the Location returned in the previous step, and use the returned Bucket as bucket_name. bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', bucket_name) # object_key is the Object returned in the previous step. 3600 is the validity period, in seconds. url = bucket.sign_url('GET', object_key, 3600)The generated presigned URL contains the
Expires,OSSAccessKeyId, andSignatureparameters and can be accessed over the Internet to play the transcoded video. The URL expires at the time specified by Expires and must be regenerated after it expires.Send the presigned URL to the downstream business service through an HTTP callback.
Play the video from the presigned URL or the output file URL.