This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream2tv.js
174 lines (156 loc) · 4.44 KB
/
stream2tv.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
var fs = require("fs"),
http = require("http"),
url = require("url"),
path = require("path"),
Browser = require('nodecast-js'),
MediaRendererClient = require('upnp-mediarenderer-client'),
local_address = require('network-address'),
keypress = require('keypress');
var settings = {
localIP: local_address(),
localPort: 34242
};
var filename_o = process.argv[2];
var filename = encodeURIComponent(filename_o);
var url_video = 'http://' + settings.localIP + ':' + settings.localPort +'/' + filename;
var url_subtitle = 'http://' + settings.localIP + ':' + settings.localPort + '/' + filename.replace(/\.[^/.]+$/, ".srt");
keypress(process.stdin);
var streamserver = http.createServer();
streamserver.on('request', function (req, res) {
if (req.url.indexOf(".srt") != -1) {
res.writeHead(200, { "Content-Type": "text/html" });
var fileS = filename_o.replace(/\.[^/.]+$/, ".srt");
fs.readFile(fileS, "binary", function(err, fileS) {
if(err) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(err + "\n");
res.end();
return;
}
res.writeHead(200);
res.write(fileS, "binary");
res.end();
});
}
if (req.url.indexOf(".srt") == -1 && req.url.indexOf(".ico") == -1) {
var path = filename_o;
try {
var stat = fs.statSync(filename_o);
} catch(e) {
console.log("ERROR");
if (e.errno == -2) {
console.log(e.path+": NOT FOUND");
}
process.exit(e.errno);
}
var total = stat.size;
if (req.headers['range']) {
var range = req.headers.range;
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = parseInt(partialstart, 10);
var end = partialend ? parseInt(partialend, 10) : total-1;
var chunksize = (end-start)+1;
var file = fs.createReadStream(path, {start: start, end: end});
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
"transferMode.dlna.org": "Streaming",
"contentFeatures.dlna.org": "DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=017000 00000000000000000000000000",
"CaptionInfo.sec": url_subtitle
});
file.pipe(res);
} else {
res.writeHead(200, {
'Content-Length': 1,
'Content-Type': 'video/mp4',
"transferMode.dlna.org": "Streaming",
"contentFeatures.dlna.org": "DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=017000 00000000000000000000000000",
"CaptionInfo.sec": url_subtitle
});
fs.createReadStream(path, {start: 0, end: 1}).pipe(res);
}
}
});
streamserver.on('connection', function (socket) {
socket.setTimeout(36000000)
})
streamserver.listen(settings.localPort);
var options = {
autoplay: true,
metadata: {
subtitlesUrl: url_subtitle,
url: url_video,
type: "video",
title: path.basename(filename_o)
},
contentType: "video/mp4"
};
var browser = new Browser();
var count = 0
browser.onDevice(function (device) {
if (device.type == 'upnp') {
count++;
}
// Connect to the first upnp device we find.
if (count == 1 && device.type == 'upnp') {
var client = new MediaRendererClient(device.xml);
var playback_status = null;
client.load(url_video, options, function(err, result) {
console.log('Starting ...');
});
client.on('status', function(status) {
// Reports the full state of the AVTransport service the first time it fires,
// then reports diffs. Can be used to maintain a reliable copy of the
// service internal state. Used for debugging.
// console.log(status);
});
client.on('playing', function() {
console.log('Playing ...');
playback_status = "playing";
});
client.on('paused', function() {
console.log('paused');
playback_status = "paused";
});
client.on('stopped', function() {
console.log('stopped');
process.exit();
});
// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
try {
if (key.name == 'p') {
if (playback_status == "playing") {
client.pause();
} else {
client.play();
}
}
if (key.name == 'q') {
client.stop();
}
} catch(e){
console.log("unknown keypress");
}
});
}
device.onError(function (err) {
console.log(err);
});
});
process.stdin.on('keypress', function (ch, key) {
try {
if (key.ctrl && key.name == 'c') {
process.exit();
}
} catch(e){
console.log("unknown keypress");
}
});
browser.start();
process.stdin.setRawMode(true);