Table of Contents generated with DocToc
- Pili Streaming Cloud server-side library for NodeJS
- URL
- RTMP推流地址: RTMPPublishURL(domain, hub, streamKey, mac, expireAfterSeconds)
- RTMP直播地址: RTMPPlayURL(domain, hub, streamKey)
- HLS直播地址: HLSPlayURL(domain, hub, streamKey)
- HDL直播地址: HDLPlayURL(domain, hub, streamKey)
- 截图直播地址: SnapshotPlayURL(domain, hub, streamKey)
- Hub
- 创建流: hub.Create(streamKey)
- 获得流: hub.Stream(streamKey)
- 列出流: hub.List(prefix, limit, marker)
- 列出正在直播的流: hub.ListLive(prefix, limit, marker)
- 批量查询直播实时信息: hub.batchLiveStreamsInfo(items)
- Stream
- 流信息: stream.Info()
- 禁用流: stream.Disable()
- 启用流: stream.Enable()
- 查询直播状态: stream.LiveStatus()
- 保存直播回放: stream.Save(start, end)
- 查询直播历史: stream.HistoryActivity(start, end)
- 保存直播截图: stream.saveSnapshot()
- 修改流转码配置: stream.updateConverts(profiles)
// install latest version
npm install piliv2 --save
var Pili = require('piliv2');
var ACCESS_KEY = 'QiniuAccessKey';
var SECRET_KEY = 'QiniuSecretKey';
var HUB = 'PiliHubName'; // The Hub must be exists before use
var credentials = new Pili.Credentials(ACCESS_KEY, SECRET_KEY);
var url = Pili.publishURL(credentials,'publish-rtmp.test.com', 'PiliSDKTest', 'streamkey', 60);
console.log(url);
// rtmp://publish-rtmp.test.com/PiliSDKTest/streamkey?e=1466652726&token=9roGzeeJkZh4y5gHpzT7Uv1CIw0KiVV8K4dfXRY0:bYZGIdK-tjjAfyIwbShQ5Bb1kBY=
var url = Pili.rtmpPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
console.log(url);
// rtmp://live-rtmp.test.com/PiliSDKTest/streamkey
var url = Pili.hlsPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
console.log(url);
// http://live-rtmp.test.com/PiliSDKTest/streamkey.m3u8
var url = Pili.hdlPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
console.log(url);
// http://live-rtmp.test.com/PiliSDKTest/streamkey.flv
var url = Pili.snapshotPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
console.log(url);
// http://live-rtmp.test.com/PiliSDKTest/streamkey.jpg
var hub = new Pili.Hub(credentials, HUB);
hub.createStream(streamKey, function(err, stream) {
if (!err) {
console.log(stream);
} else {
// Log error
console.log(err, err.errorCode, err.httpCode);
}
});
var stream = hub.newStream(streamKey);
stream.loadInfo(function(err) {
if (!err) {
console.log(stream);
} else {
console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
})
stream.liveInfo(function(err, status) {
if (!err) {
console.log(status);
} else {
console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
});
var listOptions = {
'liveonly': false,
'prefix': '',
'limit': 2,
};
var listCallBack = function(err, marker, streams) {
if (!err) {
streams.forEach(function(stream) {
console.log(stream);
});
if (marker) {
listOptions.marker = marker;
hub.listStreams(listOptions, listCallBack);
}
} else {
console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
}
hub.listStreams(listOptions, listCallBack);
hub.batchLiveStreamsInfo([streamKey], function (err, items) {
if (!err) {
console.log('live streams: ');
items.forEach(function(item) {
console.log(item);
});
}
else {
console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
});
stream.disable(-1, function(err) {
console.log(stream.disabledTill);
});
stream.enable(function(err) {
console.log(stream.disabledTill);
});
stream.liveInfo(function(err, status) {
if (!err) {
console.log(status);
} else {
console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
});
var publishHistoryOptions = {
start : null, // optional, in second, unix timestamp
end : null, // optional, in second, unix timestamp
};
stream.publishHistory(publishHistoryOptions, function(err, history) {
if (!err) {
console.log(history);
} else {
console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
})
var savePlaybackOptions = {
start : null, // optional, in second, unix timestamp
end : null, // optional, in second, unix timestamp
format: null, // optional, file format
};
stream.savePlayback(savePlaybackOptions, function(err, m3u8Name) {
if (!err) {
console.log(m3u8Name);
} else {
console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
});
var saveSnapshotOptions = {
fname: streamKey // optional, file name
};
stream.saveSnapshot(saveSnapshotOptions, function(err, snapshotName) {
if (!err) {
console.log(snapshotName);
} else {
console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
});
stream.updateConverts(['480p', '720p'], function (err) {
if (!err) {
console.log('update converts success');
}
else {
console.log('update converts error: ' + err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
}
})