-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathstreamer.js
64 lines (60 loc) · 1.54 KB
/
streamer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var Streamer = require('./streamer-server');
var logger = require('./logger');
var format = require('util').format;
var torrent;
var ready = false;
var boundURL = "not ready";
var ID;
var ip;
var port;
function startStreamer(url, torrentID, localIp) {
ID = torrentID;
ip = localIp;
url = decodeURIComponent(url);
port = parseInt(Math.random() * (8000 - 6000) + 6000);
var streamBuffer = 5 * 1024 * 1024;
if (url.indexOf("youtube") >= 0){
streamBuffer = 3 * 1024 * 1024;
}
torrent = new Streamer(url,
{
youtube: {
hd: true
},
hostname: ip,
progressInterval: 10,
buffer: streamBuffer,
port: port,
writeDir: '',
index: __dirname + "/" + torrentID + '.mp4'
});
torrent.on('ready', function (data) {
logger.Streamer('Ready to Stream, binding to ' + data.streamUrl);
boundURL = data.streamUrl;
ready = true;
});
torrent.on('close', function () {
logger.Debug('Streamer: Stream Closed');
});
torrent.on('progress', function (progress) {
logger.Debug(format('[%d%] Downloaded %dMB - %dMB/s | Peers: %d Seeds: %d | ETA: %dm',
progress.progress.toFixed(0),
(progress.downloaded / 1024 / 1024).toFixed(2),
(progress.downloadSpeed / 1024 / 1024).toFixed(2),
progress.peers,
progress.seeds,
(progress.eta / 60).toFixed(1)));
});
torrent.on('error', function (e) {
logger.error(e);
});
}
function getURL(){
return "http://" + ip + ":" + port + "/" + ID + '.mp4';
}
function getStreamer(){
return torrent;
}
exports.startStreamer = startStreamer;
exports.getURL = getURL;
exports.getStreamer = getStreamer;