Created
July 22, 2014 16:24
-
-
Save sergiors/c7e409b5512fda54cf73 to your computer and use it in GitHub Desktop.
video streamer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs"); | |
module.exports = { | |
play: function(req, res) { | |
var path = "video.mp4"; | |
var stat = fs.statSync(path); | |
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; | |
// console.log("range: " + start + " - " + end + " = " + chunksize); | |
res.writeHead(206, { | |
"Content-Range": "bytes " + start + "-" + end + "/" + total, | |
"Accept-Ranges": "bytes", | |
"Content-Length": chunksize, | |
"Content-Type": "video/mp4" | |
}); | |
fs.createReadStream(path, { | |
start: start, | |
end: end | |
}) | |
.pipe(res); | |
} else { | |
res.writeHead(200, { | |
"Content-Length": total, | |
"Content-Type": "video/mp4" | |
}); | |
fs.createReadStream(path).pipe(res); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment