-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
126 lines (95 loc) · 3.48 KB
/
index.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
var cliParser = require("./cliParser");
var http = require("http");
var fs = require("fs");
var path = require("path");
var srt2vtt = require('srt-to-vtt');
console.log("\r\n");
console.log('Simple Streamer | by : Eslam Hamouda (@EslaMx7) | www.eslamx.com');
console.log('Project page on Github : https://github.com/EslaMx7/Simple-Streamer');
var options = cliParser(process.argv);
if (options.fileName == "") {
console.log("please select the video file you want to stream.");
process.exit(1); // Terminate Program.
}
if (options.subtitle.lastIndexOf('.srt') == options.subtitle.length - 4) {
// Convert .srt to .vtt
var vtt = options.subtitle.replace(".srt", ".vtt");
fs.createReadStream(options.subtitle)
.pipe(srt2vtt())
.pipe(fs.createWriteStream(vtt))
options.subtitle = vtt;
}
// console.log(options);
/**
* Sends a static file to the HTTP client, supporting partial transfers.
*
* @req HTTP request object
* @res HTTP response object
* @fn Path to file that should be sent
* @contentType MIME type for the response (defaults to HTML)
*/
function sendFile(req, res, fn, contentType) {
contentType = contentType || "text/html";
fs.stat(fn, function (err, stats) {
var headers;
if (err) {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Could not read file");
return;
}
var range = req.headers.range || "";
var total = stats.size;
if (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;
headers = {
"Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": contentType
};
res.writeHead(206, headers);
} else {
headers = {
"Accept-Ranges": "bytes",
"Content-Length": stats.size,
"Content-Type": contentType
};
res.writeHead(200, headers);
}
var readStream = fs.createReadStream(fn, { start: start, end: end });
readStream.pipe(res);
});
}
var server = http.createServer(function (req, res) {
if (req.method == "GET" && req.url == "/") {
res.write("<head><title>Simple Streamer | by : Eslam Hamouda (@EslaMx7)</title></head>");
res.write('<video style="width:100%;height:auto;" controls="controls" > <source type="video/mp4" src="' + options.fileName + '"> <track kind="subtitles" src="' + options.subtitle + '" label="English Subtitles" srclang="en" default /></video>');
res.end();
} else if (req.method == "GET" && req.url == "/" + options.fileName) {
/*fs.readFile(options.fileName,function(err,content){
res.writeHead(200,{'Content-Type':'video/mp4'});
res.end(content);
});*/
sendFile(req, res, options.fileName, "video/mp4");
} else if (req.method == "GET" && req.url == "/" + options.subtitle) {
fs.readFile(options.subtitle, function (err, content) {
res.writeHead(200, { 'Content-Type': 'text/vtt' });
res.end(content);
});
} else {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end();
}
})
server.listen(process.env.SS_PORT || options.port || 7000);
console.log("Server listening on port :" + server.address().port);
server.on('error', function (err) {
console.log(err);
})
console.log("Press [Ctrl + C] to exit.")
module.exports = this;