全部產品
Search
文件中心

Object Storage Service:Node.js管理LiveChannel

更新時間:Jun 26, 2024

本文介紹LiveChannel常見操作,例如建立LiveChannel、列舉LiveChannel及刪除LiveChannel等。

建立LiveChannel

通過RTMP協議上傳音視頻資料前,必須先調用該介面建立一個LiveChannel。調用PutLiveChannel介面會返回RTMP推流地址,以及對應的播放地址。

以下代碼用於建立LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // yourregion填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為oss-cn-hangzhou。
  region: 'yourRegion',
  // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // 填寫Bucket名稱。
  bucket: 'examplebucket',
})

// 填寫LiveChannel名稱,LiveChannel名稱不能包含正斜線(/),例如mychannel。
const cid = 'mychannel';
const conf = {
  // 填寫LiveChannel的描述資訊,最大長度不能超過128位元組。
  Description: 'this is my channel',
  // 指定LiveChannel的狀態。此處指定為enabled,表示啟用LiveChannel。如需禁用LiveChannel,請將該參數設定為disabled。
  Status: 'enabled',
  Target: {
    // 指定轉儲的類型,目前僅支援HLS。
    Type: 'HLS',
    // 指定每個ts檔案的時間長度,單位為秒。
    FragDuration: '10',
    // 當Type為HLS時,指定m3u8檔案中包含ts檔案的個數。
    FragCount: '5',
    // 當Type為HLS時,指定產生的m3u8檔案的名稱。名稱必須以”.m3u8”結尾,長度範圍為6~128位元組。
    PlaylistName: 'playlist.m3u8'
  }
};

// 建立LiveChannel。
store.putChannel(cid, conf).then(r=>console.log(r))

擷取LiveChannel資訊

以下代碼用於擷取指定的LiveChannel資訊:

const OSS = require('ali-oss');

const store = new OSS({
  // yourregion填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為oss-cn-hangzhou。
  region: 'yourRegion',
  // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // 填寫Bucket名稱。
  bucket: 'examplebucket',
})

// 填寫LiveChannel名稱
const cid = 'mychannel'; 

// 擷取LiveChannel資訊。
store.getChannel(cid).then(r=>console.log(r));

設定LiveChannel狀態

以下代碼用於設定LiveChannel狀態:

const OSS = require('ali-oss');

const store = new OSS({
  // yourregion填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為oss-cn-hangzhou。
  region: 'yourRegion',
  // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // 填寫Bucket名稱。
  bucket: 'examplebucket',
})

// 填寫LiveChannel名稱。
const cid = 'mychannel';

// LiveChannel分為啟用(enabled)和禁用(disabled)兩種狀態。
// LiveChannel處於disabled狀態時,OSS會禁止您向該LiveChannel進行推流操作。如果您正在向該LiveChannel推流,那麼推流的用戶端會被強制斷開(會有10s左右的延遲)。
store.putChannelStatus(cid, 'disabled').then(r=>console.log(r));

擷取LiveChannel狀態資訊

以下代碼用於擷取指定LiveChannel的推流狀態資訊:

const OSS = require('ali-oss');

const store = new OSS({
  // yourregion填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為oss-cn-hangzhou。
  region: 'yourRegion',
  // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // 填寫Bucket名稱。
  bucket: 'examplebucket',
})

// 填寫LiveChannel名稱。
const cid = 'mychannel';
// 擷取LiveChannel狀態資訊。
store.getChannelStatus(cid).then(r=>console.log(r))

產生LiveChannel播放清單

PostVodPlaylist介面用於為指定的LiveChannel產生一個點播用的播放清單。OSS會查詢指定時間範圍內由該LiveChannel推流產生的ts檔案,並將其拼裝為一個m3u8播放清單。

以下代碼用於產生LiveChannel播放清單:

const OSS = require('ali-oss');

const store = new OSS({
  // yourregion填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為oss-cn-hangzhou。
  region: 'yourRegion',
  // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // 填寫Bucket名稱。
  bucket: 'examplebucket',
})

// 填寫LiveChannel名稱。
const cid = 'mychannel';

const r = await this.store.createVod(cid, 're-play', {
  // 指定查詢ts檔案的起始時間,格式為Unix時間戳記。
  startTime: 1460464870,
  // 指定查詢ts檔案的終止時間,格式為Unix時間戳記。
  endTime: 1460465877
  // EndTime必須大於StartTime,且時間跨度不能大於1天。
}).then(r=>console.log(r))

列舉指定的LiveChannel

以下代碼用於列舉指定的LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // yourregion填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為oss-cn-hangzhou。
  region: 'yourRegion',
  // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // 填寫Bucket名稱。
  bucket: 'examplebucket',
})

const r = await this.store.listChannels({
  // 列舉首碼為'my'的LiveChannel。
  prefix: 'my',
  // 指定返回的LiveChannel的最大個數為3個。
  'max-keys': 3
}).then(r=>console.log(r))

擷取LiveChannel推流記錄

GetLiveChannelHistory介面用於擷取指定LiveChannel的推流記錄。使用GetLiveChannelHistory介面最多會返回指定LiveChannel最近的10次推流記錄。

以下代碼用於擷取LiveChannel推流記錄:

const OSS = require('ali-oss');

const store = new OSS({
  // yourregion填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為oss-cn-hangzhou。
  region: 'yourRegion',
  // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // 填寫Bucket名稱。
  bucket: 'examplebucket',
})

// 填寫LiveChannel名稱。
const cid = 'mychannel';
// 擷取LiveChannel推流記錄。
store.getChannelHistory(cid).then(r=>console.log(r))

刪除LiveChannel

重要
  • 當有用戶端正在向LiveChannel推流時,刪除請求會失敗。

  • DeleteLiveChannel介面只會刪除LiveChannel本身,不會刪除推流產生的檔案。

以下代碼用於刪除指定的LiveChannel:

const OSS = require('ali-oss');

const store = new OSS({
  // yourregion填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為oss-cn-hangzhou。
  region: 'yourRegion',
  // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // 填寫Bucket名稱。
  bucket: 'examplebucket',
})

// 填寫LiveChannel名稱。
const cid = 'mychannel'; 

// 刪除LiveChannel。
store.deleteChannel(cid).then(r=>console.log(r))

相關文檔