API-based transcoding

Updated at:

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

  1. Configure transcoding templates and watermark templates, and set up message notifications for an MPS queue.

  2. Upload videos to Object Storage Service (OSS).

  3. Call the API to submit transcoding jobs.

  4. Wait for ApsaraVideo Media Processing to complete the job and send a completion message to the queue.

  5. 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

Procedure

  1. Upload the input file to OSS by using the OSS console, an upload tool, or the upload SDK.

  2. Set up message queue notifications for the MPS queue.

  3. Submit a transcoding job.

  4. After receiving the completion message, call the QueryTranscodingJob operation to query the result and retrieve the output file URL.

  5. Obtain the Bucket, Location, and Object of the output file from the Output field returned by the QueryTranscodingJob operation, generate a presigned URL by using an OSS SDK, and pass the URL to downstream business services.

    1. Get the Bucket, Location, and Object of the output file from the Output field returned by the QueryTranscodingJob operation.

    2. 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, and Signature parameters 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.

    3. Send the presigned URL to the downstream business service through an HTTP callback.

  6. Play the video from the presigned URL or the output file URL.