Skip to content

Instantly share code, notes, and snippets.

@paolorossi
Created March 7, 2012 13:21
Show Gist options
  • Save paolorossi/1993068 to your computer and use it in GitHub Desktop.
Save paolorossi/1993068 to your computer and use it in GitHub Desktop.
Node.js HTML5 video streamer
/*
* 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');
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/');
@ricardoalcocer
Copy link

I started this side project based on code on this Gist : https://github.com/ricardoalcocer/node_mediasite

@vijayrajanna
Copy link

Video streaming server under a minute.. Excellent work !!!

@puigdoalvic
Copy link

It works! But I have one question,

Can I save the current time ends my video in my Javascript? I need to save the current time when start the video in my navigator and when finished the video in the navigator or when I close my navigator.

@tkanike
Copy link

tkanike commented Dec 22, 2016

Nice work. i need to prevent download option on videos in all browser to the users.

can please help

@steebchen
Copy link

steebchen commented Jan 4, 2017

You're using a sync method using Node.js. That's probably the worst thing you could do since the whole process hangs while .statSync() gets invoked.

@NguyenTungs
Copy link

Hi @paolorossi.
Thanks for share your code. I want to ask to you a question!
How to prevent download video when streaming for user? Thanks!

@jersobh
Copy link

jersobh commented Apr 24, 2017

How would be a live stream? I mean, this way every time you open "localhost:port", it plays the file from the beginning over again

@realcygnus
Copy link

great stuff

@shalonteoh
Copy link

thank you so so much!!!!!

@swagasoft
Copy link

yea works. But i need help on how to make play on selected video.

@mirajehossain
Copy link

Thanks, man <3, its works as expected.

@JupyterJones
Copy link

worked fine I can ssh pipe videos from my VPS

@chawk
Copy link

chawk commented Sep 18, 2020

The best!

@emdin
Copy link

emdin commented Oct 16, 2020

9 years, and still works flawlessly. Kudos!

@pinksynth
Copy link

Very helpful! Thank you! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment