-
-
Save devsoft91/d7f9f258b0bf507fddf5 to your computer and use it in GitHub Desktop.
Revisions
-
paolorossi renamed this gist
Mar 7, 2012 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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'); -
paolorossi created this gist
Mar 7, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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/');