Skip to content

Commit

Permalink
Merge pull request #42 from googollee/master
Browse files Browse the repository at this point in the history
使用saveas生成hls回放
  • Loading branch information
why404 committed Feb 28, 2016
2 parents 7d51c65 + 733de5e commit 6b38a80
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 81 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ hub.deleteStream(streamId, function(err, data) {

## History

- 1.5.3
- Use saveAs in hlsPlaybackUrls, *need async function*
- 1.5.2
- Update `Stream`'s `hosts`
- Add `startFrom` in `Stream status`
Expand Down
129 changes: 65 additions & 64 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,16 @@ stream.update(options, function(err, stream) {
* Disable a Stream
*/
stream.disable(function(err, stream) {
console.log(stream.disabled);
// true
console.log(stream.disabled);
// true
});

/**
* Enable a Stream
*/
stream.enable(function(err, stream) {
console.log(stream.disabled);
// false
console.log(stream.disabled);
// false
});

/**
Expand All @@ -229,12 +229,12 @@ stream.status(function(err, status) {
// "addr": "222.73.202.226:2572",
// "startFrom": "2015-09-10T05:58:10.289+08:00",
// "status": "disconnected",
// "bytesPerSecond": 16870.200000000001,
// "framesPerSecond": {
// "audio": 42.200000000000003,
// "video": 42.200000000000003,
// "data": 0.066666666666666666
// }
// "bytesPerSecond": 16870.200000000001,
// "framesPerSecond": {
// "audio": 42.200000000000003,
// "video": 42.200000000000003,
// "data": 0.066666666666666666
// }
// }
}
});
Expand Down Expand Up @@ -277,89 +277,90 @@ console.log(urls);
* Get Stream segments
*/
var options = {
startTime : null, // optional, in second, unix timestamp
endTime : null, // optional, in second, unix timestamp
limit : null // optional
startTime : null, // optional, in second, unix timestamp
endTime : null, // optional, in second, unix timestamp
limit : null // optional
};

stream.segments(options, function(err, segments) {
if (!err) {
console.log(segments);
// Log stream segments
// [
// {
// "start": 1440196065,
// "end": 1440196124
// },
// {
// "start": 1440198072,
// "end": 1440198092
// },
// ...
// ]
}
});
stream.segments(options, function(err, segments) {
if (!err) {
console.log(segments);
// Log stream segments
// [
// {
// "start": 1440196065,
// "end": 1440196124
// },
// {
// "start": 1440198072,
// "end": 1440198092
// },
// ...
// ]
}
});

/**
* Generate HLS playback URL
*/
var start = 1440196065;
var end = 1440196105;

var urls = stream.hlsPlaybackUrls(start, end);
console.log(urls);
// {
// ORIGIN: 'http://scv02k.playback1.z1.pili.qiniucdn.com/coding/55d7fa0ee3ba5723280000cc.m3u8?start=1440196065&end=1440196105'
// }
var urls = stream.hlsPlaybackUrls(start, end, function(err, urls) {
console.log(urls);
// {
// ORIGIN: 'http://scv02k.playback1.z1.pili.qiniucdn.com/coding/55d7fa0ee3ba5723280000cc.m3u8?start=1440196065&end=1440196105'
// }
});

/**
* Snapshot Stream
*/
var name = 'imageName.jpg'; // required
var format = 'jpg'; // required
var format = 'jpg'; // required

var options = {
time : 1440196100, // optional, default as now, in second, unix timestamp
notifyUrl : null // optional
time : 1440196100, // optional, default as now, in second, unix timestamp
notifyUrl : null // optional
};

stream.snapshot(name, format, options, function(err, responseData) {
console.log(responseData);
// Log responseData
// {
// "targetUrl": "http://scv02k.static1.z1.pili.qiniucdn.com/snapshots/z1.coding.55d7faf0e3ba5723280000cd/imageName.jpg",
// "persistentId": "z1.55d7a6e77823de5a49a8899a"
// }
//
// You can get saving state via Qiniu fop service using persistentId.
// API: `curl -D GET http://api.qiniu.com/status/get/prefop?id=<PersistentId>`
// Doc reference: `http://developer.qiniu.com/docs/v6/api/overview/fop/persistent-fop.html#pfop-status`
console.log(responseData);
// Log responseData
// {
// "targetUrl": "http://scv02k.static1.z1.pili.qiniucdn.com/snapshots/z1.coding.55d7faf0e3ba5723280000cd/imageName.jpg",
// "persistentId": "z1.55d7a6e77823de5a49a8899a"
// }
//
// You can get saving state via Qiniu fop service using persistentId.
// API: `curl -D GET http://api.qiniu.com/status/get/prefop?id=<PersistentId>`
// Doc reference: `http://developer.qiniu.com/docs/v6/api/overview/fop/persistent-fop.html#pfop-status`
});

/**
* Save Stream as a file
*/
var name = 'videoName.mp4'; // required
var format = 'mp4'; // required
var start = 1440196065; // required, in second, unix timestamp
var end = 1440196105; // required, in second, unix timestamp
var name = 'videoName.mp4'; // required
var format = 'mp4'; // optional
var start = 1440196065; // required, in second, unix timestamp
var end = 1440196105; // required, in second, unix timestamp

var options = {
notifyUrl : null // optional
notifyUrl : null, // optional
};

stream.saveAs(name, format, start, end, options, function(err, responseData) {
// Log responseData
// {
// "url": "http://scv02k.media1.z1.pili.qiniucdn.com/recordings/z1.coding.55d7faf0e3ba5723280000cd/videoName.m3u8",
// "targetUrl": "http://scv02k.vod1.z1.pili.qiniucdn.com/recordings/z1.coding.55d7faf0e3ba5723280000cd/videoName.mp4",
// "persistentId": "z1.55d7a6e77823de5a49a8899b"
// }
//
// You can get saving state via Qiniu fop service using persistentId.
// API: `curl -D GET http://api.qiniu.com/status/get/prefop?id=<PersistentId>`
// Doc reference: `http://developer.qiniu.com/docs/v6/api/overview/fop/persistent-fop.html#pfop-status`
console.log(responseData);
// Log responseData
// {
// "url": "http://scv02k.media1.z1.pili.qiniucdn.com/recordings/z1.coding.55d7faf0e3ba5723280000cd/videoName.m3u8",
// "targetUrl": "http://scv02k.vod1.z1.pili.qiniucdn.com/recordings/z1.coding.55d7faf0e3ba5723280000cd/videoName.mp4",
// "persistentId": "z1.55d7a6e77823de5a49a8899b"
// }
//
// You can get saving state via Qiniu fop service using persistentId.
// API: `curl -D GET http://api.qiniu.com/status/get/prefop?id=<PersistentId>`
// Doc reference: `http://developer.qiniu.com/docs/v6/api/overview/fop/persistent-fop.html#pfop-status`
console.log(responseData);
});

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ exports.saveStreamAs = function(credentials, streamId, name, format, start, end,
data['name'] = name;
data['start'] = start;
data['end'] = end;
data['format'] = format;

if (format) {
data['format'] = format;
}
if (options) {
if (options.notifyUrl) {
data['notifyUrl'] = options.notifyUrl;
Expand Down
26 changes: 11 additions & 15 deletions lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,17 @@ Stream.prototype.hlsLiveUrls = function() {
return result;
};

Stream.prototype.hlsPlaybackUrls = function(startTime, endTime) {
var result = {};
var hlsPlayHost = this.hosts.playback.hls;
var url = fmt.format('http://%s/%s/%s.m3u8?start=%d&end=%d', hlsPlayHost,
this.hub, this.title, startTime, endTime);
result[ORIGIN] = url;

if (this.profiles) {
this.profiles.forEach(function(profile) {
var profileUrl = fmt.format('http://%s/%s/%s@%s.m3u8?start=%d&end=%d', hlsPlayHost, this.hub, this.title, profile, startTime, endTime);
result[profile] = profileUrl;
});
}

return result;
Stream.prototype.hlsPlaybackUrls = function(startTime, endTime, fn) {
var name = fmt.format("%d", Date.now() / 1000);
this.saveAs(name, null, startTime, endTime, null, function(err, resp) {
if (err) {
fn(err, null);
} else {
var result = {};
result[ORIGIN] = resp.url;
fn(null, result);
}
})
};

Stream.prototype.rtmpPublishUrl = function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pili",
"version": "v1.5.2",
"version": "v1.5.3",
"description": "Pili Streaming Cloud server-side library for NodeJS",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit 6b38a80

Please sign in to comment.