I am currently working on streaming video using node.js and I recently came across this helpful article. The setup works perfectly when streaming from a local source, but now I need to stream the video from a web source.
However, my specific requirement is to conceal the original URL in the source code. Instead of displaying the original URL like
<source src="http://goo.gl/KgGx0s" type="video/mp4"/>
, I want to show a modified one such as <source src="http://localhost:8888" type="video/mp4"/>
.
To implement this, I have included the following code snippets:
var indexPage, movie_webm, movie_mp4, movie_ogg;
fs.readFile(path.resolve(__dirname,"ANY_LOCAL_VIDEO.mp4"), function (err, data) {
if (err) {
throw err;
}
console.log(data.length);
movie_mp4 = data;
});
http.createServer(function (req, res) {
var reqResource = url.parse(req.url).pathname;
var total;
total = movie_mp4.length;
var range = req.headers.range;
var positions = range.replace(/bytes=/, "").split("-");
var start = parseInt(positions[0], 10);
var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
var chunksize = (end-start)+1;
res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type":"video/mp4"});
res.end(movie_mp4.slice(start, end+1), "binary");
}).listen(8888);
Although this solution works well for local videos, I encountered an issue when trying to use
fs.readFile("http://goo.gl/KgGx0s", function (err, data) {
instead of the aforementioned code. I attempted changing fs.readFile
to fs.filepath
, but the problem persisted with the following error message:
c:\git\personal\streaming-video-html5\server.js:13
throw err;
^
Error: ENOENT, open 'c:\git\personal\streaming-video-html5\http:\goo.gl\KgGx0s'
This error likely occurs due to the changing path. What steps should I take to address this issue? Is there a viable solution available?