VidStreamer.js – a simple Node.js video streamer

I’ve been really keen to learn Node.js for a while now, so I decided to translate the video gallery I wrote a while ago into Node (nearly done!). At the back end, the video gallery uses xmoovStream Server (site currently dead) to stream the videos to the popular JW Player (I used Flash videos as I wanted to support bitrate switching).

As using a LAMP stack to support a Node app seemed wrong, I wrote a simple JavaScript video streamer. Most of the requirements came from looking at how xmoovStream Server works.

  • Supports http pseudostreaming
  • Streams Flash video and can resume streams
  • Streams other video and file formats and can resume these streams too
  • Works with JW Player’s bitrate switching

Source on GitHub

Using VidStreamer

To make a standalone video streamer, try something like this

var http = require("http");
var vidStreamer = require("vid-streamer");
var app = http.createServer(vidStreamer);
app.listen(3000);
console.log("VidStreamer.js up and running on port 3000");

And here’s an example of including it in an Express app

var app = require("express").createServer();
var vidStreamer = require("vid-streamer");
app.get("/videos/", vidStreamer);
app.listen(3000);

Settings

There are a few things to twiddle in /config/vidStreamer.json. (Please rename vidStreamer-sample.json.)

  • mode – Not actually used right now. :)
  • forceDownload – Tell the browser to show a save file dialog.
  • random – Show a random file instead of the one named (see below).
  • rootFolder – The root folder from which files are returned. Accessing subfolders is possible by putting them in the path.
  • rootPath – The path assumed to match up with the root folder.
  • server – Server string returned in the header.

Standalone example

{
	"mode": "development",
	"forceDownload": false,
	"random": false,
	"rootFolder": "/path/to/videos/",
	"rootPath": "videos/",
	"server": "VidStreamer.js/0.1"
}

Example for Express. (Note that rootPath should be relative to the root URL of your Express app.)

{
	"mode": "development",
	"forceDownload": false,
	"random": false,
	"rootFolder": "/path/to/express/public/",
	"rootPath": "",
	"server": "VidStreamer.js/0.1"
}

Serving random files

You can use VidStreamer to serve up random files instead of the actual file requested. This can be useful if you’re demoing an app that’s supposed to have hundreds of videos but you don’t want to go to the trouble of making them all.

  • Which file to return is calculated from the requested file name, so the same one will be returned each time for the same name. This is useful if you want to switch between video bitrates for example.
  • The file returned will be of the same type (same extension) as the requested file.
  • The file returned will be from the same folder as the requested file.

Thanks

I hadn’t really thought about how to write video streamer before, so Devendra Tewari’s post and of course the xmoovStream Server source were very useful to me.

Source on GitHub

Legal fun

Copyright © 2012 Andrew Weeks http://meloncholy.com

VidStreamer.js is licensed under the MIT licence.

2 thoughts on VidStreamer.js – a simple Node.js video streamer

    • There are a couple of examples above (in the Using VidStreamer section). If that’s not enough then I may be able to make another one, but honestly there isn’t much more to it than that. I haven’t seen any security errors myself, though I was able to create one just now by trying to stream a video that I didn’t have permissions to access; the browser showed a 404 and the log said operation not permitted (error via fs). But that seems fairly straightforward, so I’m assuming your problem is something else. Do you have any more info that could help?

Let me know what you think

Your email address will not be published. Required fields are marked *