文档

Node.js管理LiveChannel

更新时间:

本文介绍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))

相关文档

  • 本页导读 (1)
文档反馈