All Products
Search
Document Center

ApsaraVideo Media Processing:Create a workflow for HLS encryption

Last Updated:Aug 26, 2026

Video encryption protects your video content from unauthorized access and distribution. It helps prevent video leaks and hotlinking. This feature is often used in fields that require high content security, such as online education and finance. This topic provides sample code that demonstrates how to use the Python software development kit (SDK) to create a workflow for HTTP Live Streaming (HLS) encryption.

Introduction

This is an example of an API call to create an HLS encryption workflow. For the complete procedure, from creating the workflow to playing the encrypted video, see HLS encryption and playback. For more information about the MPS SDK, see Installation.

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.AddMediaWorkflowRequest import AddMediaWorkflowRequest

# Read the AccessKey ID and AccessKey secret from the environment variables.
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# The region_id is the ID of the region where the service is called. For a list of supported regions, see https://www.alibabacloud.com/help/zh/mps/product-overview/regions-and-endpoints
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

request = AddMediaWorkflowRequest()
request.set_Name("hls-transcode-workflow")

startActivity = {
    "Type": "Start",
    "Parameters": {
        "InputFile": {
            "Bucket": "<your input bucket>",
            "Location": "oss-cn-shanghai",
            "ObjectPrefix": "media/"
        },
        "PipelineId": "<PipelineId>"
    }
}

transcodeActivity = {
    "Type": "Transcode",
    "Parameters": {
        "Outputs": [
            {
                "OutputObject": quote("transcode/{ObjectPrefix}/{FileName}.{ExtName}"),
                "TemplateId": "<hls TemplateId>",
                "Encryption": {
                    "Type": "hls-aes-128",
                    "KeyUri": "<The URI of the decryption key. Example: http://example.aliyundoc.com>"
                }
            }
        ],
        "OutputLocation": "oss-cn-shanghai",
        "OutputBucket": "<your output bucket>"
    }
}

reportActivity = {
    "Type": "Report",
    "Parameters": {
    }
}
topology = {
    "Activities": {
        "Act-Start": startActivity,
        "transcodingNode": transcodeActivity,
        "reportNode": reportActivity
    },
    "Dependencies": {
        "Act-Start": ["transcodingNode"],
        "transcodingNode": ["reportNode"],
        "reportNode": []
    }
}

request.set_Topology(topology)

response = client.do_action_with_exception(request)
# Print the output.
print(str(response, encoding='utf-8'))