ApsaraVideo for Live錄製可對當前直播即時錄製並進行歷史回放。本文介紹ApsaraVideo for Live錄製功能。
功能介紹
ApsaraVideo for Live是以即時資料流的形式播放推流端資料,如果想要在直播結束後再次觀看,就必須要使用直播錄製功能。
直播錄製就是將直播中心接收到的推流資料進行錄製,儲存至您指定的儲存位置。目前直播錄製儲存提供兩種方式,錄製儲存至VOD和錄製儲存至OSS。
直播錄製的原理
直播錄製的過程,是通過拉取您推上來的直播流,將音視頻封裝成一個個單獨的媒體切片(封裝格式為TS),然後進行儲存。直播錄製在工作時遵循以下原則:
直播錄製只修改音視頻的封裝格式(RTMP/FLV轉成TS),絕不修改音視頻內容(即編碼層內容)。舉個例子,如果您推送上來的直播流是花屏的,那麼錄製下來的也是花屏的。
直播錄製有許可權往使用者儲存地址寫入錄製檔案,但是不會也沒有許可權修改/刪除使用者儲存地址的任務檔案(包括已寫入的錄製檔案)。使用者儲存地址中的錄製檔案,完全由您自己管理。
使用直播錄製功能會產生錄製服務費,預設錄製檔案格式為TS/M3U8,如需轉碼封裝處理,將產生錄製轉封裝費。計費規則請參見直播錄製費用。
限制條件
音視頻編碼格式
由於直播錄製需要先將直播流切分成TS檔案,因此您推上來的直播流的音視頻編碼格式必須滿足TS封裝格式的要求。根據FLV標準和ISO/IEC 13818-1標準,直播錄製目前僅支援以下編碼格式:
視頻:H264、HEVC、MPEG4
音頻:AAC、MP3
如果直播流包含了非以上格式編碼的音視頻,直播錄製可能會出現:無法產生錄製檔案、錄製黑屏、錄製沒有聲音,以及其他一些無法預見的異常情況。
異常直播流處理機制
標準流(滿足編碼要求且幀率/時間戳記穩定) ✅ 100%正常產生錄製檔案
輕微異常流(偶發幀率波動/時間戳記跳變) ✅ 系統自動相容處理
嚴重異常流(長期無視訊框架/時間戳記錯亂/主要畫面格缺失) ❌ 無法保證錄製成功率
功能對比
錄製儲存至VOD和錄製儲存至OSS都可以對直播內容進行錄製儲存。兩者使用情境有所不同,您可以根據自己的業務決定具體採用哪種方式進行儲存。
儲存方式 | 關聯產品服務 | 支援錄製方式 | 封裝格式 | 適用情境 |
錄製儲存至VOD | 開通ApsaraVideo for VOD服務 |
|
| 傾向於對錄製內容進行二次生產,注重錄製內容的後期處理,如剪輯、播放、設定錄製視頻封面等。 |
錄製儲存至OSS | 開通OSS服務 |
| 傾向於錄製內容管理,注重儲存。 |
同一個直播流不能同時配置兩種儲存方式。
直播錄製都會先將直播流切片成一個個的TS切片,然後再合成對應封裝格式的錄製檔案。封裝為除TS/M3U8外的格式將產生轉封裝費用。
自動錄製生效規則
您可以指定網域名稱、應用程式名稱或流名稱,錄製某個網域名稱下所有App或App下所有的直播流,也可以精確錄製某一路直播流。AppName和StreamName可以填為*,表示所有AppName和所有StreamName。
配置儲存至OSS或儲存至VOD的錄製模板時您可以配置多個錄製模板,但是如果出現一路直播流同時可以匹配到多個錄製模板時,錄製模板生效存在優先順序,具體優先順序如下(數值越小,優先順序越高):
優先順序 | DomainName | AppName | StreamName |
1 | ✓ | ✓ | ✓ |
2 | ✓ | ✓ | * |
3 | ✓ | * | * |
✓ 代表通過控制台或API配置自動錄製規則時對應參數有值,且值不為*,* 代表對應參數值為*。
錄製儲存至VOD
前置條件
若採用錄製儲存至VOD方式進行直播錄製功能配置,需要開通ApsaraVideo for VOD服務,並進行配置管理儲存Bucket。
錄製檔案儲存體在ApsaraVideo for VODVOD中,會產生儲存費用,在VOD中計費請參見點播基礎服務計費。
在啟用點播系統Bucket時,需要注意點播系統Bucket需要與待佈建網域名的直播中心同地區。
使用金融雲帳號不可使用視訊直播錄製到點播。
功能配置
ApsaraVideo for Live目前提供兩種方式添加錄製儲存至VOD配置。
完成配置後重新推流錄製配置才會生效。
斷流180秒後產生錄製檔案。
方法一:控制台配置
在左側導覽列選擇功能管理>直播錄製,進入直播錄製頁面,選擇儲存至VOD配置頁簽。
選擇待配置的播流網域名稱,並且單擊添加。
配置錄製模板。

錄製模板參數及說明如下表所示。
參數
描述
AppName
視頻的應用程式名稱,輸入的AppName必須與直播推流的AppName保持一致,方可生效。如果您想要進行網域名稱層級錄製,輸入星號(*)即可。
StreamName
儲存至VOD支援流層級的錄製。您只需輸入指定的流名稱即可。如果您想要進行全部流錄製,即該AppName下的流全部錄製,輸入星號(*)即可。
說明AppName與StreamName參數支援英文、數字、“-”、“_”符號,長度限制在255個字元以內。
儲存地址
可選擇當前地區已經在點播服務中建立的儲存地址。若未找到可點擊重新整理進行同步。
錄製周期
錄製周期為當前直播轉為點播檔案後的最大時間長度。範圍為15~360分鐘,最大支援 6 小時錄製。超過 6 小時,系統將按照錄製命名規則產生新檔案。ts切片時間長度預設為 30s。
錄製轉碼模板
從列表中選擇儲存轉碼規則,可以在點播服務中對錄製的視頻進行轉碼處理。可轉碼為不同規格的視頻,也可以不轉碼即保持原畫格式。
錄製轉碼模板具體操作,請參見ApsaraVideo for VOD轉碼配置。
說明ApsaraVideo for VOD轉碼模板所在地區需與您當前網域名稱所在地區保持一致。例如:當前網域名稱為華東2區,ApsaraVideo for VOD轉碼模板地區也需要在華東2區。
自動合并
開啟後可在直播錄製結束後自動將多個錄製周期檔案合并成一個錄製檔案儲存體到VOD中。
自動合并轉碼模板
從列表中選擇儲存轉碼規則,對自動合成出來的視頻在點播服務中進行一次轉碼,錄製轉碼規則從當前點播系統中擷取。自動合并轉碼模板具體操作,請參見轉碼模板。
說明ApsaraVideo for VOD轉碼模板所在地區需與您當前網域名稱所在地區保持一致。例如:當前網域名稱為華東2區,ApsaraVideo for VOD轉碼模板地區也需要在華東2區。
單擊確定。
方法二:API配置
調用添加直播錄製轉點播配置介面添加配置。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 AddLiveRecordVodConfig {
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
AddLiveRecordVodConfigRequest addLiveRecordVodConfigRequest = AddLiveRecordVodConfigRequest.builder()
.regionId("<Your RegionId>")
.domainName("<Your DomainNmae>")
.appName("<Your AppName>")
.streamName("<Your StreamName>")
.vodTranscodeGroupId("<Your VodTranscodeGroupId>")
// 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<AddLiveRecordVodConfigResponse> response = client.addLiveRecordVodConfig(addLiveRecordVodConfigRequest);
// Synchronously get the return value of the API request
AddLiveRecordVodConfigResponse 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();
}
}以上範例程式碼表示,對<DomainName>網域名稱進行錄製儲存至VOD配置,AppName為<AppName>,StreamName為<StreamName>。錄製內容不進行轉碼(VOD_NO_TRANSCODE),儲存至點播系統Bucket<StorageLocation>。
AppName和StreamName配置詳情請參見:自動錄製生效規則。
錄製內容轉碼
錄製儲存至VOD檔案格式為m3u8,如果想儲存其他格式可以通過配置錄製轉碼模板實現。
addLiveRecordVodConfigRequest.setVodTranscodeGroupId("<TranscodeGroupId>");轉碼模板需要在點播系統進行配置,具體操作請參見點播轉碼模板。
<TranscodeGroupId>為點播系統配置的轉碼模板組ID。
配置轉碼後,會對錄製檔案進行轉碼(原檔案仍然保留),產生轉碼地址。
錄製儲存至OSS
前置條件
若採用錄製儲存至OSS方式進行直播錄製功能配置,需要完成以下操作:
開通OSS服務並建立Bucket:具體操作請參見建立儲存空間。
重要錄製檔案儲存體在OSS中,會產生儲存費用,在OSS中計費請參見儲存費用。
直播錄製儲存至OSS所需Bucket的地區與錄製配置的網域名稱所在地區必須一致。例如,直播網域名稱在華東2時,Bucket必須選擇華東2。
配置直播寫入OSS許可權:正常情況下,開通直播服務時,您已完成自動授權。
重要如果該許可權意外被刪除可點擊同意雲資源訪問授權完成快速授權。
建議您使用阿里雲帳號完成授權,若使用RAM使用者必須擁有以下許可權:
ram:CreateRole、ram:GetRole、ram:ListPoliciesForRole、ram:AttachPolicyToRole,授予RAM使用者以上許可權存在安全風險(不推薦)。
功能配置
ApsaraVideo for Live目前提供兩種方式添加錄製儲存至OSS配置。
完成配置後重新推流錄製配置才會生效。
斷流超過斷流拼接時間長度(預設180秒)後產生錄製檔案。
方法一:控制台配置
在左側導覽列選擇功能管理>直播錄製,進入直播錄製頁面,選擇儲存至OSS配置頁簽。
選擇待配置的播流網域名稱,並單擊添加。
配置錄製模板。

表 1. 錄製模板參數
參數
描述
AppName
視頻的應用程式名稱,輸入的AppName必須與直播推流地址的AppName保持一致,方可生效。如果您想要進行網域名稱層級錄製,輸入星號(*)可。
StreamName
儲存至OSS支援流層級的錄製。您只需輸入指定的流名稱即可。如果您想要進行全部流錄製,即該AppName下的流全部錄製,輸入星號(*)可。
說明AppName與StreamName參數支援英文、數字、短劃線(-)、底線(_)符號,長度限制在255字元以內。
*號只能作為單獨輸入,無法在字串中作為萬用字元輸入。
儲存位置
可選擇當前地區在OSS中建立的Bucket。
說明儲存Bucket分為兩類:
標準Bucket:用於常規檔案儲存體
媒體Bucket:專用於視頻轉碼處理,存入該類型的檔案可自動觸發MPS轉碼任務
當前系統未對Bucket類型進行標識區分,如需執行視頻轉碼,請自行記錄所使用的媒體Bucket名稱,以確保轉碼後的媒體檔案儲存位置準確。
斷流拼接時間長度
直播斷流時間長度超過設定的拼接時間長度後,將會產生新檔案,斷流拼接時間長度支援15~21600秒。
儲存格式
支援flv、m3u8、mp4、cmaf四種格式。
說明至少配置一個儲存格式,其中CMAF格式不能與M3U8格式同時選擇。
儲存規則
預設的儲存路徑為:record/{AppName}/{StreamName}/{EscapedStartTime }_{EscapedEndTime } 。
樣本:
AppName為liveApp****,StreamName為liveStream****,當錄製m3u8格式的儲存路徑為record/liveApp****/liveStream****/{EscapedStartTime}_{EscapedEndTime }。
單個TS時間長度
單個切片時間長度,預設為30秒。支援單個TS時間長度範圍5~30秒。
錄製周期
錄製周期範圍為15分鐘~360分鐘,最大支援6小時錄製。超過6小時,系統將按照錄製命名規則產生新檔案。
說明錄製周期為當前直播轉為點播檔案後的最大時間長度。
配置錄製轉碼流。(可選)
重要使用直播轉碼功能會產生轉碼費用,按轉碼標準、解析度規格及對應總轉碼時間長度計費。計費規則詳見直播轉碼費用。

表 2. 錄製轉碼流參數
參數
描述
錄製轉碼流
開啟錄製轉碼流開關,配置參數。
儲存格式
支援flv、m3u8、mp4、cmaf四種格式。
說明至少配置一個儲存格式,其中CMAF格式不能與M3U8格式同時選擇。
儲存規則
預設的儲存路徑為:transcodeRecord/{AppName}/{StreamName}_{轉碼模板ID}/{EscapedStartTime }_{EscapedEndTime }。
樣本:
AppName為liveApp****,StreamName為liveStream****,轉碼模板ID為lld,當錄製m3u8格式的實際儲存路徑為transcodeRecord/liveApp****/liveStream****_lld/{EscapedStartTime}_{EscapedEndTime }。
單個TS時間長度
單個切片時間長度,預設為30秒。支援單個TS時間長度5~30秒。
錄製周期
錄製周期範圍為15分鐘~360分鐘,最大支援6小時錄製。超過6小時,系統將按照錄製命名規則產生新檔案。
說明錄製周期為當前直播轉為點播檔案後的最大時間長度。
轉碼模板ID
支援選擇指定轉碼模板(上限10個)或全部轉碼模板。
轉碼模板所屬AppName需和錄製模板保持一致才可生效,若無轉碼模板,請先添加轉碼模板,請參見通用轉碼。
單擊確定。
方法二:API配置
調用添加錄製配置介面添加配置。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 AddLiveAppRecordConfig {
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
AddLiveAppRecordConfigRequest addLiveAppRecordConfigRequest = AddLiveAppRecordConfigRequest.builder()
.domainName("<Your DomainName>")
.appName("<Your AppName>")
.ossEndpoint("<Your OSSEndpoint>")
.ossBucket("<Your OSSBucket>")
.streamName("<Your StreamName>")
// 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<AddLiveAppRecordConfigResponse> response = client.addLiveAppRecordConfig(addLiveAppRecordConfigRequest);
// Synchronously get the return value of the API request
AddLiveAppRecordConfigResponse 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();
}
}以上範例程式碼表示,對<DomainName>網域名稱進行錄製儲存至OSS配置,AppName為<AppName>,StreamName為<StreamName>,儲存格式為m3u8,儲存至<OssBucket>中。
<OssEndpoint>可通過<OssBucket>所屬地區在OSS地區和Endpoint進行查看。
AppName和StreamName配置詳情請參見:自動錄製生效規則。
錄製轉碼流
如果希望在儲存錄製內容時減少錄製檔案體積,可通過錄製轉碼流實現(在保證畫質品質的前提下,調整視頻碼率、提高視頻壓縮率、減小檔案體積)。錄製儲存至OSS功能支援同時儲存轉碼流與原始流,也支援僅儲存轉碼流或原始流。
使用直播轉碼功能會產生轉碼費用,按轉碼標準、解析度規格及對應總轉碼時間長度計費。計費規則詳見直播轉碼費用。
自訂錄製策略
在功能配置中,我們進行了直播錄製功能的配置,在直播推流時即會開始對直播內容進行錄製,我們稱其為自動錄製。
但有些時候,我們希望能夠通過業務決策自主決定是否需要對某一次直播內容進行錄製。直播錄製功能提供瞭解決這類問題的方法,自主決定錄製分為兩種情況:
由直播中心回調詢問您的業務系統是否需要對該直播內容進行錄製,得到響應需要錄製後再進行錄製,我們稱其為按需錄製。
由您的業務系統給直播服務發出通知對直播內容進行錄製,我們稱其為手動錄製。
按需錄製

實現按需錄製,需要您業務系統提供直播服務調用的URL地址,具體說明可參見按需錄製回調。
設定錄製功能按需錄製範例程式碼如下:
//OnDemand欄位填1,表示通過HTTP回調開啟按需錄製。
addLiveAppRecordConfigRequest.setOnDemand(1);同時調用 添加網域名稱層級錄製回調配置API進行錄製回調配置。
手動錄製
實現手動錄製,需要將自動錄製功能關閉。範例程式碼如下:
//OnDemand欄位填7,表示不進行自動錄製。
addLiveAppRecordConfigRequest.setOnDemand(7);然後在直播的過程中,您可以根據業務情況通過調用 即時錄製指令API開啟錄製。
停止錄製
無論是自動錄製,按需錄製,手動錄製,都可以調用即時錄製指令API主動停止錄製。
// 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 RealTimeRecordCommand {
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
RealTimeRecordCommandRequest realTimeRecordCommandRequest = RealTimeRecordCommandRequest.builder()
.regionId("<Your RegionId>")
.command("<Your Command>")
.domainName("<Your DomainName>")
.appName("<Your AppName>")
.streamName("<Your StreamName>")
// 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<RealTimeRecordCommandResponse> response = client.realTimeRecordCommand(realTimeRecordCommandRequest);
// Synchronously get the return value of the API request
RealTimeRecordCommandResponse 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();
}
}setCommand("stop")表示停止錄製。
主動停止錄製,同樣需等待斷流拼接時間長度之後才會產生錄製檔案。
如果錄製的是轉碼流,此處需要將<StreamName>設定成
StreamName加_轉碼模板ID的形式。
配置CDN網域名稱
如果您希望錄製的直播視頻能夠更快速的在網路中進行分發,可以配置一個CDN加速網域名稱。CDN將視頻分發到各地節點,使用者就近訪問而無需串連OSS來源站點,不僅可提升邊緣使用者的訪問速度和體驗,且外網流量費用僅為OSS的50%,有效節省網路費用。具體操作可參見OSS通過CDN加速訪問OSS。
相關文檔
使用直播錄製時遇到問題,請參見直播錄製常見問題。
使用Java SDK,請參見Java SDK使用說明。








