Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/');
@nicolsondsouza
Copy link

nicolsondsouza commented Nov 14, 2014

Hi,

It works for me that is true,
but I get this error and crash the server if I fast forward it.
Can't render headers after they are sent to the client.
any idea what could be the problem

@foysalit
Copy link

foysalit commented Apr 18, 2015

man you're my hero! seriously, you rock!

@CreeJee
Copy link

CreeJee commented Jul 21, 2015

thanks man!

@uhfg123
Copy link

uhfg123 commented Aug 14, 2015

Is it possible to stream the youtube video through this module? How can i Play youtube video through this module?

@cafe4it
Copy link

cafe4it commented Oct 23, 2015

thanks, it works for meteor js too.

@aida-mirabadi
Copy link

aida-mirabadi commented Oct 27, 2015

awesome.. Thanks, works for me

@hankphung
Copy link

hankphung commented Nov 16, 2015

This cool but I'm looking for solution to stop the readStream when client close or disconnected. :(

@realinit
Copy link

realinit commented Jan 2, 2016

var range = req.headers.range;
console.log("range>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "+range);
var total = fs.statSync("/home/nitin/Minecraft + Node.js + Socket.io = Awesome-dTIv_f-Ll2g.mp4").size;// your video size.. you can use fs.statSync("filename").size to get the size of the video if it is stored in a file system
splitq = range.split(/[-=]/),
start = +splitq[1],
end = splitq[2] ? +splitq[2] : total - 1,
chunkSize = end - start + 1;

    var start = parseInt(start, 10);
    var end = end ? parseInt(end, 10) : total - 1;
    var chunksize = (end - start) + 1;
    console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize);

Use this one for correct it...enjoy

@rajswarnam
Copy link

rajswarnam commented Jan 5, 2016

I am having trouble getting stream to work in chrome .. This works perfectly in Mozilla. Any ideas ? Appreciate all your help.

@felipenoris
Copy link

felipenoris commented Jan 31, 2016

Thank you!

@deefens
Copy link

deefens commented Feb 12, 2016

I have the same issue as rajswarnam, works like a charme on all desktop browsers and mobile firefox & safari, but not mobile chrome for android. Console log shows the same request from chrome mobile than chrome desktop, but the player stays blank.

@silvaros
Copy link

silvaros commented Feb 21, 2016

This helped me learn how to do it. Thanks!

@GrimStal
Copy link

GrimStal commented Jun 24, 2016

Man, you're the best. I spent two hours for tries to send video correctly. Thank you

@acesama
Copy link

acesama commented Jul 11, 2016

works like magic , thx

@BlogBlocks
Copy link

BlogBlocks commented Jul 17, 2016

Works fine. I left it as it was and it played video at http://localhost:1337/

I used it to pipe video to my public port http://127.0.0.1:8000/

When the public port received the video it needed no name - just < source src="http://127.0.0.1:1337" type="video/mp4">

It worked fine to broadcast on internet.

@ricardoalcocer
Copy link

ricardoalcocer commented Jul 25, 2016

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

@vijayrajanna
Copy link

vijayrajanna commented Aug 3, 2016

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

@puigdoalvic
Copy link

puigdoalvic commented Aug 8, 2016

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

NguyenTungs commented Feb 15, 2017

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

realcygnus commented Jun 25, 2017

great stuff

@shalonteoh
Copy link

shalonteoh commented Jul 24, 2017

thank you so so much!!!!!

@swagasoft
Copy link

swagasoft commented Nov 22, 2018

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

@mirajehossain
Copy link

mirajehossain commented Jan 7, 2019

Thanks, man <3, its works as expected.

@JupyterJones
Copy link

JupyterJones commented Feb 20, 2020

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

pinksynth commented Mar 16, 2021

Very helpful! Thank you! ❤️

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