Skip to content

Instantly share code, notes, and snippets.

@devsoft91
Forked from paolorossi/html5-video-streamer.js
Created February 27, 2016 15:35
Show Gist options
  • Save devsoft91/d7f9f258b0bf507fddf5 to your computer and use it in GitHub Desktop.
Save devsoft91/d7f9f258b0bf507fddf5 to your computer and use it in GitHub Desktop.

Revisions

  1. @paolorossi paolorossi renamed this gist Mar 7, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions video-streamer.js → html5-video-streamer.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    /*
    * Inspired by: http://stackoverflow.com/questions/4360060/video-streaming-with-html-5-via-node-js
    */

    var http = require('http'),
    fs = require('fs'),
    util = require('util');
  2. @paolorossi paolorossi created this gist Mar 7, 2012.
    29 changes: 29 additions & 0 deletions video-streamer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    var http = require('http'),
    fs = require('fs'),
    util = require('util');

    http.createServer(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);

    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' });
    file.pipe(res);
    } else {
    console.log('ALL: ' + total);
    res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
    fs.createReadStream(path).pipe(res);
    }
    }).listen(1337, '127.0.0.1');
    console.log('Server running at http://127.0.0.1:1337/');