Skip to content

Commit

Permalink
Stream de vídeo
Browse files Browse the repository at this point in the history
  • Loading branch information
emerleite committed May 17, 2011
1 parent fda8dff commit 7e49c60
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions video.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,40 @@
var http = require('http'),
fileSystem = require('fs'),
path = require('path'),
util = require('util');

var server = http.createServer(function(request, response) {
var filePath = path.join(__dirname, 'video.mp4');
var stat = fileSystem.statSync(filePath);

var inicio = 0;
var fim = 0;

var range = request.headers.range;
console.log(range);

if (range) {
inicio = parseInt(range.slice(range.indexOf("bytes=") + 6, range.indexOf("-")));
fim = parseInt(range.slice(range.indexOf("-") + 1, range.length));
}

if (isNaN(fim) || fim == 0) {
fim = stat.size-1;
}

if (inicio > fim) return;

response.writeHead(206, {
'Content-Type': 'video/mp4',
'Content-Length': stat.size,
'Content-Range':'bytes '+ inicio + '-' + fim + '/' + stat.size,
'Transfer-Encoding':'chunked'
});

var readStream = fileSystem.createReadStream(filePath, { flags: 'r', start: inicio, end: fim});

util.pump(readStream, response);
});

server.listen(2000);

Binary file added video.mp4
Binary file not shown.

0 comments on commit 7e49c60

Please sign in to comment.