全部產品
Search
文件中心

ApsaraVideo Live:阿里雲視頻加密

更新時間:Aug 13, 2025

阿里雲視頻加密(又稱阿里雲私人加密)可針對直播流進行加密,本文介紹優勢、整體架構和使用方法。

說明
  • 僅支援輸出HLS格式和FLV格式。

  • 只能使用阿里雲播放器。

  • H5端支援情況參見瀏覽器適配說明阿里雲私人加密視頻播放功能項。

背景

防盜鏈URL不能防止使用者通過一次付費行為拿到付費直播合法的防盜鏈URL,進而實現二次分發。因此,防盜鏈方案對於直播的著作權保護是遠遠不夠的。

優勢介紹

阿里雲視頻加密是對視頻資料加密,即使下載到本地,視頻本身也是被加密的,無法惡意二次分發。視頻加密可有效防止視頻泄露和盜鏈問題,廣泛用於線上教育、財經金融、行業培訓、獨播劇等線上著作權視頻領域。

採用阿里雲私人密碼編譯演算法,安全層級高,能夠便捷、高效、安全地保護視頻資源。

  • 每個媒體檔案擁有獨立的加密鑰匙,能有效避免採用單一密鑰時,一個密鑰的泄露引起大範圍的安全問題。

  • ApsaraVideo for Live提供完善的許可權管理機制“子帳號+播放憑證”。

  • ApsaraVideo for Live提供信封加密機制”密文Key+明文Key”,明文Key不儲存,所有過程只在記憶體中。

  • ApsaraVideo for Live提供安全的播放核心SDK。

整體架構

阿里雲視頻加密方案包含兩部分:加密轉碼+解密播放。

  • 加密轉碼:流程1~3。

    主播推流到直播中心後,直播服務負責通過Key Management Service產生明文Key和密文Key。通過直播轉碼服務,使用清除金鑰對音視頻進行對稱加密,並將密文Key存入視頻封裝。

  • 解密播放:流程4~11。

    播放終端如需播放此直播流,需嚮應用伺服器AppServer發起播放請求,擷取播流URL。使用播流URL向直播服務要求視頻流。直播服務將經過轉碼的加密視頻和密文Key傳輸到播放器SDK。

    播放終端用密文Key向直播服務擷取二次加密後的明文Key,直播服務再通過密文Key向Key Management Service擷取明文Key。播放終端將解密得到的明文Key傳給播放器SDK,播放器SDK解密視頻進行播放。

使用方式

阿里雲視頻加密是通過轉碼模板進行配置,可支援控制台和API兩種配置方式。

重要

使用阿里雲視頻加密需要配置KMS密鑰ID,如果沒有密鑰,請先前往KMS服務建立一個密鑰,需注意建立密鑰時KMS密鑰的地區需要和網域名稱的直播中心保持一致。具體操作,請參見建立密鑰

方式一:通過ApsaraVideo for Live控制台。具體操作,請參見通用轉碼自訂轉碼

方式二:API調用。

  1. 配置視頻加密轉碼模板,可調用AddLiveStreamTranscode添加通用轉碼配置資訊或調用AddCustomLiveStreamTranscode添加自訂轉碼配置資訊,設定加密配置參數EncryptParameters,其中EncryptType加密類型取值aliyun

    Java SDK添加通用轉碼配置程式碼範例如下:

    // This file is auto-generated, don't edit it. Thanks.
    package demo;
    
    import com.aliyun.auth.credentials.Credential;
    import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
    import com.aliyun.core.http.HttpClient;
    import com.aliyun.core.http.HttpMethod;
    import com.aliyun.core.http.ProxyOptions;
    import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
    import com.aliyun.sdk.service.live20161101.models.*;
    import com.aliyun.sdk.service.live20161101.*;
    import com.google.gson.Gson;
    import darabonba.core.RequestConfiguration;
    import darabonba.core.client.ClientOverrideConfiguration;
    import darabonba.core.utils.CommonUtil;
    import darabonba.core.TeaPair;
    
    //import javax.net.ssl.KeyManager;
    //import javax.net.ssl.X509TrustManager;
    import java.net.InetSocketAddress;
    import java.time.Duration;
    import java.util.*;
    import java.util.concurrent.CompletableFuture;
    import java.io.*;
    
    public class AddLiveStreamTranscode {
        public static void main(String[] args) throws Exception {
    
            // HttpClient Configuration
            /*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
                    .connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
                    .responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
                    .maxConnections(128) // Set the connection pool size
                    .maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
                    // Configure the proxy
                    .proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
                            .setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
                    // If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
                    .x509TrustManagers(new X509TrustManager[]{})
                    .keyManagers(new KeyManager[]{})
                    .ignoreSSL(false)
                    .build();*/
    
            // Configure Credentials authentication information, including ak, secret, token
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                    .accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                    .accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                    //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                    .build());
    
            // Configure the Client
            AsyncClient client = AsyncClient.builder()
                    .region("<Your RegionId>") // Region ID
                    //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                    .credentialsProvider(provider)
                    //.serviceConfiguration(Configuration.create()) // Service-level configuration
                    // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                      // Endpoint 請參考 https://api.aliyun.com/product/live
                                    .setEndpointOverride("live.aliyuncs.com")
                            //.setConnectTimeout(Duration.ofSeconds(30))
                    )
                    .build();
    
            // Parameter settings for API request
            AddLiveStreamTranscodeRequest addLiveStreamTranscodeRequest = AddLiveStreamTranscodeRequest.builder()
                    .regionId("<Your RegionId>")
                    .domain("<Your Domain>")
                    .app("<Your App Name>")
                    .template("<Your Template>")
                    .encryptParameters("<Your EncryptParameters>")
                    // Request-level configuration rewrite, can set Http request parameters, etc.
                    // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                    .build();
    
            // Asynchronously get the return value of the API request
            CompletableFuture<AddLiveStreamTranscodeResponse> response = client.addLiveStreamTranscode(addLiveStreamTranscodeRequest);
            // Synchronously get the return value of the API request
            AddLiveStreamTranscodeResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
            // Asynchronous processing of return values
            /*response.thenAccept(resp -> {
                System.out.println(new Gson().toJson(resp));
            }).exceptionally(throwable -> { // Handling exceptions
                System.out.println(throwable.getMessage());
                return null;
            });*/
    
            // Finally, close the client
            client.close();
        }
    
    }
    說明
    • 如果更改轉碼配置,需要進行重新推流後配置才可生效。

    • 使用JAVA SDK,具體請參見Java SDK使用說明

  2. 其他相關API。

    API名稱

    說明

    更新通用轉碼配置

    調用UpdateLiveStreamTranscode更新通用轉碼配置資訊。

    更新自訂轉碼配置

    調用UpdateCustomLiveStreamTranscode更新自訂轉碼配置資訊。

    查詢直播流轉碼配置

    調用DescribeLiveStreamTranscodeInfo查詢轉碼配置資訊。

    刪除轉碼配置

    調用DeleteLiveStreamTranscode刪除轉碼配置資訊。

相關文檔

使用視頻加密,會自動建立AliyunServiceRoleForLiveKes角色擷取KMS服務存取權限,更多資訊請參見直播Data Encryption Service關聯角色