Skip to content

Instantly share code, notes, and snippets.

@caingougou
Created August 23, 2017 16:53
Show Gist options
  • Save caingougou/b804a4059e57044b86d1e6c4dc03e0e3 to your computer and use it in GitHub Desktop.
Save caingougou/b804a4059e57044b86d1e6c4dc03e0e3 to your computer and use it in GitHub Desktop.
Quick code for play local video on TV via dlna (inspired by stream2tv.js)
var fs = require("fs"),
http = require("http"),
url = require("url"),
Browser = require('nodecast-js'),
MediaRendererClient = require('upnp-mediarenderer-client'),
local_address = require('network-address');
var settings = {
localIP: local_address(),
localPort: 12345
};
var filename_o = process.argv[2];
var filename = encodeURIComponent(filename_o);
var url_video = 'http://' + settings.localIP + ':' + settings.localPort +'/0';
var url_subtitle = 'http://' + settings.localIP + ':' + settings.localPort + '/' + filename.replace(/\.[^/.]+$/, ".srt");
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);
setTimeout(function () {
var timestamp = 60; // in seconds
var browser = new Browser();
browser.onDevice(function (device) {
device.onError(function (err) {
console.log(err);
});
// console.log(browser.getList()); // list of currently discovered devices
device.play(url_video, timestamp);
});
browser.start();
setTimeout(function () {
browser.destroy(); // destroy your browser
}, 20000);
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment